Display the percentage of numbers above average

Im having trouble displaying what percentage of numbers are above-average. Very new to this, any help is appreciated.

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
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

int main ()
{
cout<<" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
<<endl
<<" The purpose of this program is to prompt the user to \n "
<<"enter a number of salaries, then display the average salary, all\n "
<<"salaries above the average, and the percentage of salaries \n"
<< " above the average.\n ";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";




const int SIZE = 30; 
double salary = 0, total = 0, average = 0, percent, aboveaverage;
double salaries[SIZE];
int i = 0, counter = 0, high=0, count;
cout << endl;

cout<<setprecision(2)<<fixed<<showpoint;

while ((salary != -1) && (counter < SIZE)) {
cout << "Please enter a salary, -1 to finish entering:  ";
cin >> salary;
if (salary != -1) {
salaries[counter] = salary;
counter++;
} 
}

if (counter == 0)
cout << "\nNo salaries were entered!\n\n";

else {

// Total
for (i = 0; i < counter; i++)
total += salaries[i];
// Compute and display average of grades
cout << "\n\n Average of  entered:\n";
cout<<setprecision(2)<<fixed<<showpoint;
average = total /counter;
cout << " $" << average;

cout<< "\n\n Above average entered: \n";

for(int i = 0; i < counter; i++) //above average
    if (salaries[i] > average)
    aboveaverage = salaries[i];
    cout<< "  $"<<aboveaverage << "\n";
    cout<<endl;
    

    percent =  aboveaverage / counter * 100.00;
   
   
    cout<<setprecision(2)<<fixed<<showpoint;
	cout << "\nPercentage of salaries above average: \n" << "  " <<percent << "% \n";
    cout<<endl;
	cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}



}
you have absolutely no {} on your above average section.
perhaps like this
1
2
3
4
5
6
7
for()
{
  if()
  {
    cout;
  }
}


honestly I find it best practice to ALWAYS put {} on condition, loop, and similar statements even if they only do 1 line of code. Then you KNOW you did it right, and if you change it later, it is STILL right (add a second line, for example).

and on top of that aboveaverage is a value, not a counter.
you need to count the ones above average if you want a %.
that is, if 5 items are above av, you want 5/total
but what you have is
somearraylocation[whatever]/total
Last edited on
Possibly something like this:

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
#include <iostream>

int main()
{
  int n_xs = 0;            // what number of exes do we have?
  int const max_xs = 10;   // what is the maximum number of exes we're allowed to have? 
  int xs[max_xs];  

  double sum_xs = 0.0; 
  for (; n_xs < max_xs && std::cin >> xs[n_xs]; ++n_xs) 
    sum_xs += xs[n_xs];

  { // If the user tries to supply more exes than we can handle,
    // we must either fail or produce the wrong answer.  It's better to fail.
    int ignore;
    if (std::cin >> ignore) 
    {
      std::cerr << "data set too large\n";
      return 1;
    }
  }

  double const average = sum_xs / n_xs;
  
  int n_greater_than_average = 0;
  for (int i = 0; i < n_xs; ++i) 
    if (xs[i] > average) ++n_greater_than_average;

  std::cout << 100.0 * (static_cast<double>(n_greater_than_average) / n_xs) << '\n';
}
Last edited on
I just need to make a counter for my above average elements. Going kind of by what you did and matching it to my variables, would this work?

1
2
3
4
5
6
int aboveaverage =0 ;

for (int i =0; i < counter; ++i)
if (salaries [i] > average) ++ aboveaverage;

cout << 100.0 *  (aboveaverage / counter) << " \n";
Last edited on
nvm it doesn't, I think I just need to study up on counters as they pertain to arrays.
The problem is that aboveaverage / counter is an int dividing an int.

In C++-world, 1 / 2 gives you 0. With integer division, any fractional part is thrown away. But 1.0 / 2 is an int dividing a double. That gives you 0.5.

To convert an int into a double, write
static_cast<double>(my_int)
As in line 29 of my post.
Last edited on
As I mentioned in vasncode's previous code - see my code at http://www.cplusplus.com/forum/beginner/283232/

This is a duplicate post of a duplicate post...
@vasncode: Please explain how/if these differ (without creating a new thread):
1
2
3
4
5
6
7
int aboveaverage, counter;
A:  100.0 * (aboveaverage  / counter)
B:  100.0 * (static_cast<double>(aboveaverage) / counter)
C:  100.0 * (aboveaverage  / static_cast<double>(counter))
D: (100.0 *  aboveaverage) / counter
E:  100.0 *  aboveaverage  / counter
F:  100   *  aboveaverage  / counter
Last edited on
Topic archived. No new replies allowed.