//*****************************************************************
// R. A. Hillyard
// arrayAdd.cpp
// March 2001
//
// Get up to MAXVAL integers from the user (sentinel = 0).
// Store them in an array and then add all the even elements(0,2,4..)
// and all the odd elements(1,3,5...) and print the result
//*****************************************************************
#include <iostream>
using namespace std;
void initialize(int a[], int size); //set each element = 0
const int MAXVAL = 20;
int main()
{
int numVals; //counter for number of entries
int value[MAXVAL]; //declare the array of doubles
int evens = 0; //variable for even sum
int odds = 0; //variable for odd sum
//print message to user
cout << "Enter up to " << MAXVAL << " integers (one per line)\n";
cout << "Type 0 to terminate\n";
//get input from user
for(numVals = 0; numVals < MAXVAL; numVals++)
{
cin >> value[numVals];
if(value[numVals] == 0)
break;
}//endfor
//calculate totals
for(int i = 0; i < numVals; i += 2)
evens += value[i];
for(int i = 1; i < numVals; i += 2)
odds += value[i];
cout << "evens: " << evens << " odds: " << odds << endl;
return 0;
}
//*****************************************************************
void initialize(int a[], int size)
{
for(int i = 0; i < size; i++)
a[i] = 0;
}
//*****************************************************************
/******************************Program Output************************/
Enter up to 20 integers (one per line)
Type 0 to terminate
5
3
4
5
6
7
8
9
9
8
7
6
0
evens: 39 odds: 38