### Disregard

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

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` `````` #include #include #include #include using namespace std; int main () { cout<<" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" <> 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< average) { aboveaverage = salaries[i]; cout<< " \$"<
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]

 ``123456789101112131415161718192021`` ``````#include #include using namespace std; template ostream & operator << ( ostream &out, const valarray &V ) { for ( T e : V ) out << e << ' '; return out; } int main () { valarray salaries = { 45050, 52900, 19010, 56000, 23460 }; double average = salaries.sum() / salaries.size(); valarrayhighSalaries = 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.