CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: c++ weird error

  1. #1
    Join Date
    Sep 2014
    Posts
    15

    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;
    }

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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

  3. #3
    Join Date
    Sep 2014
    Posts
    15

    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
  •  





Click Here to Expand Forum to Full Width

Featured