array exercises

This is a discussion on array exercises within the C++ Programming forums, part of the General Programming Boards category; I am doing some array exercises and I keep getting this debug error. Can anybody please give me any ideas ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Smile array exercises

    I am doing some array exercises and I keep getting this debug error. Can anybody please give me any ideas on what this means? The program compiles and outputs the array total, as it should.

    Here's the code:

    Code:
    //array initialization and sum output
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	int i = 0, sum[6];
    	int values[6] = {10,22,13,99,4,5};
    	for (i = 0; i < 6; i++ )
    	{
    		cout << values[i] << " " ;
    	}
    		sum[i] = values[0] + values[1] + values[2] + values[3] + values[4] + values[5];
    		cout << endl;
    		cout << "Total is: ";
    		cout << sum[i] << endl;
    }


    Code:
    array2.exe has triggered a breakpoint
    The program '[2924] array2.exe: Native' has exited with code 0 (0x0).
    It also gives an error of runtime check failure #2 - stack around the variable 'sum' was corrupted.

    Any ideas would be great... Thanks!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You only need one sum, not an array of them. As it is, when the loop ends i is 6, so you walk off the end of the array.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >		sum[i] = values[0] + values[1] + values[2] + values[3] + values[4] + values[5];
    Here i has the value 6 (because it's past the for-loop), and is outside the bounds of the array sum. sum really doesn't need to be an array, as all you store is one value. So you could change your code to:
    Code:
    //array initialization and sum output
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	int i = 0;
    	int values[6] = {10,22,13,99,4,5};
    
    	int sum = 0;
    	for (i = 0; i < 6; i++ )
    	{
    		cout << values[i] << " " ;
    		sum += values[i];
    	}
    	cout << endl;
    	cout << "Total is: " << sum << endl;
    }

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Smile

    Oh, yes.. I kind of missed that small detail, .. Thanks guys..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21