//The following is a collection of array examples (most of
//which were presented in class) bundled together as if part of
//a single program. You might copy+paste part or all of this
//code into the compiler to watch it run or to use as a basis
//for your own arrays. Or you might just print it out.
#include <iostream.h>
void main()
{
int days_in_month[12]; //array to hold number of days in each month of year
//important: slots are numbered starting at 0!!
days_in_month[0]=31; //slot 0 for January; 31 days in January
days_in_month[1]=28; //slot 1 for February; 28 days in February
days_in_month[2]=31; //slot 2 for March; 31 days in March
days_in_month[11]=31; //slot 11 for December; 31 days in December
int daysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//example of shorthand notation for filling array at time of creation
double temperature[31];
//array to hold precise temperature for every day in January
char grades[25]; //array to hold letter grade for each student in class of 25
grades[0]='A';
grades[1]='C';
char Grades[5] = { 'A', 'B', 'A', 'F', 'C' }; //shorthand filling with chars
char name[100]; //array to hold someone's name
cin >> name; //allow user to type in name at keyboard; store in array
cout << name; //display what ever words the user typed in
char Name[75] = { 'B', 'o', 'b', '\0' };
//shorthand filling of character array that we intend to print using cout...
//don't forget the '\0' if you want the cout command below to work!!
cout << Name; //displays "Bob" on screen; '\0' tells cout when to stop
char NAME[75] = { "Bob" };
//even shorter shorthand!! '\0' is built into notation
cout << NAME << endl;
///////////////////////////////////////////////////////////////
//how to display days_in_month:
int n=0;
while (n<12) {
cout << days_in_month[n] << endl;
++n;
}
//another way to display NAME:
n=0;
while (NAME[n]!='\0') {
cout << NAME[n];
++n;
}
//filling grades, this time using for loop:
for (n=0; n<25; ++n)
{
cin >> grades[n];
}
///////////////////////////////////////////////////////////////
int boxscore[9][3]; //two-dimensional array for baseball game score
boxscore[0][0] = 2; //2 runs in the first inning
boxscore[0][1] = 4; //4 hits in the first inning
boxscore[0][2] = 1; //1 error in the first inning
const int RUNS=0;
const int HITS=1;
const int ERRORS=2; //or can just use: enum { RUNS, HITS, ERRORS };
boxscore[1][RUNS] = 4; //4 runs in the second inning
boxscore[1][HITS] = 6; //6 hits in the second inning
boxscore[2][ERRORS] = 2; //2 errors in the second inning
//shorthand filling of 2D array
int score[9][3] = { { 2,4,1}, {4,6,2}, {0,1,0}, {0,0,0}, {0,0,0}, {0,2,0},
{0,0,1}, {1,3,0}, {0,0,0} };
int game[2][9][3]; //3-dimensional array representing 2 teams, entire game
game[0][3][HITS] = 5; //team 0 had 5 hits in the 4th inning
game[1][7][RUNS] = 2; //team 1 scored 2 runs in the 8th inning
char dayNames[7][20] = { {"Monday"}, {"Tuesday"}, {"Wednesday"},
{"Thursday"}, {"Friday"}, {"Saturday"}, {"Sunday"} };
cout << dayNames[0]; //display "Monday" on screen
int day;
cin >> day; //enter a number from 0 to 6!!
cout << dayNames[day]; //display requested day name on the screen
//another way to store series of words (more efficient use of memory)
char* DayNames[7] = { {"Monday"}, {"Tuesday"}, {"Wednesday"},
{"Thursday"}, {"Friday"}, {"Saturday"}, {"Sunday"} };
//filling array without specifying number of slots (automatically done)
int DaysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
}