Apr 15, 2013 at 11:08am Apr 15, 2013 at 11:08am UTC
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
#include <string>
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 Apr 15, 2013 at 11:25am Apr 15, 2013 at 11:25am UTC
Apr 15, 2013 at 11:29am Apr 15, 2013 at 11:29am UTC
I do not see where there are
in your program. If you are speaking about this loop
1 2 3 4 5 6 7
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
1 2 3 4 5 6
do
{
cout << "Enter mark " << count + 1 << endl;
cin >> mark[count++ ];
}
while (count < 20);
Last edited on Apr 15, 2013 at 11:35am Apr 15, 2013 at 11:35am UTC
Apr 15, 2013 at 11:32am Apr 15, 2013 at 11:32am UTC
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:
1 2 3
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 Apr 15, 2013 at 11:35am Apr 15, 2013 at 11:35am UTC
Apr 15, 2013 at 11:35am Apr 15, 2013 at 11:35am UTC
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 Apr 15, 2013 at 11:36am Apr 15, 2013 at 11:36am UTC
Apr 15, 2013 at 11:38am Apr 15, 2013 at 11:38am UTC
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.
Apr 15, 2013 at 11:41am Apr 15, 2013 at 11:41am UTC
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.
Apr 15, 2013 at 11:53am Apr 15, 2013 at 11:53am UTC
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 Apr 15, 2013 at 11:53am Apr 15, 2013 at 11:53am UTC
Apr 15, 2013 at 11:55am Apr 15, 2013 at 11:55am UTC
@ vlad from moscow
I tried your loop but still loops back to enter number 6 when 5 is entered for every element of the array. Any ideas?
Apr 15, 2013 at 11:59am Apr 15, 2013 at 11:59am UTC
@ vlad from moscow
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <iostream>
#include <string>
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 Apr 15, 2013 at 12:00pm Apr 15, 2013 at 12:00pm UTC
Apr 15, 2013 at 12:03pm Apr 15, 2013 at 12:03pm UTC
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)
Apr 15, 2013 at 12:05pm Apr 15, 2013 at 12:05pm UTC
@moooce
Thanks, my fault, didn't look at my program properly before posting. All fixed now, thanks guys!
Apr 15, 2013 at 1:33pm Apr 15, 2013 at 1:33pm UTC
Did you get rid of all the extra loops? Could probably do the whole thing with 20-25 LOC.