Error with an array

This is a discussion on Error with an array within the C++ Programming forums, part of the General Programming Boards category; I am currently taking a beginner C++ course. My teacher is extremely inept at this subject, as she herself does ...

  1. #1
    IRLeeb
    Join Date
    Feb 2005
    Location
    Amston, CT
    Posts
    7

    Question Error with an array

    I am currently taking a beginner C++ course. My teacher is extremely inept at this subject, as she herself does not know how to program in any modern language. She relies on 2 students who took the class last year to help us out, but, alas, no one is proficient in C++. We are now tackling arrays, in which I was instructed to construct this program:
    Code:
    /*	Len Bansavich III
    
      March 25th, 2005
      
    	This program lets the user decide the number of elements in an array, sorts the array into two groups (odd elements and even elements), and prints the 
    	elements by group
    */
    
    
    
    #include <iostream.h>
    #include <iomanip.h>
    #include <windows.h>
    #include <math.h>
    
    
    int main()	{
    	
    	// declaring variables
    	
    	int array[1000]; // declaring ARRAY array
    	int length; // declaring length of array variable
    	int i; // declaring incrimintation variable
    	
    	// querying user for number of elements in array
    	
    	do	{
    		cout << "Please enter the number of elements in the array." << endl;
    		cin >> length;
    	}
    	while(length < 0 && length > 1000);
    	
    	// querying user for elements of ARRAY array
    	
    	for (i = 1; i <= length; i++)	{
    		cout << "Please enter a value to be sorted." << endl;
    		cin >> array[i];
    	}
    	
    	// printing elements by type (odd and even elements)
    	
    	cout << "Odd Values" << endl;
    	
    	for ( i = 0; i <= length - 1; i++)	{
    		if (array[i]%2 != 0)	{
    			cout << array[i] << "  ";
    		}
    	}
    	
    	cout << endl << "Even Values" << endl;
    	
    	for (i = 0; i <= length - 1; i++)	{
    		if (array[i]%2 == 0)	{
    			cout << array[i] << "  ";
    		}
    	}
    	
    	return 0;
    }
    
    /*	Runs:	Please enter the number of elements in the array.
    			4
    			Please enter a value to be sorted.
    			1
    			Please enter a value to be sorted.
    			2
    			Please enter a value to be sorted.
    			3
    			Please enter a value to be sorted.
    			4
    			Odd Values
    			1  3
    			Even Values
    			-858993460  2  Press any key to continue
    */

    Please notice on the run, how I recieve -858993460 as a number. This happens no matter the length of the array, or the numbers entered, and always appears in the same place. Please help me. Thank you.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    for (i = 1; i <= length; i++) {
        cout << "Please enter a value to be sorted." << endl;
        cin >> array[i];
    }
    	
    // printing elements by type (odd and even elements)
    	
    cout << "Odd Values" << endl;
    
    for ( i = 0; i <= length - 1; i++) {
        if (array[i]%2 != 0) {
            cout << array[i] << "  ";
        }
    }
    	
    cout << endl << "Even Values" << endl;
    	
    for (i = 0; i <= length - 1; i++) {
        if (array[i]%2 == 0) {
            cout << array[i] << "  ";
        }
    }
    When reading in and storing your values, you are inputting into array locations 1-4. When outputting your even/odd values, you are reading from locations 0-3. I suggest the following:

    Code:
    for (i = 0; i < length; i++) {
        cout << "Please enter a value to be sorted." << endl;
        cin >> array[i];
    }
    	
    // printing elements by type (odd and even elements)
    	
    cout << "Odd Values" << endl;
    
    for ( i = 0; i < length; i++) {
        if (array[i]%2 != 0) {
            cout << array[i] << "  ";
        }
    }
    	
    cout << endl << "Even Values" << endl;
    	
    for (i = 0; i < length; i++) {
        if (array[i]%2 == 0) {
            cout << array[i] << "  ";
        }
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    IRLeeb
    Join Date
    Feb 2005
    Location
    Amston, CT
    Posts
    7
    Thank you very much. By the way, the Heckler & Koch MP5-K PDW is a very fine weapon.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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