Couple questions about arrays (sample program included)

Started by
2 comments, last by wilsocn 19 years, 6 months ago
#include <iostream>
using namespace std;

//prototype declarations
int sumArray(int integerArray[], int sizeOfFloatArray);
void displayArray(int integerArray[], int sizeOfFloatArray);


//begin main
int main() 
{
	cout << "This program sums the values entered by the user.\n";
	cout << "Terminate the loop by entering a negative number.\n";

	//Store numbers into an array
	int inputValues[128];
	int numberOfValues = 0;

	for (; numberOfValues < 128; numberOfValues++)
	{
		//fetch another number
		int integerValue;

		cout << "Enter the next number: ";
		cin >> integerValue;
		cout << endl;

		//Check to see if the number is negative
		if (integerValue < 0)
		{
			break;
		}

		//otherwise store the number in the array.
		inputValues[numberOfValues] = integerValue;
	}

	//now output the values and the sum of the values
	displayArray(inputValues, numberOfValues);
	cout << "The sum is " << sumArray(inputValues, numberOfValues) << endl;

	return 0;
}

//display array 
void displayArray(int integerArray[], int sizeOfArray)
{
	cout << "The value of the array is:\n";
	for (int i = 0; i < sizeOfArray; i++)
	{
		cout.width(3);
		cout << i << ": " << integerArray << endl;
	}
}

//sum array
int sumArray(int integerArray[], int sizeOfArray)
{
	int accumulator = 0;
	for (int i = 0; i < sizeOfArray; i++)
	{
		accumulator += integerArray;
	}

	return accumulator;
}
Well I am new to programming and this program has me a little confused. I am reading a book called C++ Weekend Crash Course. It isnt bad, but doesn't look to be as good as the C++ in 21 Days series. MY first question is on the variable "numberOfValues". It is initialized to zero. This value is then put into the for loop for incrementing. Is this value incremented right away because of the "numberOfValue++" or does it stay 0 until the loop has completed one loop? Second, after the break the function "sumArray(inputValues, numberOfValues)" is called. What does the variable "numberOfValues" represent? Lets say that I enetered 3 positive numbers before entering a negative to terminate the loop. Does the variable "numberOfValues" represent a 2 (0,1,2) or a 3 (1,2,3)? I hope that I didnt totally confuse anyone with this problem. If I have I would appreciate anyone who could maybe explain this program to me a little. Many thanks to all those who can help. ~Chad
Advertisement
Original post by wilsocn
#include <iostream>using namespace std;//prototype declarationsint sumArray(int integerArray[], int sizeOfFloatArray);void displayArray(int integerArray[], int sizeOfFloatArray);//begin mainint main() {	cout << "This program sums the values entered by the user.\n";	cout << "Terminate the loop by entering a negative number.\n";	//Store numbers into an array	int inputValues[128];	int numberOfValues = 0;	for (; numberOfValues < 128; numberOfValues++)	{		//fetch another number		int integerValue;		cout << "Enter the next number: ";		cin >> integerValue;		cout << endl;		//Check to see if the number is negative		if (integerValue < 0)		{			break;		}		//otherwise store the number in the array.		inputValues[numberOfValues] = integerValue;	}	//now output the values and the sum of the values	displayArray(inputValues, numberOfValues);	cout << "The sum is " << sumArray(inputValues, numberOfValues) << endl;	return 0;}//display array void displayArray(int integerArray[], int sizeOfArray){	cout << "The value of the array is:\n";	for (int i = 0; i < sizeOfArray; i++)	{		cout.width(3);		cout << i << ": " << integerArray << endl;	}}//sum arrayint sumArray(int integerArray[], int sizeOfArray){	int accumulator = 0;	for (int i = 0; i < sizeOfArray; i++)	{		accumulator += integerArray;	}	return accumulator;}




Well I am new to programming and this program has me a little confused. I am reading a book called C++ Weekend Crash Course. It isnt bad, but doesn't look to be as good as the C++ in 21 Days series.

MY first question is on the variable "numberOfValues". It is initialized to zero. This value is then put into the for loop for incrementing. Is this value incremented right away because of the "numberOfValue++" or does it stay 0 until the loop has completed one loop?

Second, after the break the function "sumArray(inputValues, numberOfValues)" is called. What does the variable "numberOfValues" represent? Lets say that I enetered 3 positive numbers before entering a negative to terminate the loop. Does the variable "numberOfValues" represent a 2 (0,1,2) or a 3 (1,2,3)?

I hope that I didnt totally confuse anyone with this problem. If I have I would appreciate anyone who could maybe explain this program to me a little.

Many thanks to all those who can help.


~Chad


numberOfValues is incremented after the body of the loop.
see:
for (initialization (1); test (2); increment(4) ) {    // body of function (3)}


the value of numberOfValues is the size of the array that is being passed. as you can see the function passes an array and also the size of the array (in this case, numberOfValues).

no confusion here [smile]

Beginner in Game Development?  Read here. And read here.

 

Quote:Is this value incremented right away because of the "numberOfValue++" or does it stay 0 until the loop has completed one loop?


In the for() loop, the first part of the for statement is executed once, on start; the second part is executed at the beginning of each loop and the loop exits if it doesn't return true; the third part is incremented everytime you complete a full execution of the body.

You could have found this out yourself. Assuming you're using a development environment with a decent debugger, just put a breakpoint on the for() loop line, as well as the line right before the last brace of the for loop. Then run the program, and step through each line, looking at the value of the variables you're interested in. You will see when they change.

Regarding what the number represents, the convention is that it represents the actual number (3); the indices in the array are 0, 1 and 2, so when you want to look at the numbers, you go from 0 (inclusive) up to but not including 3. This half-open interval is a very cornerstone of array handling in C/C++.
enum Bool { True, False, FileNotFound };
Thanks to both of you for the replies. This has helped me a great deal because those questions were key for me to be able to understand the program completely.

Thanks

This topic is closed to new replies.

Advertisement