-
October 24th, 2014, 11:23 AM
#1
c++ weird error
[CODE]guys i'm having a weird error coming when i compile this program
it's a program to fill an array of 100 integers with random grades for 100 students in the range of 40-100, then calculate the average of the grades and the number of grades higher and lower than the average
any help please?
Code:
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(0));
int list[100];
double sum=0, average , GradesBelow=0, GradesAbove=0;
for( int j=1; j<101; j++)
{
int i = rand()%61 + 40;
list[j]=i;
sum += list[j];
}
average = sum/100;
for( int j=1; j < 101; j++)
{
int i = rand()%61 + 40;
list[j] = i;
if ( list[i] < average )
{
GradesBelow++;
}
if ( list[i] > average )
{
GradesAbove++;
}
}
cout<< " The class average is : "<<average<<endl;
cout<< " There are "<< GradesBelow << " grades below the average. "<<endl;
cout<< " Also, there are "<< GradesAbove << " grades above the average. "<<endl;
return 0;
}
-
October 24th, 2014, 11:51 AM
#2
Re: c++ weird error
1) You should describe the error .. compile time ? run time ?
2) you are accessing invalid indices in your array ...
Code:
int list[100];
for( int j=1; j<101; j++)
{
int i = rand()%61 + 40;
list[j]=i;
sum += list[j];
}
The valid indices for list are 0 to 99 , not 1 to 100
-
October 24th, 2014, 11:56 AM
#3
Re: c++ weird error
thank you so much; it's working now
turns out i need to fix the boundaries of j.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|