### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` ``````#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<< " \$"< you have absolutely no {} on your above average section.
perhaps like this
 ``1234567`` ``````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:

 ``123456789101112131415161718192021222324252627282930`` ``````#include 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(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?

 ``123456`` ``````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... ``1234567`` ``````int aboveaverage, counter; A: 100.0 * (aboveaverage / counter) B: 100.0 * (static_cast(aboveaverage) / counter) C: 100.0 * (aboveaverage / static_cast(counter)) D: (100.0 * aboveaverage) / counter E: 100.0 * aboveaverage / counter F: 100 * aboveaverage / counter``````