help!! asap (dynamic arrays)

so I am writing a program to prompt the user how many values they would like to input then take those inputs and sort them low to high find average and finally use bool search to see if any of the inputs were equal to an 'A' grade 90 or above after finishing and compiling the program runs but after inputing all the values it just starts outputing there is an A value entered even if there is not and i have to use control c to quit the program please help

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

#include <iostream> 
using namespace std; 
  
#include <cstdlib>


int compare(const void* pa, const void* pb)
  {
    const int& a = *static_cast<const int*>(pa);
    const int& b = *static_cast<const int*>(pb);
     if (a < b) return -1;
     if (a > b) return 1;
     return 0; 
  }

  double getAverage(int* score, int n)
  {
    int sum = 0;
    int i = 0;
    for (i = 0; i < n; i++)
  
      sum += score[i];
      double average = double(sum) / n;
      return average;
   }


int main() 
{ 
  int size; 
  cout << "How many scores would you like to record?: ";
  cin >> size;
  cin.ignore(1000, 10); 
  int* score = new int[size]; 
  cout << endl;
  
  // read and save the scores 
  
  int i; 
  for (i = 0; i < size; i++) 
  { 
    cout << "Enter the scores [" << i << "]: ";
    cin >> score[i]; 
    cin.ignore(1000, 10); 
    cout << endl;
   
  } // for 
 
  //use qsort from cstdlib to sort the input values from low to high
  qsort(score, size, sizeof(int), compare);
  
  for (i = 0; i < size; i++) 
    cout << score[i] << ' '; 
    delete [] score; 
    cout << endl; 

  //to find max and min   
  int max = score[0];
  int min = score[0];
  for (i = 1; i < size; i++)
  {
    if (max < score[i]) max = score[i];
    if (min > score[i]) min = score[i];
  }
    cout << endl;
    cout << "Minimum = " << min << endl;
    cout << "Maximum = " << max << endl;

   cout << "Average = " << getAverage(score , size) << endl;
  
   bool found = false;
  while (true)
  {
    if (score[i] >= 90) found = true;
      cout << "At least one 'A' grade entered." << endl;
  }
    if (found)
      cout << "No 'A' grade entered." << endl;
  
  cout << endl;

  return 0; 
} // main




you were missing brackets around a for loop.. i dont think this completely solves your problem though

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream> 
using namespace std; 
  
#include <cstdlib>


int compare(const void* pa, const void* pb)
  {
    const int& a = *static_cast<const int*>(pa);
    const int& b = *static_cast<const int*>(pb);
     if (a < b) return -1;
     if (a > b) return 1;
     return 0; 
  }

  double getAverage(int* score, int n)
  {
    int sum = 0;
    int i = 0;
    for (i = 0; i < n; i++)
  
      sum += score[i];
      double average = double(sum) / n;
      return average;
   }


int main() 
{ 
  int size; 
  cout << "How many scores would you like to record?: ";
  cin >> size;
  cin.ignore(1000, 10); 
  int* score = new int[size]; 
  cout << endl;
  
  // read and save the scores 
  
  int i; 
  for (i = 0; i < size; i++) 
  { 
    cout << "Enter the scores [" << i << "]: ";
    cin >> score[i]; 
    cin.ignore(1000, 10); 
    cout << endl;
   
  } // for 
 
  //use qsort from cstdlib to sort the input values from low to high
  qsort(score, size, sizeof(int), compare);
  
  for (i = 0; i < size; i++) 
    {                  //these brackets were missing
    cout << score[i] << ' '; 
    delete [] score; 
    cout << endl; 
    }                 //these brackets were missing
  //to find max and min   
  int max = score[0];
  int min = score[0];
  for (i = 1; i < size; i++)
  {
    if (max < score[i]) max = score[i];
    if (min > score[i]) min = score[i];
  }
    cout << endl;
    cout << "Minimum = " << min << endl;
    cout << "Maximum = " << max << endl;

   cout << "Average = " << getAverage(score , size) << endl;
  
   bool found = false;
  while (true)
  {
    if (score[i] >= 90) found = true;
      cout << "At least one 'A' grade entered." << endl;
  }
    if (found)
      cout << "No 'A' grade entered." << endl;
  
  cout << endl;

  return 0; 
} // main 
now i can get it to compile but it does not cout the grade report at all?

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

#include <iostream> 
using namespace std; 
  
#include <cstdlib>


int compare(const void* pa, const void* pb)
  {
    const int& a = *static_cast<const int*>(pa);
    const int& b = *static_cast<const int*>(pb);
     if (a < b) return -1;
     if (a > b) return 1;
     return 0; 
  }

  double getAverage(int* score, int n)
  {
    int sum = 0;
    int i = 0;
    for (i = 0; i < n; i++)
  
      sum += score[i];
      double average = double(sum) / n;
      return average;
   }


int main() 
{ 

  int size; 
  cout << "How many scores would you like to record?: ";
  cin >> size;
  cin.ignore(1000, 10); 
  int* score = new int[size]; 
  cout << endl;
  
  // read and save the scores 
  
  int i; 
  for (i = 0; i < size; i++) 
  { 
    cout << "Enter the scores [" << i << "]: ";
    cin >> score[i]; 
    cin.ignore(1000, 10); 
    cout << endl;
   
  } // for 
 

  //use qsort from cstdlib to sort the input values from low to high
  qsort(score, size, sizeof(int), compare);
  
  for (i = 0; i < size; i++) 
    
      cout << score[i] << ' '; 
      delete [] score; 
      cout << endl; 
    
  //to find max and min   
  int max = score[0];
  int min = score[0];
  for (i = 1; i < size; i++)
  {
    if (max < score[i]) max = score[i];
    if (min > score[i]) min = score[i];
  }
    cout << endl;
    cout << "Minimum = " << min << endl;
    cout << "Maximum = " << max << endl;

   cout << "Average = " << getAverage(score , size) << endl;

   bool gradeA = false;
   for (i = 0; i < size; i++)
     {
       if (score[i] >= 90)
       {
         gradeA = true;
         cout << "At least one 'A' grade entered." << endl;
         break;
       }
         if (gradeA) 
         cout << "No 'A' grades entered." << endl;
         break;
        
 } 
  
  return 0; 
} // main 
yay! i fixed it..

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <cstdlib>

using namespace std;



int compare(const void* pa, const void* pb)
  {
    const int& a = *static_cast<const int*>(pa);
    const int& b = *static_cast<const int*>(pb);
     if (a < b) return -1;
     if (a > b) return 1;
     return 0;
  }

  double getAverage(int* score, int n)
  {
    int sum = 0;
    int i = 0;
    for (i = 0; i < n; i++)

      sum += score[i];
      double average = double(sum) / n;
      return average;
   }


int main()
{

  int size;
  cout << "How many scores would you like to record?: ";
  cin >> size;
  cin.ignore(1000, 10);
  int* score = new int[size];
  cout << endl;

  // read and save the scores

  int i;
  for (i = 0; i < size; i++)
  {
    cout << "Enter the scores [" << i << "]: ";
    cin >> score[i];
    cin.ignore(1000, 10);
    cout << endl;

  } // for


  //use qsort from cstdlib to sort the input values from low to high
  qsort(score, size, sizeof(int), compare);

  for (i = 0; i < size; i++)

      cout << score[i] << ' ';
      cout << endl;

  //to find max and min
  int max = score[0];
  int min = score[0];
  for (i = 1; i < size; i++)
  {
    if (max < score[i]) max = score[i];
    if (min > score[i]) min = score[i];
  }
    cout << endl;
    cout << "Minimum = " << min << endl;
    cout << "Maximum = " << max << endl;

   cout << "Average = " << getAverage(score , size) << endl;

   bool gradeA = false;
   for (i = 0; i < size; i++)
     {
       if (score[i] >= 90)
       {
         gradeA = true;
         cout << "At least one 'A' grade entered." << endl;
         break;
       }
         if (gradeA)
         cout << "No 'A' grades entered." << endl;
         break;

 }

  return 0;
} // main
the line 58:

delete [] score;

was unnecessary and deleted the array you needed =P your program was trying to find values that didnt exist haha

the code i posted above works as you intended /\
Last edited on
ok i took that an ran it but it still does not say "at least one 'A' grade entered or vice versa when numbers less than 90 or above are input? that is the only problem i am having with the whole entire code? cant figure out how to make it work?
there.. now it really works
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <cstdlib>

using namespace std;



int compare(const void* pa, const void* pb)
  {
    const int& a = *static_cast<const int*>(pa);
    const int& b = *static_cast<const int*>(pb);
     if (a < b) return -1;
     if (a > b) return 1;
     return 0;
  }

  double getAverage(int* score, int n)
  {
    int sum = 0;
    int i = 0;
    for (i = 0; i < n; i++)

      sum += score[i];
      double average = double(sum) / n;
      return average;
   }


int main()
{

  int size;
  cout << "How many scores would you like to record?: ";
  cin >> size;
  cin.ignore(1000, 10);
  int* score = new int[size];
  cout << endl;

  // read and save the scores

  int i;
  for (i = 0; i < size; i++)
  {
    cout << "Enter the scores [" << i << "]: ";
    cin >> score[i];
    cin.ignore(1000, 10);
    cout << endl;

  } // for


  //use qsort from cstdlib to sort the input values from low to high
  qsort(score, size, sizeof(int), compare);

  for (i = 0; i < size; i++)

      cout << score[i] << ' ';
      cout << endl;

  //to find max and min
  int max = score[0];
  int min = score[0];
  for (i = 1; i < size; i++)
  {
    if (max < score[i]) max = score[i];
    if (min > score[i]) min = score[i];
  }
    cout << endl;
    cout << "Minimum = " << min << endl;
    cout << "Maximum = " << max << endl;

   cout << "Average = " << getAverage(score , size) << endl;

   bool gradeA = false;
   for (i = 0; i < size; i++)
     {
       if (score[i] >= 90)
       {
         gradeA = true;
         cout << "At least one 'A' grade entered." << endl;
         break;
       }
         if (max<90)  // <-- fixed this
         {   // <-- you were missing brackets
         cout << "No 'A' grades entered." << endl;
         break;
         }   // <-- you were missing brackets

      }

  return 0;
}
Last edited on
and ive noticed from your rockpaperscissors game.. you need to watch your brackets. which is why indentation is very helpful
sorry i didnt have enough time before... this is how your program should look, atleast.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <cstdlib>

using namespace std;



int compare(const void* pa, const void* pb)
{
    const int& a = *static_cast<const int*>(pa);
    const int& b = *static_cast<const int*>(pb);
    if (a < b) return -1;
    if (a > b) return 1;
    return 0;
}

double getAverage(int* score, int n)
{
    int sum = 0;
    int i = 0;
    double average;
    for (i = 0; i < n; i++)
    {
        sum += score[i];
        average = double(sum) / n;
    }
    return average;
}


int main()
{

    int size;
    cout << "How many scores would you like to record?: ";
    cin >> size;
    cin.ignore(1000, 10);
    int* score = new int[size];
    cout << endl;

    // read and save the scores

    int i;
    for (i = 0; i < size; i++)
    {
        cout << "Enter the scores [" << i << "]: ";
        cin >> score[i];
        cin.ignore(1000, 10);
        cout << endl;

    } // for


    //use qsort from cstdlib to sort the input values from low to high
    qsort(score, size, sizeof(int), compare);

    for (i = 0; i < size; i++)
    {
        cout << score[i] << ' ';
        cout << endl;
    }
    //to find max and min
    int max = score[0];
    int min = score[0];
    for (i = 1; i < size; i++)
    {
        if (max < score[i]) max = score[i];
        if (min > score[i]) min = score[i];
    }
    cout << endl;
    cout << "Minimum = " << min << endl;
    cout << "Maximum = " << max << endl;

    cout << "Average = " << getAverage(score , size) << endl;

    bool gradeA = false;
    for (i = 0; i < size; i++)
    {
        if (score[i] >= 90)
        {
            gradeA = true;
            cout << "At least one 'A' grade entered." << endl;
            break;
        }
        if (max<90)  // <-- fixed this
        {   // <-- you were missing brackets
            cout << "No 'A' grades entered." << endl;
            break;
        }   // <-- you were missing brackets

    }
    return 0;
}
awesome thanks a lot for the help ya did notice my brackets are a little crazy but very helpful non the less
no problem man. pm me if you ever need anymore help
Topic archived. No new replies allowed.