PDA

View Full Version : i need some more C help




andrewface
Feb 8, 2007, 05:15 PM
i need to write a program that asks the user for the number of days of the week and the starting day of the month and i need it to print out a months calendar

any advice?

edit: heres what ive got so far

#include <stdio.h>

int main() {
// declare variables
int numberofdays, startday;
char nameofmonth[10];
//Explains program and asks for the name of the month, and week start number
printf("This program creates a calendar for one month\nPlease enter the name of the month:");
scanf("%s", nameofmonth);
printf("Please enter the number of days in the month:");
scanf("%d", &numberofdays);
printf("Please enter the day of the week that the month starts; starting with Sunday 1-7:");
scanf("%d", &startday);
printf("%17s\nSun Mon Tue Wed Thu Fri Sat\n", nameofmonth);
printf("1 2 3 4 5 6 7"); //lost here this doesnt work...

return 0;

}



bousozoku
Feb 8, 2007, 05:54 PM
You'd probably want to use the tab 't character to separate things to make them a bit neater.

Also, you'll likely want to use a calculation to figure out how many weeks to loop. You know how to use for loops, right?

The modulo operator % should come in handy, too.

andrewface
Feb 8, 2007, 05:56 PM
You'd probably want to use the tab 't character to separate things to make them a bit neater.

Also, you'll likely want to use a calculation to figure out how many weeks to loop. You know how to use for loops, right?

The modulo operator % should come in handy, too.

yes i have no idea what calculations to use in the for loops thats the thing

scan
Feb 8, 2007, 06:26 PM
I don't understand what your program is suppose to do. Could you explain it a little bit clearer?

All your program does is takes the input. Not to be rude but a monkey could have written that code. You could at least try to solve the problem yourself instead of asking people to do your homework.

bousozoku
Feb 8, 2007, 06:28 PM
yes i have no idea what calculations to use in the for loops thats the thing

Perhaps, then, you should sit down with pencil and paper and figure out how you would do it step-by-step. Once you figure out how that works, you can translate it to code.

andrewface
Feb 8, 2007, 06:35 PM
I don't understand what your program is suppose to do. Could you explain it a little bit clearer?

All your program does is takes the input. Not to be rude but a monkey could have written that code. You could at least try to solve the problem yourself instead of asking people to do your homework.

im one dumb monkey then

toddburch
Feb 8, 2007, 06:39 PM
Andrew, some free advice...

You have to be able to communicate your thoughts onto paper (or in type) to solve "math" type problems (aka word problems). Once you can do it by math, you can then write code that implements the math.

Fumbling with code before you have an action plan is a waste of time. Take the time to write out what it is you need to do, as a series of steps, then approach the problem from a coding standpoint.

- get the month (this goes on the top of the report)
- get the # of days in the month
- get the first day of the month.

You have that so far.

The first day of the month will be your trigger to starting printing with "1" when you get to it in the first loop. For instance, if the day entered was 4, then for the first 3 positions in the row for the first week, you'll print blanks. Once you hit your target day, then you can start clicking off 1, 2, 3, and so on.

You need two loops - an outer loop and an inner loop.

The outer loop will print the line you build in the inner loop and manage the CRLF to create make the report look like a table.

The inner loop will build the print line with

1) either blanks and numbers (for the first week), or
2) all numbers (for the first, middle or last weeks) or
3) numbers and blanks (for the last week).

Your trigger to quit the loops is when you've accounted for "numberofdays" days.

So, keep coding with this in mind and see what you come up with.

Todd

andrewface
Feb 8, 2007, 06:45 PM
Andrew, some free advice...

You have to be able to communicate your thoughts onto paper (or in type) to solve "math" type problems (aka word problems). Once you can do it by math, you can then write code that implements the math.

Fumbling with code before you have an action plan is a waste of time. Take the time to write out what it is you need to do, as a series of steps, then approach the problem from a coding standpoint.

- get the month (this goes on the top of the report)
- get the # of days in the month
- get the first day of the month.

You have that so far.

The first day of the month will be your trigger to starting printing with "1" when you get to it in the first loop. For instance, if the day entered was 4, then for the first 3 positions in the row for the first week, you'll print blanks. Once you hit your target day, then you can start clicking off 1, 2, 3, and so on.

You need two loops - an outer loop and an inner loop.

The outer loop will print the line you build in the inner loop and manage the CRLF to create make the report look like a table.

The inner loop will build the print line with

1) either blanks and numbers (for the first week), or
2) all numbers (for the first, middle or last weeks) or
3) numbers and blanks (for the last week).

Your trigger to quit the loops is when you've accounted for "numberofdays" days.

So, keep coding with this in mind and see what you come up with.

Todd

thanks alot ima keep going at it

scan
Feb 8, 2007, 07:03 PM
im one dumb monkey then

my comment was not to insult you. It was actually suppose to help you. I know that you're just starting out but asking for people to do your homework is not going to make you a better programmer. You need to practice get yourself thinking like a programmer. You learn from experience how to tackle types of problems.

toddburch
Feb 8, 2007, 11:05 PM
Just an FYI - I worked it out. So, when you're ready with your next hurdle, holler.


[Session started at 2007-02-08 23:01:19 -0600.]
This program creates a calendar for one month
Please enter the name of the month: December
Please enter the number of days in the month: 31
Please enter the day of the week that the month starts; starting with Sunday 1-7: 7


December
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

Calendar has exited with status 0.


and...


[Session started at 2007-02-08 23:03:13 -0600.]
This program creates a calendar for one month
Please enter the name of the month: February
Please enter the number of days in the month: 28
Please enter the day of the week that the month starts; starting with Sunday 1-7: 1


February
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28

Calendar has exited with status 0.


Todd

bousozoku
Feb 9, 2007, 12:01 AM
Just an FYI - I worked it out. So, when you're ready with your next hurdle, holler.
...
Todd

Don't you think you've already helped too much? He's supposed to be paying attention in class and reading his books and learning to solve problems on his own, as well as code the solutions.

Maybe, you should consider waiting until he's got it running fairly well.

toddburch
Feb 9, 2007, 12:27 AM
Don't you think you've already helped too much? He's supposed to be paying attention in class and reading his books and learning to solve problems on his own, as well as code the solutions.

No I don't think I've helped too much, and neither you or I know that he is or is not doing what you say he should be doing. The guy is obviously struggling with basics here. I simply tried to point his head in the right direction.

Maybe, you should consider waiting until he's got it running fairly well.

Am. I don't categorize showing the output as any kind of give-away. The earlier prose was to get him to think about the overall objectives and thinking (and hopefully heading) in the right direction.

Todd