The program below is executing fine but when I enter the last of 10
numbers and the final output(shows the average of the 10 numbers) is displayed,
I get the following error message:

Run-Time Check Failure #2 - Stack around the variable 'numbers' was
corrupted.

If someone could explain what this error message is for, I would appreciate it.

Thanks
FWD

Code:

#include "stdafx.h"
#include <iostream>

using namespace std ;

double calculateAvg(double sumNum, int numCount); // calculateAvg's Prototype

int main()
{
		
	/****************************************/
	/*Explanation of what the program does. */
	/****************************************/
	cout << "This program will prompt you 10 times to enter a number. " << endl;
	cout << "After you have entered the last number the program will " << endl;
	cout << "divide the sum of the numbers you entered by 10. " << endl;
	cout << "The program will then display the result of that calculation " << endl;
	cout << "as the average of those 10 numbers." << endl << endl;
	
	/*********************************************************/
	/* Declaration and Initialization of Variables and Array */
	/*********************************************************/
	int const nSize = 9;	// integer constant to hold the size of array numbers[]
	double numbers [nSize];	// Declaration of the array numbers[] to hold 10 values entered by user.
	
	int i;			// Variable i declared to act as counter in the for l	                        
	double sum;		// Variable sum declared to hold a running sum of numbers entered.
	sum = 0;		// Initializing sum to a value of 0.

	
	/****************************************************************/
	/* The following for-loop is used to prompt the user for input,
	   store the users input into the Array numbers[nSize], 
	   and keep a running sum of the numbers entered into the array.*/
	/****************************************************************/
	for (i=1; i<=nSize+1; i++)
	{
		
		/*****************************************************************/
		/* Selection statement to determine the prompt given to the user.*/
		/*****************************************************************/
		if (i == 1)					  
		{
			cout << i << ".  Enter the first positive-number and press Enter:  ";
			cin >> numbers[i - 1];
			sum = sum + numbers[i - 1];
			cout << endl;
		}
		else if (i == 10)
		{
			cout << i << ". Enter the last positive-number and press Enter:   ";
			cin >> numbers[i - 1];
			sum = sum + numbers[i - 1];
			cout << endl << endl;
		}
		else
		{
			cout << i << ".  Enter the next positive-number then press Enter:  ";
			cin >> numbers[i - 1];
			sum = sum + numbers[i - 1];
			cout << endl;
		}
	}

	/***************************************************************************************/
	/* Output statement which calls the function calculateAvg(double sumNum, int numCount)
	   with the parameters sum and (nSize + 1).  The output statement outputs the value
	   returned by the function, which is the avg of the 10 numbers the user entered.      */
	/***************************************************************************************/
	cout <<"The average of all ten numbers that you entered is:  ";
	cout << calculateAvg(sum, (nSize + 1)) << endl << endl;

	return 0;
}

/***********************************************************/
/* The following function computes the the Average of all 10    
   numbers entered by the user.           
   Inputs:                                
      sumNum (double), numCount (integer) 
                                          
   Output:                                
      avgNum (double)                                      */
/***********************************************************/
double calculateAvg(double sumNum, int numCount)  
{
	double avgNum;

	avgNum = sumNum / numCount;

	return(avgNum);

}

With an array, you're working with the offset. Offset zero is first, and offset nine is 9 removed from the initial value, or 10. Try

int const nSize = 10;

In your code, you specify numbers of length "9" (index "0" to index "8"). Then, you have a look that feeds data into the array "if nSize is less than or equal to length '10' (index '0' to index '9')". Therefore, your program will try to reference an index of the array that doesn't exist. This is what it should be something like (this is for 10 array values).

int const nSize = 10;
double numbers[nsize];

for (i=0; i<nSize; i++){
//do loop here
}

In your code, you specify numbers of length "9" (index "0" to index "8"). Then, you have a look that feeds data into the array "if nSize is less than or equal to length '10' (index '0' to index '9')". Therefore, your program will try to reference an index of the array that doesn't exist. This is what it should be something like (this is for 10 array values).

int const nSize = 10;
double numbers[nsize];

for (i=0; i<nSize; i++){
//do loop here
}

Ok here is what I changed and it worked. I don't have any clue why it worked though.

Changed: int const nSize = 9 to: int const nSize = 10
Changed: for (i=1; i<=nSize+1; i++) to: for (i=1; i<=nSize; i++)
Changed: cout << calculateAvg(sum, (nSize + 1)) << endl << endl; to: cout << calculateAvg(sum, nSize) << endl << endl;

The reason I set the counter i to 1 in the counter controlled loop is because it served as the counter as well as the list number for the user prompt. For Example:

1. Please enter the first number:
2. Please enter the next number:
.
.
.
10. Please enter the last number:

So 1, 2, all the way to 10 is actually the value of i during the counter controlled loop.
I used the counter to control the text of the prompt with the If Then Else statement. So it's purpose was two fold. Thats why I didn't want to initialize it to 0 in the For loop.

Anyway I still don't understand what was wrong.
Am I wrong to think that the Array numbers[9] will hold ten values?

#include "stdafx.h"
#include <iostream>

using namespace std ;

double calculateAvg(double sumNum, int numCount); // calculateAvg's Prototype

int main()
{
		
	/****************************************/
	/*Explanation of what the program does. */
	/****************************************/
	cout << "This program will prompt you 10 times to enter a number. " << endl;
	cout << "After you have entered the last number the program will " << endl;
	cout << "divide the sum of the numbers you entered by 10. " << endl;
	cout << "The program will then display the result of that calculation " << endl;
	cout << "as the average of those 10 numbers." << endl << endl;
	
	/*********************************************************/
	/* Declaration and Initialization of Variables and Array */
	/*********************************************************/
	int const nSize = 10;	// integer constant to hold the size of array numbers[]
	double numbers [nSize];	// Declaration of the array numbers[] to hold 10 values entered by user.
	
	int i;					// Variable i declared to act as counter in the for loop.	
	double sum;				// Variable sum declared to hold a running sum of numbers entered by user.
	sum = 0;				// Initializing sum to a value of 0.

	
	/****************************************************************/
	/* The following for-loop is used to prompt the user for input,
	   store the users input into the Array numbers[nSize], 
	   and keep a running sum of the numbers entered into the array.*/
	/****************************************************************/
	for (i=1; i<=nSize; i++)
	{
		
		/*****************************************************************/
		/* Selection statement to determine the prompt given to the user.*/
		/*****************************************************************/
		if (i == 1)					  
		{
			cout << i << ".  Enter the first positive-number and press Enter:  ";
			cin >> numbers[i - 1];
			sum = sum + numbers[i - 1];
			cout << endl;
		}
		else if (i == 10)
		{
			cout << i << ". Enter the last positive-number and press Enter:   ";
			cin >> numbers[i - 1];
			sum = sum + numbers[i - 1];
			cout << endl << endl;
		}
		else
		{
			cout << i << ".  Enter the next positive-number then press Enter:  ";
			cin >> numbers[i - 1];
			sum = sum + numbers[i - 1];
			cout << endl;
		}
	}

	/***************************************************************************************/
	/* Output statement which calls the function calculateAvg(double sumNum, int numCount)
	   with the parameters sum and (nSize + 1).  The output statement outputs the value
	   returned by the function, which is the avg of the 10 numbers the user entered.      */
	/***************************************************************************************/
	cout <<"The average of all ten numbers that you entered is:  ";
	cout << calculateAvg(sum, nSize) << endl << endl;

	return 0;
}

/***********************************************************/
/* The following function computes the the Average of all 10    
   numbers entered by the user.           
   Inputs:                                
      sumNum (double), numCount (integer) 
                                          
   Output:                                
      avgNum (double)                                      */
/***********************************************************/
double calculateAvg(double sumNum, int numCount)  
{
	double avgNum;

	avgNum = sumNum / numCount;

	return(avgNum);

}

Test Run Below:

This program will prompt you 10 times to enter a number.
After you have entered the last number the program will
divide the sum of the numbers you entered by 10.
The program will then display the result of that calculation
as the average of those 10 numbers.

1. Enter the first positive-number and press Enter: 100

2. Enter the next positive-number then press Enter: 500

3. Enter the next positive-number then press Enter: 600

4. Enter the next positive-number then press Enter: 300

5. Enter the next positive-number then press Enter: 888

6. Enter the next positive-number then press Enter: 600

7. Enter the next positive-number then press Enter: 699

8. Enter the next positive-number then press Enter: 845

9. Enter the next positive-number then press Enter: 898

10. Enter the last positive-number and press Enter: 258


The average of all ten numbers that you entered is: 568.8

Press any key to continue . . .

You're still blowing past your array. You need to stop 'playing' with the index. If you make a 10-element array, loop from 0 to 9, not 1 to 10. And don't add/subtract from the index.

You're still blowing past your array. You need to stop 'playing' with the index. If you make a 10-element array, loop from 0 to 9, not 1 to 10. And don't add/subtract from the index.

Hi Walt,

I don't think I'm blowing past my array after the changes I made above. All I did was change the array size from [9] to [10]. Now the counter loops from 1 to 10 and it also serves as the list numbers in the prompt to the user. The array is filled with 10 numbers and the calculation takes the sum of those numbers and divides it by 10.

My last post shows the coded changes I made to stop receiving the original Run-time error.

So should I change the index back to 9 and change the selection statements as follows?:

if (i==1) to: if (i==0)
if (i==10) to: if (i==9)

and then just change:

cout << i << ". Enter the first positive-number and press Enter: ";
to: cout << i + 1 << ". Enter the first positive-number and press Enter: ";

Thanks
FWD

as has been previously stated

int array[10]

contains 10 elements indexed from 0 to 9
if you attempt to read array[10] who knows what you will get.

commented: as has previously been stated, therefore we do not need it again. +0

as has been previously stated

int array[10]

contains 10 elements indexed from 0 to 9
if you attempt to read array[10] who knows what you will get.

So array[10] is actually 10 elements and not 11.

what do you mean "If you attempt to read array[10] who knows what you will get"?
Above that your say that array[10] is correct. I don't get it. It's write or wrong?

I'm going to go ahead and Mark this as solved. I don't want to drag this on. I'm not getting the error with the adjustments I made in the second block of code.

Thanks for your help.

FWD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.