Calendar Table using Turbo C how?

Input the first day and the total days of the month

Page 1 of 1

9 Replies - 5669 Views - Last Post: 10 November 2010 - 10:23 AM Rate Topic: -----

#1 Civirol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-November 10

Calendar Table using Turbo C how?

Posted 10 November 2010 - 06:46 AM

The user is asked to input the number of days in a month and the first day of the month. Here is the sample output:
Input the number of days in a month: 30
input the first day of the month: 4
0-Sunday
1-Monday
2-Tuesday
3-Wednesday
4-Thursday
5-Friday
6-Saturday

Sun Mon Tues Wed Thurs 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

This is my sample code. I enter 1-6 in the first day of the month and it works...but everytime I enter O in the first day of the month, the alignment is lost. Help!!! i tried to fix it several times but i can't!!!
#include<stdio.h>
#define p printf
#define s scanf
int TD,D,x,y=1,E;
main()
{
clrscr();
p("Input Number of days in a month:");
s("%d",&TD);
p("Input the first day of the month:");
s("%d",&D);
p("Sun\tMon\tTues\tWed\tThurs\tFri\tSat\n");
for(x=1;x<=42;x++)
{
if(x<=D)
{
printf("\t");
}
else if(y<=TD)
{
E=7-D;
if(y%7==D)
{
printf("%d\n",y);
}
else
{
printf("%d\t",y);
}
y++;
}}
getch();
}


This post has been edited by macosxnerd101: 10 November 2010 - 06:51 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Calendar Table using Turbo C how?

#2 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Calendar Table using Turbo C how?

Posted 10 November 2010 - 08:50 AM

Your provided code dose not work for any case (0-6). I had to change if(y%7==D) to if(y%7==E) to make it work as you described. Your main problem is this:
E=7-D;
if(y%7==E)



With a zero value for D, E is 7, thus y%7 will never be equal to 7 as % only returns the remainder. So you gotta make an E value of 7 into 0 before you hit the if statement.
Was This Post Helpful? 1
  • +
  • -

#3 Civirol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-November 10

Re: Calendar Table using Turbo C how?

Posted 10 November 2010 - 09:13 AM

View Postbrds, on 10 November 2010 - 07:50 AM, said:

Your provided code dose not work for any case (0-6). I had to change if(y%7==D) to if(y%7==E) to make it work as you described. Your main problem is this:
E=7-D;
if(y%7==E)



With a zero value for D, E is 7, thus y%7 will never be equal to 7 as % only returns the remainder. So you gotta make an E value of 7 into 0 before you hit the if statement.


Do I need to change my formula or equate E to 0? Or do I need to make another for loop before the if function?

This post has been edited by Civirol: 10 November 2010 - 09:15 AM

Was This Post Helpful? 0
  • +
  • -

#4 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Calendar Table using Turbo C how?

Posted 10 November 2010 - 09:19 AM

A simple if statement will suffice, or you can get fancy with E %= 7; after the E = 7 - D;.
E = 7 - D;
E %= 7; /* Set E to the remainder of E/7 */



@Edit: typo.

This post has been edited by brds: 10 November 2010 - 09:23 AM

Was This Post Helpful? 1
  • +
  • -

#5 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Calendar Table using Turbo C how?

Posted 10 November 2010 - 09:25 AM

Or do it in one line
E = (7 - D) % 7;


Was This Post Helpful? 1
  • +
  • -

#6 Civirol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-November 10

Re: Calendar Table using Turbo C how?

Posted 10 November 2010 - 09:44 AM

View Postbrds, on 10 November 2010 - 08:19 AM, said:

A simple if statement will suffice, or you can get fancy with E %= 7; after the E = 7 - D;.
E = 7 - D;
E %= 7; /* Set E to the remainder of E/7 */



@Edit: typo.


Thank you so much for the information!!! It finally works!!! You're great!!!
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • Joined: 16-October 07

Re: Calendar Table using Turbo C how?

Posted 10 November 2010 - 09:52 AM

View PostCivirol, on 10 November 2010 - 07:46 AM, said:

#define p printf
#define s scanf



This level of lazy boggles the mind.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Calendar Table using Turbo C how?

Posted 10 November 2010 - 09:58 AM

Typical Turbo C code.
Was This Post Helpful? 0
  • +
  • -

#9 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Calendar Table using Turbo C how?

Posted 10 November 2010 - 10:14 AM

baavgai++
JackOfAllTrades++

Some day we will live in a world without Turbo C, and we will all have are very own flying pigs.
Was This Post Helpful? 0
  • +
  • -

#10 Civirol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-November 10

Re: Calendar Table using Turbo C how?

Posted 10 November 2010 - 10:23 AM

Thank you so much! I'm just a new student taking computer science, so i have difficulty in simple c problems. thanks!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1