Disregard

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

#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;
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 =  sizeof(aboveaverage) / counter * 100.00;  
	
  cout << "\nPercentage of salaries above average: " << percent << "% \n";
	cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}



}
Last edited on
Line 55: you loop entire array. On line 45 you did loop only the part that has salaries ...

display salaries above the average

You display on line 60 the integer 'high'. That is not "a salary that is above average".
You should:
IF salary is above average THEN display it

display the percentage of salaries above the average

On line 63 you pick one salary and divide it by number of salaries. How is that "percentage of numbers"?


Tip: if you divide integer with integer, then the result is integer; remainder is discarded. Since all salaries can't be "above average", the percentage is less than 100%. a/b, where a<b is 0.
If either operand is a float, then division is different and produces a float.
One can force that: static_cast<float>(a) / b

However, while static_cast<float>(3)/4 is about 0.75, you should not show "0.75%". That is a tiny amount. We expect to see "75%". 300/4 is 75, isn't it?
Last edited on
[continued from http://www.cplusplus.com/forum/beginner/283232/ - see my response in that thread]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <valarray>
using namespace std;

template<typename T> ostream & operator << ( ostream &out, const valarray<T> &V )
{
   for ( T e : V ) out << e << ' ';
   return out;
}

int main ()
{
   valarray<double> salaries = { 45050, 52900, 19010, 56000, 23460 };
   double average = salaries.sum() / salaries.size();
   valarray<double>highSalaries = salaries[salaries > average];
   double percentHigh = 100.0 * highSalaries.size() / salaries.size();
   
   cout << "Average salary = " << average << '\n'
        << "Higher-than-average salaries = " << highSalaries << '\n'
        << "Percentage of higher-than-average salaries = " << percentHigh << '\n';
}


Average salary = 39284
Higher-than-average salaries = 45050 52900 56000 
Percentage of higher-than-average salaries = 60

Okay its completely changed, the problem is now the percentage.

Im trying to divide the amount of above average numbers by the amount of numbers the user inputted initially.
Oh, you have edited the code in the first post. Don't do that. Now comments about the post as it was originally do not make sense; are out of context. Rather show the new code in new comment.

Your new code is even worse than the original. I think it is. Hard to prove as we can't see both versions.
yikes okay noted
Topic archived. No new replies allowed.