9 Replies - 1082 Views - Last Post: 23 October 2009 - 12:10 AM Rate Topic: -----

#1 heckyaaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 07-October 09

Calendar in C

Posted 22 October 2009 - 01:04 AM

So there aren't any issues with the code itself.

Was there a more efficient way to write this is my question. Where can I make it better coding.

The object of the program is to read in the start day for the beginnging of the month, how many days are in the month and output a calendar looking model.
#include <stdio.h>
#include <stdlib.h>

int main(void)
{

	int days;
	int date;
	int day;
	char cont;


	do{
	printf("\nCS133 Lab4 Track 2: Printing a calendar\n");
	printf("Enter number of days in month:");
	scanf("%d", &days);

	printf("Enter starting day of the week (1=Sun, 7=Sat):");
	scanf("%d", &day);

	printf("\n\n");



	for (date = 1; date <= days; date++)



	switch (day){


	 case 1 : if (date == 7)
			printf("%4d\n", date);
			 else if (date == 14)
			printf("%4d\n", date);
			 else if (date == 21)
			printf("%4d\n", date);
			 else if (date == 28)
			printf("%4d\n", date);
			 else
			printf("%4d", date);
			break;


	 case 2 :if (date == 1)
			printf("	");
			if (date == 6)
			printf("%4d\n", date);
			 else if (date == 13)
			printf("%4d\n", date);
			 else if (date == 20)
			printf("%4d\n", date);
			 else if (date == 27)
			printf("%4d\n", date);
			 else
			printf("%4d", date);
			break;

	case 3 :if (date == 1)
			printf("		");
			if (date == 5)
			printf("%4d\n", date);
			 else if (date == 12)
			printf("%4d\n", date);
			 else if (date == 19)
			printf("%4d\n", date);
			 else if (date == 26)
			printf("%4d\n", date);
			 else
			printf("%4d", date);
			break;

	case 4 :if (date == 1)
			printf("			");
			if (date == 4)
			printf("%4d\n", date);
			 else if (date == 11)
			printf("%4d\n", date);
			 else if (date == 18)
			printf("%4d\n", date);
			 else if (date == 25)
			printf("%4d\n", date);
			 else
			printf("%4d", date);
			break;

	case 5 :if (date == 1)
			printf("				");
			if (date == 3)
			printf("%4d\n", date);
			 else if (date == 10)
			printf("%4d\n", date);
			 else if (date == 17)
			printf("%4d\n", date);
			 else if (date == 24)
			printf("%4d\n", date);
			 else
			printf("%4d", date);
			break;

	case 6 :if (date == 1)
			printf("					");
			if (date == 2)
			printf("%4d\n", date);
			 else if (date == 9)
			printf("%4d\n", date);
			 else if (date == 16)
			printf("%4d\n", date);
			 else if (date == 23)
			printf("%4d\n", date);
			 else if (date == 30)
			printf("%4d\n", date);
			 else
			printf("%4d", date);
			break;

	case 7 :if (date == 1)
			printf("						%4d\n", date);
			 else if (date == 8)
			printf("%4d\n", date);
			 else if (date == 15)
			printf("%4d\n", date);
			 else if (date == 22)
			printf("%4d\n", date);
			 else if (date == 29)
			printf("%4d\n", date);
			 else
			printf("%4d", date);
			break;


	default : date = days;
			printf("Dont be stupid\n");

}

	printf("\n\nWant to do it again? (y/n): ");
	scanf("%s", &cont);


	}while ( cont == 'y' || cont == 'Y');

	printf("\nGoodbye!");

	return 0;
}


This post has been edited by heckyaaa: 22 October 2009 - 01:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Calendar in C

#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Calendar in C

Posted 22 October 2009 - 02:03 AM

There is indeed a much "cleaner" way, and that's to avoid using switch ... case.
  • Display the "offset" for the first line (it depends on your day variable), so that your calendar starts on the right day,
  • For each date, Print the date,
  • If current date+day-1 modulo 7 is 0, insert a line break.
Hope I've been clear enough...

This post has been edited by EdwinNameless: 22 October 2009 - 02:03 AM

Was This Post Helpful? 0
  • +
  • -

#3 heckyaaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 07-October 09

Re: Calendar in C

Posted 22 October 2009 - 10:28 AM

I'm not sure I understand fully what the last person was trying to say, probably because when this was written it was bright and early.

So to ask again, I understand I can output the spacing for the start of the calendar through a simple printf statement, then use 'if' statements to formulate the organization? Or how would I go about doing that.
Was This Post Helpful? 0
  • +
  • -

#4 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Calendar in C

Posted 22 October 2009 - 11:12 AM

View Postheckyaaa, on 22 Oct, 2009 - 09:28 AM, said:

I'm not sure I understand fully what the last person was trying to say, probably because when this was written it was bright and early.

So to ask again, I understand I can output the spacing for the start of the calendar through a simple printf statement, then use 'if' statements to formulate the organization? Or how would I go about doing that.


Yes. You would have 2 loops.

The first one who print the spaces to create the shift. For example, if you start on Wed (4), you have to print spaces from 1 to 3.

Then, the second loop prints out the days, from 1 to days, inserting a newline character every time the condition ((date+day-1) % 7 == 0) is met (meaninga week has been filled).

Does it make more sense now?
Was This Post Helpful? 0
  • +
  • -

#5 heckyaaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 07-October 09

Re: Calendar in C

Posted 22 October 2009 - 11:18 AM

So this means I would use for loops? or would I be making an if statement followed by an else statement.

Quote

Yes. You would have 2 loops.

The first one who print the spaces to create the shift. For example, if you start on Wed (4), you have to print spaces from 1 to 3.

Then, the second loop prints out the days, from 1 to days, inserting a newline character every time the condition ((date+day-1) % 7 == 0) is met (meaninga week has been filled).

Does it make more sense now?

Was This Post Helpful? 0
  • +
  • -

#6 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Calendar in C

Posted 22 October 2009 - 11:29 AM

View Postheckyaaa, on 22 Oct, 2009 - 10:18 AM, said:

So this means I would use for loops? or would I be making an if statement followed by an else statement.


You would use 2 for loops, and inside the second one, as there is a condition, you'll have a if.
Was This Post Helpful? 1
  • +
  • -

#7 heckyaaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 07-October 09

Re: Calendar in C

Posted 22 October 2009 - 12:02 PM

I'm really struggling here. Not sure how to construct them, I understand or believe I understand I need to construct 2 for loops, one similar to this
for (startd = 0; startd < date; startd++)
	{printf(" ");}



And the other I believe to be started out like this
for ((date+day-1) % 7 == 0)
	{
	}


Not sure how to do exactly what you are saying for the second loop.


Quote

You would use 2 for loops, and inside the second one, as there is a condition, you'll have a if.

This post has been edited by heckyaaa: 22 October 2009 - 12:03 PM

Was This Post Helpful? 0
  • +
  • -

#8 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Calendar in C

Posted 22 October 2009 - 12:57 PM

[quote name='heckyaaa' date='22 Oct, 2009 - 11:02 AM' post='807554']
Not sure how to do exactly what you are saying for the second loop.
[quote]

The second one would look like this:

// Print all the dates from 1 to days.
	  for (date = 1; date <= days; date++) {
	printf("%4d", date);
// If We've filled the week, print a newline.
	if ((date+day-1)%7 == 0) {
	  printf("\n");
	}
	  }

Was This Post Helpful? 0
  • +
  • -

#9 heckyaaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 07-October 09

Re: Calendar in C

Posted 22 October 2009 - 02:08 PM

Now need help with formatting, it will print out the calendar, however I can't figure out how to get the correct alignment of the dates...

#include <stdio.h>
#include <stdlib.h>

int main(void)
{

	int days;	  /* Input holder for number of days in month */
	int date;	  /* Input holder for date */
	int startd;	/* Input holder for starting day of the week */
	char cont;	 /* Constant used for continuation of program */
	
				  /* Prompt to find what the number of days in the month are */
	do
	{
	printf("CS133 Lab4 Track 2: Printing a calendar\n");
	printf("Enter number of days in month:");
	scanf("%d", &days);
				  /* Prompt and find start day of week */
	printf("Enter starting day of the week (1=Sun, 7=Sat):");
	scanf("%d", &startd);

	printf("\n\n");
				  /* Print blank spaces, enough for starting day of week -1 */
	for (date = 0; date < days; date++)
	{printf("");}
	
				  /* Print all the dates from 1 to days. */
	for (date = 1; date <= days; date++) 
	{
	printf("%4d", date);
				  /* If We've filled the week, print a newline. */
	if ((date+days-1)%7 == 0) 
	  {
	  printf("\n");
	  }
	}

		printf("\n\nWant to do it again? (y/n): ");
		scanf("%s", &cont);
	}
	
	while ( cont == 'y' || cont == 'Y');
	printf("\nGoodbye!");

	
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#10 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Calendar in C

Posted 23 October 2009 - 12:10 AM

View Postheckyaaa, on 22 Oct, 2009 - 01:08 PM, said:

Now need help with formatting, it will print out the calendar, however I can't figure out how to get the correct alignment of the dates...

	/* Print blank spaces, enough for starting day of week -1 */
	for (date = 0; date < days; date++)
	{printf("");}




The problem is here: you are printing an empty string. You have to print spaces. I'll let you find out how many you need.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1