the program gives me an infinite loop

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void loadScores( int [], int & );
double average( int [], int );
double deviation(int [], double, int );
void frequency( int [], int[], int, double, double);
void sortScores(int [], int );
void displayArray( int [], double [], int, int [], double, double );
void comp( double [], double, double );

int main ()
{
   int x[100];
   double compe[4];
   double sum = 0;
   int  amount = 0;
   int Gfrequency[] = {0, 0, 0, 0, 0};
   double averageG;
   double total; 
   double std;
   
   loadScores( x, amount );
   
   averageG = average( x, amount );
   
   std = deviation(x, averageG, amount );

   sortScores(x, amount );

   comp(compe, averageG, std);

   displayArray(x, compe, amount, Gfrequency, averageG, std );

   return 0;
   
}

void displayArray( int x[], double compe[], int size , int Gfrequency[], double average, double std)
{
   int i = 0;
   int j = 0;
   cout<<"Grade Distribution"<<endl;
   cout<<"=================="<<endl;
   cout<<"A: " <<endl;

   while( x[i] >= compe[j] )
   {
      cout<< x[i] << "  ";
      i++;
      Gfrequency[j]++;
   }
   j++;
   cout<< "B: " <<endl;

   while( x[i] >= compe[j] )
   {
      cout<< x[i] << "  ";
      i++;
      Gfrequency[j]++;
   }
   j++;
   cout<< "C: " <<endl;
   
   while(x[i] >= compe[j] )
   {
      cout<< x[i] << "  ";
      Gfrequency[j]++;
   }
   j++;
   cout<< "D: " <<endl;

   while ( x[i] >= compe[j] )
   {
      cout<< x[i] << "  ";
      i++;
      Gfrequency[j]++;
   }
   j++;
   cout<< "F: " <<endl;

   while (i < size)
   {
      cout<< x[i] << "  ";
      i++;
      Gfrequency[j]++;
   }
   cout<< "==================" <<endl;
   cout<< "Grade Frequency" <<endl;
   cout<< "===============" <<endl;
   cout<< "A: "<< Gfrequency[0] <<endl;
   cout<< "B: "<< Gfrequency[1] <<endl;
   cout<< "C: "<< Gfrequency[2] <<endl;
   cout<< "D: "<< Gfrequency[3] <<endl;
   cout<< "F: "<< Gfrequency[4] <<endl;
   cout<< "===============" <<endl;
   cout<< "n = " << size <<endl;
   cout<< "Class Average: " <<average<<endl;
   cout<< "Class STD: " << std <<endl;
}

void loadScores( int x[], int &size )
{
   size = 0;   
   
   while(!cin.eof())   
   {
      cin>> x[size];
      size++;
   }

}


void sortScores( int x[], int size )
{
   for( int i = 0; i < size - 1; i++ )
   {
      for ( int j = 0; j < size - 1; j++ )
      {
         int temp;
         if ( x[j] < x[j + 1] )
         {
            temp = x[j];
            x[j] = x[j + 1];
            x[j + 1] = temp;
         }
      }
   }
}

double average( int x[], int size)
{
   double sum = 0;
   double avg;
   for ( int i = 0; i < size; i++ )
   {
      sum += x[i];
   }

   avg = sum / size;
   return avg;
}

double deviation( int x[], double average, int n )
{
   double total = 0;
   double std;

   for (int i = 0; i < n; i++ )
   {
      total += (x[i] - average)*(x[i] - average);
   }
   total /= n;
   return sqrt(total);
}

void comp( double compe[], double average, double std)
{
   compe[0] = average + 2 * std;
   compe[1] = average + std;
   compe[2] = average - std;
   compe[3] = average - 2 * std;
}

>>the program gives me an infinite loop
where ? If you don't know where then add some print statements or use your compiler's debugger.

And your bubble sort algorithm is wrong because it goes through too may iterations and comparisons.

void sortScores( int x[], int size )
{
   for( int i = 0; i < size - 1; i++ )
   {
      for ( int j = i+1; j < size; j++ )
      {
         int temp;
         if ( x[ i ] < x[ j ] )
         {
            temp = x[ i ];
            x[ i ] = x[ j ];
            x[ j ] = temp;
         }
      }
   }
}

the loop is in the while statement the program will compile and run if i the loadScore up but then it wont load the array

i changed the loadScore to this

void loadScores( int x[], int &size )
{
int i = 0;

while(!cin.eof())
{
cin>> x;
i++;
}

}
and now the output is

Grade Distribution
==================
A:
B:
C:
D:
F:
==================
Grade Frequency
===============
A: 0
B: 0
C: 0
D: 0
F: 0
===============
n = 0
Class Average: nan
Class STD: nan

The infinite loop is the one that starts on line 68 because the i is never incremented.

thanks i cant belive i missed that but the output still isnt working

Grade Distribution
==================
A:
B:
C:
93 82 78 92 76 93 74 84 73 98 83 8840236 72 67 66 65 64 54 38 35 29 24 D:
F:
==================
Grade Frequency
===============
A: 0
B: 0
C: 22
D: 0
F: 0
===============
n = 22
Class Average: 401894
Class STD: 1.8414e+06

i cant seem to figure this one out i dont now where the logic error is

It isn't working because you changed that function wrong. It was correct the first time. After that you need to add checks that loops don't check beyone the value of amount because that is how many valid items are in the array.

What values are in the compe array?

This is what the program looks like now, it is still giving me bad output. What function where you talking about?

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void loadScores( int [], int & );
double average( int [], int );
double deviation(int [], double, int );
void frequency( int [], int[], int, double, double);
void sortScores(int [], int );
void displayArray( int [], double [], int, int [], double, double );
void comp( double [], double, double );

int main ()
{
   int x[100];
   double compe[4] = {0,0,0,0};
   double sum = 0;
   int  amount = 0;
   int Gfrequency[] = {0, 0, 0, 0, 0};
   double averageG;
   double total; 
   double std;
   
   loadScores( x, amount );
   
   averageG = average( x, amount );
   
   std = deviation(x, averageG, amount );

   sortScores(x, amount );

   comp(compe, averageG, std);

   displayArray(x, compe, amount, Gfrequency, averageG, std );

   return 0;
   
}

void displayArray( int x[], double compe[], int size , int Gfrequency[], double average, double std)
{
   int i = 0;
   int j = 0;
   cout<<"Grade Distribution"<<endl;
   cout<<"=================="<<endl;
   cout<<"A: " <<endl;

   while( x[i] >= compe[j] )
   {
      cout<< x[i] << "  ";
      i++;
      Gfrequency[j]++;
   }
   j++;
   cout<< "B: " <<endl;

   while( x[i] >= compe[j] )
   {
      cout<< x[i] << "  ";
      i++;
      Gfrequency[j]++;
   }
   j++;
   cout<< "C: " <<endl;
   
   while(x[i] >= compe[j] )
   {
      cout<< x[i] << "  ";
      i++;
      Gfrequency[j]++;
   }
   j++;
   cout<< "D: " <<endl;

   while ( x[i] >= compe[j] )
   {
      cout<< x[i] << "  ";
      i++;
      Gfrequency[j]++;
   }
   j++;
   cout<< "F: " <<endl;

   while (i < size)
   {
      cout<< x[i] << "  ";
      i++;
      Gfrequency[j]++;
   }
   cout<< "==================" <<endl;
   cout<< "Grade Frequency" <<endl;
   cout<< "===============" <<endl;
   cout<< "A: "<< Gfrequency[0] <<endl;
   cout<< "B: "<< Gfrequency[1] <<endl;
   cout<< "C: "<< Gfrequency[2] <<endl;
   cout<< "D: "<< Gfrequency[3] <<endl;
   cout<< "F: "<< Gfrequency[4] <<endl;
   cout<< "===============" <<endl;
   cout<< "n = " << size <<endl;
   cout<< "Class Average: " <<average<<endl;
   cout<< "Class STD: " << std <<endl;
}

void loadScores( int x[], int &size )
{
   size = 0;   
   
   while(!cin.eof())   
   {
      cin>> x[size];
      size++;
   }

}


void sortScores( int x[], int size )
{
   for( int i = 0; i < size - 1; i++ )
   {
      for ( int j = 0; j < size - 1; j++ )
      {
         int temp;
         if ( x[j] < x[j + 1] )
         {
            temp = x[j];
            x[j] = x[j + 1];
            x[j + 1] = temp;
         }
      }
   }
}

double average( int x[], int size)
{
   double sum = 0;
   double avg;
   for ( int i = 0; i < size; i++ )
   {
      sum += x[i];
   }

   avg = sum / size;
   return avg;
}

double deviation( int x[], double average, int n )
{
   double total = 0;
   double std;

   for (int i = 0; i < n; i++ )
   {
      total += (x[i] - average)*(x[i] - average);
   }
   total /= n;
   return sqrt(total);
}

void comp( double compe[], double average, double std)
{
   compe[0] = average + 2 * std;
   compe[1] = average + std;
   compe[2] = average - std;
   compe[3] = average - 2 * std;
}

This is the output I get. It is obvious you are failing to check for maximum quantity of numbers entered at the keyboard.

10
20
50
60
^Z
Grade Distribution
==================
A:
B:
C:
60 50 20 10 D:
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -8589934
60 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858
993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -8589934
60 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858
993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -8589934
60 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858
993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -8589934
60 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858
993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -8589934
60 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858
993460 -858993460 -858993460 -858993460 1245080 4272406 1 9256408 924983
2 -633697869 0 0 2147336192 3579545 0 0 1245184 0 1245020 142 124514
8 4264096 -630588893 0 1245088 4271965 1245100 1980315699 2147336192 12
45164 2002954685 2147336192 1232591 0 0 2147336192 0 0 0 1245112 0 -
1 2002750450 2003025963 0 0 0 4264346 2147336192 0 2020893505 32 1 1
2296 220 0 32 0 20 1 7 52 364 1 0 0 0 0 0 2 438759246 644 68
716 608 0 760826203 1324 50 1376 768 0 -214797894 2144 74 2220 798
0 852421325 3020 66 3088 822 0 944791496 3912 94 4008 872 0 F:
==================
Grade Frequency
===============
A: 0
B: 0
C: 4
D: 193
F: 0
===============
n = 5
Class Average: -1.71799e+008
Class STD: 3.43597e+008
Press any key to continue . . .

And if you initialize array x on line 17 to 0 you will not get all those crazy numbers. int x[100] = {0};

im using I/O with this data set

93
82
78
66
54
92
76
65
35
93
74
67
38
84
73
24
64
29
98
83
72

>>im using I/O with this data set
doesn't matter -- the program should work the same with any data set.

what do you mean by check the amount in the loop

what are code tags i dont understand why i got an infraction

>>what are code tags i dont understand why i got an infraction
Look very closely in that edit box -- there are grey words there that explain code tags. Also read the rules because they are explained there too. Finally read the Announcements at the top of the c++ board because this is the third place where they are explained. You got two infractions so far because you have failed to read any of the above mentioned threads.

>>what do you mean by check the amount in the loop
It means to verify that the loops do not exceed the number of entries you made. loadScores() (should) change the value of amount to be the number of integers you enter at the keyboard. Just use that value in other places, something like below which was extracted from function displayArray()

while( x[i] >= compe[j] && i < size && j < size)
   {
      cout<< x[i] << "  ";
      i++;
      Gfrequency[j]++;
   }
void loadScores( int x[], int &amount)
{
   amount = 0;

   cin>> x[amount];

   while(!cin.eof())
   {
      amount++;
      cin>> x[amount];

   }

}

i figured out the problem i jsut had to move the incrementer of [/amount]

void loadScores( int x[], int &amount)
{
   amount = 0;

   cin>> x[amount];

   while(!cin.eof())
   {
      amount++;
      cin>> x[amount];

   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.