#include
using namespace std;
/*
* This program was used to help introduce the topic of arrays.
*
* @creator gdt
* @created 02016.10.25
* @edu The code for this program was written on the
* board during a csc100 class at SCC.
*/
int main(int, char**) {
// specification: store 5 ages that will be
// initialized with user entered inputs...
/*
int a1;
int a2;
int a3;
int a4;
int a5;
cout << "enter age 1: ";
cin >> a1;
cout << "enter age 2: ";
cin >> a2;
cout << "enter age 3: ";
cin >> a3;
cout << "enter age 4: ";
cin >> a4;
cout << "enter age 5: ";
cin >> a5;
// short cut...
// cout << "enter 5 ages: ";
//cin >> a1 >> a2 >> a3 >> a4 >> a5;
*/
// What if there were 1000 ages?
// What if there were a billion ages?
// Thank goodness for the array data structure
// and the fact that C and C++ have builtin
// support for the array data structure.
int ages[5]; // define an array to store 5 int values
// initialize the ages array with values (i.e. ages)...
ages[0] = 18;
ages[1] = 21;
ages[2] = 19;
ages[3] = 24;
ages[4] = 19;
// print the ages array starting with element 0...
cout << "hard-coded ages: ";
for (int i = 0; i < 5; i++)
cout << ages[i] << " "; // [] is the array operator
cout << endl;
// calculate and print the average age...
int sum = 0;
for (int i = 0; i < 5; i++)
sum += ages[i];
cout << "average age is " << (double)sum / 5 << endl;
// find and print the youngest and oldest ages...
int youngest = 100000;
int oldest = -1;
for (int x = 0; x < 5; x++) {
if (ages[x] < youngest)
youngest = ages[x];
if (ages[x] > oldest)
oldest = ages[x];
}
cout << "youngest is " << youngest << " and "
<< "oldest is " << oldest << endl;
// re-initialize the array with user inputs...
cout << endl;
for (int i = 0; i < 5; i++) {
cout << "enter age #" << i+1 << ": ";
cin >> ages[i];
}
// print the new array contents...
cout << "user initialized array: ";
for (int n = 0; n < 5; n++)
cout << ages[n] << " ";
cout << endl;
// define a new array using initialization list...
double math_constants[] = { 3.14, 1.618, 2.718 };
cout << "\nmath constants: "
<< "pi = " << math_constants[0] << "; "
<< "phi = " << math_constants[1] << "; "
<< "e = " << math_constants[2] << endl;
// sort math_constants from smallest-to-largest...
double dtmp = math_constants[2];
math_constants[2] = math_constants[0];
math_constants[0] = math_constants[1];
math_constants[1] = dtmp;
cout << "math constants sorted: "
<< "phi = " << math_constants[0] << "; "
<< "e = " << math_constants[1] << "; "
<< "pi = " << math_constants[2] << endl;
// next class: change the length of the ages array from
// 5 to 10 and then define a constant variable that
// stores the array length; swaps some elements;
// break the program up into functions...
return 0; // to be continued...
}