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.