help with code - global variables

This is a discussion on help with code - global variables within the C Programming forums, part of the General Programming Boards category; hi all, the foll code prints a calender for year between 1900 & 2100. i have used 3 global vars ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    26

    help with code - global variables

    hi all,
    the foll code prints a calender for year between 1900 & 2100. i have used 3 global vars - dayCount1, 2 & 3 to keep track of days. is there an alternative to using these global vars? any help will be appreciated. thanks in advance.

    Code:
    #include <stdio.h>
    
    #define TRUE		1
    #define FALSE		0
    #define MIN_YEAR	1900
    #define MAX_YEAR	2100
    #define START_DAY	2
    #define DAYS_IN_WEEK	7
    #define MONTHS_IN_YEAR	12
    
    
    int checkYear		(void);
    int isLeap		(int);
    int calcStartDayYear	(int);
    int printCalender	(int, int, int[], int, int);
    
    
    int dayCount1 = 0;
    int dayCount2 = 0;
    int dayCount3 = 0;
    
    
    
    int main()
    {
    	int days[] = {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};
    	int year = 0;
    	int leapResult = 0;
    	int startDay = 0;
    	int monthsInRow = 0;
    	int daysInRow  = 0;
    
    
    	printf("\nEnter a year between 1900 and 2100 : ");
    	year = checkYear();
    
    	leapResult = isLeap(year);
    	startDay = calcStartDayYear(year);
    
    	printf("\n\n\t\t\t\t\t%d\n\n", year);
    	for(monthsInRow = 0; monthsInRow <12; monthsInRow += 3)
    	{
    		switch(monthsInRow)
    		{
    			case 0:
    				dayCount1 = 0;
    				dayCount2 = 0;
    				dayCount3 = 0;
    
    				printf("\tJan\t \t\t\tFeb\t \t\t\tMar\n");
    				break;
    			case 3:
    				dayCount1 = 0;
    				dayCount2 = 0;
    				dayCount3 = 0;
    
    				printf("\tApr\t \t\t\tMay\t \t\t\tJun\n");
    				break;
    			case 6:
    				dayCount1 = 0;
    				dayCount2 = 0;
    				dayCount3 = 0;
    
    				printf("\tJul\t \t\t\tAug\t \t\t\tSep\n");
    				break;	
    			case 9:
    				dayCount1 = 0;
    				dayCount2 = 0;
    				dayCount3 = 0;
    
    				printf("\tOct\t \t\t\tNov\t \t\t\tDec\n");
    				break;
    		}
    		printf("  S  M Tu  W Th  F  S\t\t");
    		printf("  S  M Tu  W Th  F  S\t\t");
    		printf("  S  M Tu  W Th  F  S\n");
    		
    		for(daysInRow = 0; daysInRow < 6; daysInRow++)
    		{
    			startDay = printCalender(monthsInRow + 1, year, days, startDay, daysInRow);
    			startDay = printCalender(monthsInRow + 2, year, days, startDay, daysInRow);
    			startDay = printCalender(monthsInRow + 3, year, days, startDay, daysInRow);
    			printf("\n");
    		}
    		printf("\n");
    	}
    	printf("\n\n");
    	return 0;
    }
    
    int checkYear()
    {
    	int year = 0;
    	
    
    	scanf("%d", &year);
    	while( (year < MIN_YEAR) || (year > MAX_YEAR) )
    	{
    		printf("\nPlease enter year between 1900 and 2100 : ");
    		scanf("%d", &year);
    	}
    	return year;
    }
    
    int isLeap(int year)
    {
    	if( (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) )
    	{
    		return TRUE;
    	}
    	return FALSE;
    }
    
    int calcStartDayYear(int year)
    {
    	int yearCount = 0;
    	int startDayNextYear = START_DAY;
    	
    
    	for(yearCount = MIN_YEAR; yearCount < year; yearCount++)
    	{
    		if( isLeap(yearCount) )
    		{
    			startDayNextYear += 2;
    		}
    		else
    		{
    			startDayNextYear += 1;
    		}
    		if(startDayNextYear == 8)
    		{
    			startDayNextYear = 1;
    		}
    		else if(startDayNextYear == 9)
    		{
    			startDayNextYear = 2;
    		}
    	}
    	return startDayNextYear;
    }
    
    int printCalender(int month, int year, int days[], int startDay, int daysInRow)
    {
    	int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    	int dayOfWeek[] = {1, 2, 3, 4, 5, 6, 7};
    	int colCount = 0;
    	int index = 0;	
    
    
    	if(daysInRow == 0)
    	{
    		while(dayOfWeek[index] != startDay)
    		{
    			printf("   ");
    			index++;
    			colCount++;
    		}
    		if( (month == 2) && (isLeap(year)) )
    		{
    			daysInMonth[month] = 29;
    		}
    		startDay = (startDay + daysInMonth[month]) % DAYS_IN_WEEK;
    		if(startDay == 0)
    		{
    			startDay = 7;
    		}
    	}
    	while(colCount <= 6)
    	{	
    		if( (month == 1) || (month == 4) || (month == 7) || (month == 10) )
    		{
    			if(dayCount1 < daysInMonth[month])
    			{
    				printf(" %2d", days[dayCount1]);
    				dayCount1++;
    				colCount++;
    			}
    			else
    			{
    				while(colCount <= 6)
    				{
    					printf("   ");
    					colCount++;
    				}
    			}
    		}
    		else if( (month == 2) || (month == 5) || (month == 8) || (month == 11) )
    		{
    			if( (month == 2) && (isLeap(year)) )
    			{
    				daysInMonth[month] = 29;			
    			}
    			if(dayCount2 < daysInMonth[month])
    			{
    				printf(" %2d", days[dayCount2]);
    				dayCount2++;
    				colCount++;
    			}
    			else
    			{
    				while(colCount <= 6)
    				{
    					printf("   ");
    					colCount++;
    				}
    			}
    		}
    		else
    		{
    			if(dayCount3 < daysInMonth[month])
    			{
    				printf(" %2d", days[dayCount3]);
    				dayCount3++;
    				colCount++;
    			}
    			else
    			{
    				while(colCount <= 6)
    				{
    					printf("   ");
    					colCount++;
    				}
    			}
    		}
    	}
    	printf("\t\t");
    
    	return startDay;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You could try implementing the variables as being static within the printCalender() function, or you could pass them as parameters to it. Or maybe you could change printCalender() to print out 3 months in one go, without the need to call it 6 times.

    As with most things in programming, there are plenty of ways to do the same thing, choose one you are comfortable with.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    26
    i used static variables in printCalender() but couldn't re-initialize them for the next 3 months & so on... abt calling printCalender() just once for 3 months, sld i change the code so that i print 6 rows inside the function? thanks.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i used static variables in printCalender() but couldn't re-initialize them for the next 3 months & so on...
    I suppose you could set the back to zero if you saw a change in one of the incoming parameters, say for example, if startDay was 0.

    >>abt calling printCalender() just once for 3 months, sld i change the code so that i print 6 rows inside the function?
    Yes, that it was I meant. Obviously, with the two ideas given here, you need to implement only one.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    26

    Thumbs up

    thank you so much. i implemented the second maethod u told me. it works just great!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global variables
    By shadovv in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2005, 03:21 PM
  2. Use global variables or pointers?
    By RealityFusion in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2005, 09:47 PM
  3. Global Variables - Mutex
    By cjschw in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2003, 07:50 AM
  4. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  5. GLobal variables
    By fuh in forum C++ Programming
    Replies: 21
    Last Post: 01-01-2003, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21