Hi I need some help on my homework assignment and would appreciate any help that
ID: 3642070 • Letter: H
Question
Hi I need some help on my homework assignment and would appreciate any help that you can give me. I need some help on the how an array would look for this assignment. I am still pretty new to programming, so explain in the simplest way that you can. Thanks!The following is the homework assignment:
Create a program that counts the number of elapsed days from Jan 1, 2012 to a date in the year 2012 specified by the user. Program output should be similar to the following:
This program calculates the number of days from Jan 1, 2012 to a date in the year 2012 that you specify.
Please enter a month (1
Explanation / Answer
I have commented each part of the program that I felt needed explanation
Remember, don't just copy what I wrote up here, use it as a guide and make sure you can write the entire program from scratch!
#include <iostream>
#include <string>
using namespace std;
int main() {
//This creates an array of the amount of days in each month, months[0] is 31 because there are 31 days in January
//Note that there are 29 days in months[1] because 2012 is a leap year and thus there is an extra day in February
int months[] = {31,29,31,30,31,30,31,31,30,31,30,31};
//This creates an array of the name of each month
//It has the same amount of elements as the array "months"
string names[] = {"January","February","March","April","May","June","July","August","September","October","November", "December"};
//Create an int for the month and ask the user to input the value
int month;
cout << "Please enter a month (1 to 12): ";
cin >> month;
//A loop that continues until the user inputs a valid month
//If they entered a valid value the first time then this loop never starts)
while(month < 1 || month > 12) {
cout << month << " is not a valid entry for the month. Please enter a value between 1 and 12: ";
cin >> month;
}
int day;
cout << "Please enter a day of the month: ";
cin >> day;
//A loop that continues until the user inputs a valid day
//If they entered a valid value the first time then this loop never starts)
//Notice that because we already know what month they are using by this point
//We can make sure the day number is valid for that particular month
//You access months[month - 1] rather than months[month] because for example
//January which is month 1 is actually at the index 0 in the array
//This is because the first element of an array is actually at months[0], not months[1]
while(day < 1 || day > months[month - 1]) {
cout << day << " is not a valid day in the month of " << names[month-1] << "." << endl;
cout << "Please enter a value between 1 and " << months[month-1] << ": ";
cin >> day;
}
//Create an int to keep track of the amount of days
int days = 0;
//For each month that has passed, access the number of days in that month
//and add it to the total number of days
//For example, if January and February have passed entirely
//because your date is somewhere in February
//this for loop will add 31 and 29 to the total number of days
for(int i = 0; i < month - 1; ++i) {
days += months[i];
}
//This final addition also adds the amount of days that has passed in
//the user's particular month
days += (day - 1);
cout << days << " days elapsed between January 1, 2012 and " << names[month - 1] << " " << day << ", 2012." << endl;
system("pause");
}