Using a bool function to search an array

This program allows you to enter in grades and calculate an average, min, and max, using arrays. The problem I am having is that we need to use a bool function to search for any grades of A (90+) value, and return a message if there are. It has to be a subprogram outside of main, which is the issue I am having.

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
#include <fstream> 
#include <iostream> 
using namespace std; 


#include <cstdlib>

// search for A grades
bool hasA(int* score, int SIZE, int i)
{  
  for (i = 0; i < SIZE; i++) 
  { 
    if (score[i] >= 90) 
    { 
      return true;
      cout << "At least one 'A' grade entered."; 
      break; 
    } // if
  } // for 
  return false;
}

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; // negative if a<b 
  if (a > b) return 1; // positive if a>b 
  return 0; // 0 for tie 
} // compare

double getAverage(int* score, int n)
{
  int sum = 0;
  double average = 0.0;
  
  for (int i = 0; i < n; i++)
    sum += score[i];
  
  average = double(sum) / n;
  return average;
}
  
int main() 
{ 
  cout << "Description: This program prompts the user to enter a variable" << endl;
  cout << " number of scores, displays the input, calculates average, displays" << endl;
  cout << " the minimum and maximum values, and notifies the user if any 'A'" << endl;
  cout << " scores were entered." << endl; 
  cout << endl;

  int SIZE; 

  // prompt user for size
  cout << "How many scores? ";
  cin >> SIZE; 
  cin.ignore(1000, 10);
 
  int* score = new int[SIZE]; 
  
  // prompt user for scores 
  int i; 
  for (i = 0; i < SIZE; i++) 
  { 
    cout << "Enter score " << (i + 1) << ": "; 
    cin >> score[i]; 
    cin.ignore(1000, 10); 
  } // for  

  // sort scores
  qsort(score, SIZE, sizeof(int), compare);
  
  // display scores
  for (i = 0; i < SIZE; i++) 
    cout << score[i] << ' '; 
  cout << endl; 

  // display min, max, and average
  cout << "Minimum: " << score[0] << endl;
  cout << "Maximum: " << score[SIZE-1] << endl;
  cout << "Average: " << getAverage(score, SIZE) << endl;
  cout << hasA(int* score, int SIZE, int i) << endl;

  cout << endl;

  delete [] score; 
  return 0; 
} // main  
closed account (zb0S216C)
chirrrs wrote:
1
2
3
4
5
6
7
8
9
// search for A grades
bool hasA(int* score, int SIZE, int i)
{  
  for (i = 0; i < SIZE; i++) 
  { 
    if (score[i] >= 90) 
    { 
      return true;
      cout << "At least one 'A' grade entered."; 

Here, the std::cout statement is never reached; and never will be. When return is encountered, the function ends there-and-then.

Wazzak
It's the way you call it. You need to drop the data type keyword before passing the parameter. Also, the third parameter is kind of messy. I would just drop that and initialize a new loop counter inside the function.

Edit: Also, you need to assign the return value to some variable in main.
Last edited on
Even when I have switched the order of lines 8 and 9, I still get that "the function does not take 0 arguments" error
So here's what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// search for A grades
bool hasA(int* score, int SIZE)
{  
  int i;
  for (i = 0; i < SIZE; i++) 
  { 
    if (score[i] >= 90) 
    { 
      cout << "At least one 'A' grade entered."; 
      return true;
      break; 
    } // if
  } // for 
  return false;
}


And for the call:
cout << hasA(int* score, int SIZE) << endl;

Also, not sure what you mean by "dropping the data type keyword before passing the parameter"
Also, I actually get syntax errors at the function call (int should be preceeded by ) and syntax error: ) in addition to the "hasA function does not take 0 arguments" error. Any ideas?
closed account (zb0S216C)
On line 82, you're calling hasA(), but the compiler thinks it's a function prototype. The call should resemble:

hasA(score, SIZE, i)

Wazzak
Last edited on
So that works, but somehow, it posts a number 1 at the end. I don't see where it comes from?!?

For example, if I say 4 grades, 88,94,86,92, it ends up saying this on the last line:

"At least one 'A' grade entered.1"

In C++ booleans, zero is generally false and anything not zero is considered true.

When your function returns true, it'll return 1. Otherwise, it'll return 0.

You need to check the return value and print out the statement.
1
2
3
4
if(hasA(score, size, i))
   cout << "A grade exists" << endl;
else
   cout << "No A grades exist" << endl;
Topic archived. No new replies allowed.