Monday, July 27, 2009

C program help again?

using C program


#include%26lt;stdio.h%26gt;


main()


{


printf


scanf


}


how can i do this program to work i dont even know how to start thanks





Write a program to print a one month calendar. The user specifies the number of days in the month and the day of the week on which the month begins. The users input is bolded and underlined in the sample.





Enter number of days in month: 31








Enter starting day of the week (1=Sun, 7=Sat): 3





S M T W T F S








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





Users should not enter days in month less than 28 or greater than 31. Likewise users should not enter starting days of the week less than 1 or greater than 7. If they do, display an error message giving them the limits and allow them to re-enter a value before printing the calendar.





Hint - Use a for statement to print the calendar that uses a variable i to count from 1 to number of days in month

C program help again?
I can answer your question if you please be clear on the starting day of the week.


There are two options :


1.User enters the day on which first of the month falls


2.or date of first sunday or any other day falls





Please make me clear about this.


I will post the code very shortly





So as of now I considered that the input is week day number as per your example(1- Sun, 2=Mon etc)


Here is the code:





#include %26lt;stdio.h%26gt;


main()


{


int iNo_Of_Days;


int iWeek;


int iLoop;


clrscr();


printf("Enter number of days in the month: ");


scanf("%d",%26amp;iNo_Of_Days);


if(iNo_Of_Days==31 || iNo_Of_Days==30 || iNo_Of_Days==28 || iNo_Of_Days==29)


{


printf("Enter starting day of the week : ");


scanf("%d",%26amp;iWeek);


if(iWeek%26gt;=1 %26amp;%26amp; iWeek%26lt;=7)


{


printf("\tS\t M\t T\t W\t T\t F\t S\n");


for(iLoop=1;iLoop%26lt;=iNo_Of_Days;iLoo...


{





if(iWeek==1)


{


if(iLoop==1 || iLoop==8 || iLoop==15 || iLoop==22 || iLoop==29)


{


printf("\n");


printf("\t%d",iLoop);


}


else


{


printf("\t%d",iLoop);


}


}


else if(iWeek==2)


{


if(iLoop==1 || iLoop==7 || iLoop==14 || iLoop==21 || iLoop==28)


{


printf("\n");


if(iLoop==1)


printf("\t");


printf("\t%d",iLoop);


}


else


{


printf("\t%d",iLoop);


}


}





else if(iWeek==3)


{


if(iLoop==1 || iLoop==6 || iLoop==13 || iLoop==20 || iLoop==27)


{


printf("\n");


if(iLoop==1)


printf("\t\t");


printf("\t%d",iLoop);


}


else


{


printf("\t%d",iLoop);


}


}


else if(iWeek==4)


{


if(iLoop==1 || iLoop==5 || iLoop==12 || iLoop==19 || iLoop==26)


{


printf("\n");


if(iLoop==1)


printf("\t\t\t");


printf("\t%d",iLoop);


}


else


{


printf("\t%d",iLoop);


}


}





else if(iWeek==5)


{


if(iLoop==1 || iLoop==4 || iLoop==11 || iLoop==18 || iLoop==25)


{


printf("\n");


if(iLoop==1)


printf("\t\t\t\t");


printf("\t%d",iLoop);


}


else


{


printf("\t%d",iLoop);


}


}





else if(iWeek==6)


{


if(iLoop==1 || iLoop==3 || iLoop==10 || iLoop==17 || iLoop==24 || iLoop==31)


{


printf("\n");


if(iLoop==1)


printf("\t\t\t\t\t");


printf("\t%d",iLoop);


}


else


{


printf("\t%d",iLoop);


}


}


else if(iWeek==7)


{


if(iLoop==1 || iLoop==2 || iLoop==9 || iLoop==16 || iLoop==23 || iLoop==31)


{


printf("\n");


if(iLoop==1)


printf("\t\t\t\t\t\t");


printf("\t%d",iLoop);


}


else


{


printf("\t%d",iLoop);


}


}


}


}


else


{


printf("You have entered an invalid value for starting day of the week");


printf("\nValid values are 1 2 3 4 5 6 7 ");


}


}


else


{


printf("You have entered wrong number of days\n");


printf("Valid inputs are 28 29 30 31");


}


getch();


return(0);





}
Reply:So you have not been listening in class then.





You have two options.





Give up now and fail the course


or


Break the problem down into its parts.





First the user enter a number of days for the month.


You have to check that this is valid.


If not valid then ask for the days again. This could well be a loop. You do not know how many times the user will get this wrong so it will NOT be a for loop.





Now you want the starting day of the week.


You have to check that this is valid too.


If not valid then ask for the day of the week again. This will also be a loop. It will probably be the same loop as the previous input.


Now print the calendar. Well you know the numbers involved in this so this will probably be a for loop.


You may need a nested for loop too.





That will be enough to get you started.
Reply:Hmm well, you could do 1 of 2 things.





1) Use a nested loop with a counter value of the number of days in the first, and the second loop would have the 7 days of the week display before producing an "endl" statement.





2) Use an array and display a new line at every 7th day. That is if you have gotten into arrays of course.


No comments:

Post a Comment