Click to See Complete Forum and Search --> : Help with array assignment


Jody
April 19th, 1999, 10:21 PM
I need help completing an array assignment. My program displays dates in different formats. It allows the user to enter a date 5 different times. I need to define arrays and store input values as they are entered. After the user has entered 5 dates, I want to display each in a column format showing the four converted strings in different columns. I've tried to write this, but don't fully understand arrays enough to successfully code it. Any help will be appreciated! THANKS TO EVERYONE FOR HELPING THOSE OF US WHO USE THIS FORUM FOR HELP!!


// // Program to display different date formats.
#include <stdio.h>
#include <string.h>
#define NUMBER 5 /* defining the number of dates accepted from the user */
/* date formats */
/* format scaned by user then passed to other dates */

int Date (int &month, int &day, int &year);
int Europe (int month, int day, int year);
int Julian (int month, int day, int year);
int Military (int month, int day, int year);
int Business (int month, int day, int year);

int main (void)
{
int month, day, year;
int count;
printf ("\tNOTE: You can enter up to five dates! ");
/* for loop that allows up to 5 enteries */
for (count = 1; count <= NUMBER ; count++)
{
Date (month, day, year);
/* program flow for four unique date outputs */
Europe (month, day, year);
Julian (month, day, year);
Military (month, day, year);
Business (month, day, year);
}

return 0;
}

int Date (int &month, int &day, int &year)
{
while (1)
{
printf ("\n\t *** Please enter a date using the following format: (mm/dd/yr) ***\n\n");
scanf ("%d/%d/%d" ,&month, &day, &year);
/* checking for valid entry of dates */
if ((month < 13) && (month > 0) && (day < 32) && (day > 0) && (year < 100) && (year > 0))
{
break;
}
else
{
printf ("Invalid Date Entered!\n");
printf ("Please try again using the following format: (mm/dd/yr)\\nn");

}
printf ("\nDate entered = %d/%d/%d\n\n", month, day, year);
}

return 0;
}

int Europe (int month, int day, int year) /* simple date conversion from scanf */
{
printf ("European format = %d/%d/%d\n", day, month, year);
return 0;
}
/* figure Julian date format */
int Julian (int month, int day, int year)
{
int days;
switch (month)
{
case 1:
days = day;
break;
case 2:
days = 31 + day;
break;
case 3:
days = 31 + 28 + day;
break;
case 4:
days = 31 + 28 + 31 + day;
break;
case 5:
days = 31 + 28 + 31 + 30 + day;
break;
case 6:
days = 31 + 28 + 31 + 30 + 31 + day;
break;
case 7:
days = 31 + 28 + 31 + 30 + 31 + 30 + day;
break;
case 8:
days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
break;
case 9:
days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day;
break;
case 10:
days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
break;
case 11:
days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
break;
case 12:
days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
break;
}

printf ("Julian format = %d", year); /* print output in the (yyjjj) format */
if (days > 0 && days < 10)
printf ("00%d", days);
if (days >= 10 && days < 100)
printf ("0%d", days);
if (days >= 100 && days <= 366)
printf ("%d", days);
printf ("\n");

return 0;
}
/* second switch for abreviated month */
int Military (int month, int day, int year)
{
char Month[15];
switch (month)
{
case 1:
strcpy(Month,"JAN");
break;
case 2:
strcpy(Month,"FEB");
break;
case 3:
strcpy(Month,"MAR");
break;
case 4:
strcpy(Month,"APR");
break;
case 5:
strcpy(Month,"MAY");
break;
case 6:
strcpy(Month,"JUN");
break;
case 7:
strcpy(Month,"JUL");
break;
case 8:
strcpy(Month,"AUG");
break;
case 9:
strcpy(Month,"SEP");
break;
case 10:
strcpy(Month,"OCT");
break;
case 11:
strcpy(Month,"NOV");
break;
case 12:
strcpy(Month,"DEC");
break;
}
printf ("Military format ="); /* print output for Military conversion */
if (day < 10)
printf (" 0%d-", day);
if (day >= 10)
printf (" %d-", day);
printf ("%s-", Month);
printf ("%d\n", year + 1900);

return 0;
}

/* third switch for full month */
int Business (int month, int day, int year)
{
char Month[15];
switch (month)
{
case 1:
strcpy(Month,"January");
break;
case 2:
strcpy(Month,"February");
break;
case 3:
strcpy(Month,"March");
break;
case 4:
strcpy(Month,"April");
break;
case 5:
strcpy(Month,"May");
break;
case 6:
strcpy(Month,"June");
break;
case 7:
strcpy(Month,"July");
break;
case 8:
strcpy(Month,"August");
break;
case 9:
strcpy(Month,"September");
break;
case 10:
strcpy(Month,"October");
break;
case 11:
strcpy(Month,"November");
break;
case 12:
strcpy(Month,"December");
break;
}
printf ("Business format = %d %s %d\n", day, Month, year + 1900);
return 0;
}

Paul McKenzie
April 20th, 1999, 04:15 AM
To give you an idea of how to use arrays, I'll attempt to optimize one of your functions, Military(). Here goes:

char *MonthName[12] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};

int Military (int month, int day, int year)
{
if ( month < 1 || month > 12 )
return -1; //Illegal month

printf ("Military format =");
/* print output for Military conversion */
if (day < 10)
printf (" 0%d-", day);
if (day >= 10)
printf (" %d-", day);
printf ("%s-", MonthName[month-1]);
printf ("%d\n", year + 1900);
return 0;
}

Note that I stored the month names in an array called MonthName. Note that this is array of char *, and each char* represents a string denoting the month name.

Also understand the way I printed the month name (MonthName[month-1]). I used the month that was passed in as an index into the MonthName[] array. The reason why I did month-1 is that C and C++ arrays start at 0 (not 1), so I had to subtract 1 to get to the correct name.

Do the same type of thing with the Business() and the other functions (if you're clever enough, you can do this with the Julian function also). This will help you get an understanding of what arrays are all about and how to use them.

Regards,

Paul McKenzie