Code:
//ArrayDemo - demonstrate the use of arrays
// by reading a sequence of integers
// and then displaying them in order
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
//prototype declarations
int sumArray(int integerArray[], int sizeOffloatArray);
void displayArray(int integerArray[], int sizeOfloatArray);
int main(int nNumberofArgs, char* pszArgs[])
{
//input the loop count
[COLOR=Blue]int nAccumulator = 0;[/COLOR]
cout << "This program sums values entered "
<< "by the user\n";
cout << "Terminate the loop by entering "
<< "a negative number\n";
cout << endl;
//store numbers into an array
int inputValues[128];
int numberOfValues;
for(numberOfValues = 0;
numberOfValues < 128;
numberOfValues++)
{
//fetch another number
int integerValue;
cout << "Enter next number: ";
cin >> integerValue;
//if it's negative...
if (integerValue < 0)
{
//...then exit
break;
}
//...otherwise sotre the number
//into the storage array
inputValues[numberOfValues] = integerValue;
}
//now output the values and the sum of the values
displayArray([B]inputValues, numberOfValues[/B]);
cout << "The sum is "
<< sumArray([B]inputValues, numberOfValues[/B])
<< endl;
//wait until user is ready before terminating program
//to allow the user to see the program results
system("PAUSE");
return 0;
}
//displayArray - display the members of an
// array of length sizeOffloatArray
void displayArray([B]int integerArray[], int sizeOfArray[/B])
{
cout << "The value of the array is:" << endl;
for (int i = 0; i < sizeOfArray; i++)
{
cout.width(3);
cout << i << ": " << integerArray[i] << endl;
}
cout << endl;
}
//sumArray - return the sum of the members of an
// integer array
int sumArray([B]int integerArray[], int sizeOfArray[/B])
{
int accumulator = 0;
for (int i = 0; i < sizeOfArray; i++)
{
accumulator [COLOR=Red]+=[/COLOR] integerArray[i];
}
return accumulator;
}
I had a few questions about a sample program in a book about C++ I was reading (the one in my avatar). I had everything in the program pretty much figured out except for two things (and maybe a third). This is a program trying to explain how to use arrays and my questions aren't answered in the book. The first question: why at the beginning of main() do they define nAccumulator (blue) as 0 and then never again refer to it in the program? Question two: why do the arguments sent to the two functions defined at the bottom change their names(bolded)? And finally, question three: what does that += (red) symbol do in sumArray()? The book makes no mention of any of these things when explaining the program. This book is extremely confusing so much thanks to anyone who can help out.