Using Visual Studio in C++..........I need help writing this 2 Dimensional Array
ID: 3691667 • Letter: U
Question
Using Visual Studio in C++..........I need help writing this 2 Dimensional Array Program
In this program, you will store a calendar month of dates (numbers starting at 1) in a two-dimensional array. Some months have 31 days, others 30, and February has 28 (29 if it’s a leap year, which is a year divisible by 4).
Here is a list of Months, the number of Days in each month, and the Day on which each Month Starts, in 2016, 2017, 2018, and 2019:
Day on which Month Starts
Months Days 2016 2017 2018 2019
January 31 Friday Sunday Monday Tuesday
February 28* Monday Wednesday Thursday Friday
March 31 Tuesday Wednesday Thursday Friday
April 30 Friday Saturday Sunday Monday
May 31 Sunday Monday Tuesday Wednesday
June 30 Wednesday Thursday Friday Saturday
July 31 Friday Saturday Sunday Monday
August 31 Monday Tuesday Wednesday Thursday
September 30 Thursday Friday Saturday Sunday
October 31 Saturday Sunday Monday Tuesday
November 30 Tuesday Wednesday Thursday Friday
December 31 Thursday Friday Saturday Sunday
*except leap year
Given a month name, the name of the starting day of the week, and the year, write a program to store the date (number) of each day in the corresponding array location. Use a two-dimensional array called month of type int to store the generated month dates.
Declare month to be an array of type int of the proper size and dimensions (weeks and days), and initialize all elements to 0.
Handle user input as follows:
Ask the user for the month name, and match it to the appropriate number of days (hint: use parallel arrays)
If the month is February, you need to know the year, and test if it is a leap year to adjust February’s number of days, if needed.
Ask the user which day of the week the month starts on, and match it to the proper day number (Sunday being day 1), to determine which position in the two-dimensional array to start storing dates (hint: use parallel arrays)
Use formatted output to display the month left-aligned, omitting “0” dates (see the output example below)
Use functions if desired. Allow your main program to loop, allowing the user to ask for an unlimited number of months.
Program should look like this:
Explanation / Answer
Programme::
#include<stdio.h>
#define TRUE 1
#define FALSE 0
int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int inputyear(void)
{
int year;
printf("Please enter a year (example: 1999) : ");
scanf("%d", &year);
return year;
}
int inputmonth(void)
{
int month;
printf("Please enter month (example: 12) : ");
scanf("%d", &month);
return month;
}
int determinedaycode(int year)
{
int daycode;
int d1, d2, d3;
d1 = (year - 1.)/ 4.0;
d2 = (year - 1.)/ 100.;
d3 = (year - 1.)/ 400.;
daycode = (year + d1 - d2 + d3) %7;
return daycode;
}
int determineleapyear(int year)
{
if(year% 4 == FALSE && year%100 != FALSE || year%400 == FALSE)
{
days_in_month[2] = 29;
return TRUE;
}
else
{
days_in_month[2] = 28;
return FALSE;
}
}
void calendar(int year, int month, int daycode)
{
int day;
printf(" Sun Mon Tue Wed Thu Fri Sat " );
// Correct the position for the first date
for ( day = 1; day <= 1 + daycode * 5; day++ )
{
printf(" ");
}
// Print all the dates for one month
for ( day = 1; day <= days_in_month[month]; day++ )
{
printf("%2d", day );
// Is day before Sat? Else start next line Sun.
if ( ( day + daycode ) % 7 > 0 )
printf(" " );
else
printf(" " );
}
// Set position for next month
daycode = ( daycode + days_in_month[month] ) % 7;
}
int main(void)
{
int year, month, daycode, leapyear;
year = inputyear();
month = inputmonth();
daycode = determinedaycode(year);
determineleapyear(year);
calendar(year, month, daycode);
printf(" ");
}
Output:
Please enter a year (example: 1999) : 2016
Please enter month (example: 12) : 4
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.