### Array input unusual loop

The problem I have is that when entering the same number for the 20 arrays then it loops unexpectedly asking for input, e.g. if I enter all 5's then it asks for the 6th number after the twentieth, any help would be great thanks!

code as follows
 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#include #include using namespace std; int mark[20]; int count; int highest = 0; int lowest = 1000; int sum = 0; int main() { do { count++; cout << "Enter mark " << count << endl; cin >> mark[count]; } while (count < 20); for(int i=1;i<=20;i++) { if (highest < mark[i]) highest=mark[i]; } cout << "The highest number is " << highest << endl; for(int x=1;x<=20;x++) { if (lowest > mark[x]) lowest=mark[x]; } cout << "The lowest number is " << lowest << endl; for(int i=1;i<=20;i++) { sum+=mark[i]; } cout << "The average grade is " << sum / 20 << endl << endl;; cout << mark[0]; cin >> sum; }``````
Last edited on
I do not see where there are
 the 20 arrays

 ``1234567`` `````` do { count++; cout << "Enter mark " << count << endl; cin >> mark[count]; } while (count < 20);``````

then it has two errors. The first is that you are not initializing the element of array with index 0 and you are initializing the element with index 20 that does not belong to the acceptable range of indexes for the array.

EDIT: I would rewrite the loop the following way

 ``123456`` `````` do { cout << "Enter mark " << count + 1 << endl; cin >> mark[count++]; } while (count < 20);``````
Last edited on
You could combine all the code into 1 for loop & print the results at the end. Start the for loop at zero, otherwise you miss the first element:

 ``123`` ``````for(int x=0;x<20;x++){ }``````

hope all goes well.

Edit: Division doesn't work well for ints - change them to double.

Declare the lop variable before the for loop so it stays in scope for calculations at the end.
Last edited on
Sorry there isn't 20 arrays but an array with 20 values if you understand. The problem is something to do with the do while loop because it when I enter the same value for each value of the array it loops back asking for more input when it should exit the loop as the count variable is not less than 20.
Last edited on
By the way why did you include header <string>?

And take into account that all other your loops are invalid. Also the algorithms of searching the highest and lowest elements are also invalid.
I think that the problem is that you are overwriting the memory occupied by count when you assign a value to mark[20];
I already showed how the correct loop could look.
If you have not understood the problem till now then I will mention that acceptable values of indexes for array mark declared as mark[20] are 0 - 19
Last edited on

I tried your loop but still loops back to enter number 6 when 5 is entered for every element of the array. Any ideas?

ignore that last post but now the highest number is shown as 20 when all array elements are 5 and the average is 6 which is incorrect.
 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include #include using namespace std; int mark[20]; int count = 0; int highest = 0; int lowest = 1000; int sum = 0; int main() { do { cout << "Enter mark " << count + 1 << endl; cin >> mark[count++]; } while (count < 20); for(int i=0;i<=20;i++) { if (highest < mark[i]) highest=mark[i]; } cout << "The highest number is " << highest << endl; for(int x=0;x<=20;x++) { if (lowest > mark[x]) lowest=mark[x]; } cout << "The lowest number is " << lowest << endl; for(int i=0;i<=20;i++) { sum+=mark[i]; } cout << "The average grade is " << sum / 20 << endl << endl;; cout << mark[0]; cin >> sum; }``````
Last edited on
like vlad was saying about the index from 0 to 19, all your

`for(int i=0;i<=20;i++)`
need to become

`for(int i=0;i<20;i++)`

as you are reading from one past the size of your array (element 20 is the 21st element of your array, which doesn't exist)
@moooce

Thanks, my fault, didn't look at my program properly before posting. All fixed now, thanks guys!
Did you get rid of all the extra loops? Could probably do the whole thing with 20-25 LOC.
Topic archived. No new replies allowed.