Name this program day.c – This program gets two numbers from the command line, a
ID: 3671303 • Letter: N
Question
Name this program day.c – This program gets two numbers from the command line, as shown below. ./a.out 2 22 The first number represents the month and the second number represents the day. For example, 2 22 represents February 22. Your program should print out the day-of-the-year for this date. Since this is 2016, it is a leap year (February has 29 days). For this task, you will always be given two legal input values. Check your answers at http://mistupid.com/calendar/dayofyear.htm if you want.
I tried this but got nothing close to what I should have
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* months[12] = {"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
int dayInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main(int argc, char* argv[])
{
if (argc != 3)
{
printf("Usaage: ./YearDay month day ");
return 1;
}
//Variable to store day no. in a year
int dayNum = 0;
//variables to store command line inputs
char* month = argv[1];
int day = atoi(argv[2]);
int i;
for (i = 0; i < 12; i++)
{
//adds all day if month not inputted month
if (strcmp(month, months[i]) != 0)
{
dayNum += dayInMonth[i];
}
//adds no. of days inputted otherwise
else
{
dayNum += day;
break;
}
}
printf("%d ", dayNum);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.