Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

WhiteRabbit

macrumors newbie
Jan 11, 2005
26
0
Software development can be quite challenging. To solve a problem the programmer must break down the problem to the smallest factors possible. You really have to think like a machine.

I notice that you are using pretty much the shortest possible identifiers possible. Although there is less code, the code is not very readable. It's a tremendous help to anyone reading your code (including yourself, and however good a programmer you are, you will spend more time reading your code than writing) that you use meaningful variable and method names. Most IDEs will autocomplete identifiers for you, so don't be afraid of being to descriptive.

It seems as though you have the mod operator backwards. Just remember that mod is a division. Whatever it is you want to divide goes on the left, and what you want to divide by goes on the right. Mod returns the remainder of this division. 7%8=7, 8%7=1, 7%7=0.

Also, you have a nested for loop (for loop inside of a for loop). These can be very useful for iterating a grid with x and y for example (this calendar view does not need one). The problem with your nested loop is that you are using the same variable to iterate both loops. Although the syntax is legal, it's easy to setup some errors doing this. The for loop inside is changing the same variable that the outside loop is using. It's best to avoid having any code other than the for loop itself modify the iterator.

Code:
int daysInMonth = 30, firstDay = 3;
// display a header
printf (" S\t M\t T\t W\t T\t F\t S");
// insert some blank days
for (int i=1; i<firstDay; i++)
  printf ("\t");
// iterate all the days of the month
for (int thisDay=1; thisDay<=daysInMonth; thisDay++) {
  // display the day and follow it with a tab
  printf ("%2d\t", thisDay);
  // if this happens to be a Saturday (column is a multiple of 7
  // we can find out the column by adding thisDay and firstDay
  if ( (thisDay+firstDay-1) % 7 == 0) // firstDay is 1 to 7, so we must subtract 1
    // then we'll need to break the line
    printf ("\n");
}
 

macsmurf

macrumors 65816
Aug 3, 2007
1,200
948
I'm feeling so disappointed with my ability. I've read the first 10 chapters from King's book and tried to do all of the chapter projects. I could do correctly as few as half of the chapter projects only. Learning C is the first programming experience of mine and I've been doing it on my own because I can't go to a formal class.

I've seen that so many guys of you have taught yourself at least one programming language and it seems that most of you all have later become a true programmer/expert. It's very possible that I was born to learn sth else and not programming. The more I learn, the more interested I am in programming, but it also seems that the more challenging I find learning programming by self-study. Does any one of you have a similar experience when you first tried to pick up a programming language? I'm feeling so frustrated that when I look at a chapter it seems that there are only a few basic principles, all seeming basic/understandable/not difficult to comprehend, but when it comes to doing problems/projects/exercises I always get stuck on applying principles/theory to problems. Most of the exercises that I've posted in here are said to be simple, not complicated, but I've been struggling to get them right. So disappointed with myself.

OK, so first of all: Programming is hard! I tend to think of it in a way comparable to math. Most would agree that math is hard. The hardest part about math is applying principles to solve a problem. After a while you develop an intuition on how to solve certain kinds of problems, but the only way to develop that intuition is by working at it. Same thing with programming.

However, programming is also fun! Part of the fun is chipping away at a problem for hours on end until you get it right. If that is not your definition of fun you will find programming intensely frustrating. It's more of a mindset thing than anything else. Mind you, C is not exactly the best language to start out with as there are a lot of details you need to get right in order for your program to work properly. You might have a better time with something like java, although object oriented programming can be hard to get your head around, but at least you don't have to deal with pointers.

Anyway, if you keep at it, eventually it should get easier, but it takes a lot of work :)
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
I'm feeling so disappointed with my ability. I've read the first 10 chapters from King's book and tried to do all of the chapter projects. I could do correctly as few as half of the chapter projects only. Learning C is the first programming experience of mine and I've been doing it on my own because I can't go to a formal class.

I wouldn't get so discouraged. You've been asking good questions (and hopefully getting decent answers :)) and you've really been moving at a pretty strong pace, it seems.

Maybe slow down a bit. And maybe just work on something fun, rather than another exercise.

It takes a while to get the hang of this stuff. But the good news is once you understand it, then picking up additional languages and new devices and new ideas and such will be quite easy and fun. Just hang in there and understand there is a lot of work in getting started.

Good luck and keep the questions coming. Hopefully people can continue to help.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.