0
#include <iostream>
using namespace std;

int main()
{
    int n, G;
    float num[500], sum=0.0, average,Grades;

    cout << "Enter the numbers of data: ";
    cin >> n;

    while (n > 500 || n <=  0)
    {
        cout << "Error! number should in range of (1 to 500)." << endl;
        cout << "Enter the number again: ";
        cin >> n;
    }


int grades_on_avg, upper_grades = 0; 
for(int i = 0; i < n; ++i)
{
    cout << i + 1 << ". Enter number: ";
    cin >> num[i];
    sum += num[i];
       
}
average = sum / n;
for(int i = 0; i < n; ++i) // use separate for loop and redifend index
{
    if(num[i] == average) // compare to average
        grades_on_avg++;
    else if(num[i] > average) // if bigger than average
        upper_grades++;
}



cout<<endl;

cout << "Grades Average = " << average << endl;
cout << "Grades above or equal the Average = " << (grades_on_avg + upper_grades) << endl;
cout << "Number of grades above the Average = "<< upper_grades ;
    
    return 0;
}

i coded this code but the Number of grades above the Average don't show the Entered grade by the user that is above the avg why

I want print the entered grade from user if it above the avg how

WhozCraig
  • 63,603
  • 10
  • 71
  • 134
devo
  • 5
  • 3
  • 1
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel May 28 '22 at 19:45
  • Fyi, `grades_on_avg` is indeterminate after its definition, unlike `upper_grades`, which is at-least initialized to zero. That should be `int grades_on_avg = 0, upper_grades = 0;` . Your compiler should have warned you about this on the line `grades_on_avg++;` . If it didn't, you need to turn up your warnings (and, as always, treat them as errors). – WhozCraig May 28 '22 at 19:49
  • It don't make any change – devo May 28 '22 at 19:54
  • I tried to make grades_on_avg = 0 but same result – devo May 28 '22 at 19:55
  • I didn't say it would fix your problem; i simply pointed out it was flat-out broken. Your post should include your test input, your expected outcome, and your actual outcome. I suspect you're expecting `if(num[i] == average)` to just "work", but in reality an entered grade compared precisely against the computed average probably isn't doing what you think it is. If you *debug your code* and examine the values of `num[i]` and `average` I suspect it will be illuminating. – WhozCraig May 28 '22 at 20:07
  • Aha ok thanks bro ❤️❤️ – devo May 28 '22 at 20:09

1 Answers1

0

I guess this would do:

  1. Always initialize variables. Especially those you're immediately using, like grades_on_avg. An int grades_on_avg{} should suffice (since C++11); otherwise, int grades_on_avg = 0 is perfectly OK.
  2. Float equality is more complicated than f1 == f2 unfortunately. You'll find lots of discussions about it. The almost_equal function used below is just an example from cppreference.

Also:

  • Prefer to declare variables close to where they are first used; that makes the code more readable.
  • Prefer to declare one variable per line; it may lead to confusion not to do it that way sometimes (e.g. int* a, b;).
  • Prefer std::vector<float> nums(n) to float nums[500]; instead of reserving a fixed array of 500 elements, use a dynamic array of exactly n elements.
  • Using {} even for one-line blocks will save you from some surprises.
  • Try not to using namespace std.
  • Prefer using '\n' to std::endl.

[Demo]

#include <cmath>     // fabs
#include <iostream>  // cin, cout
#include <limits>    // epsilon
#include <vector>

bool almost_equal(float x, float y, int ulp) {
    return
        std::fabs(x - y) <= std::numeric_limits<float>::epsilon() * std::fabs(x + y) * ulp
        || std::fabs(x - y) < std::numeric_limits<float>::min();
}

int main() {
    int n{};
    std::cout << "Enter the numbers of data: ";
    std::cin >> n;
    while (n > 500 || n <= 0) {
        std::cout << "Error! number should in range of (1 to 500).\n"
                  << "Enter the number again: ";
        std::cin >> n;
    }

    std::vector<float> nums(n);
    float sum{};
    for (int i = 0; i < n; ++i) {
        std::cout << i + 1 << ". Enter number: ";
        std::cin >> nums[i];
        sum += nums[i];
    }

    float average = sum / n;
    int grades_on_avg{};
    int upper_grades{};
    for (int i = 0; i < n; ++i) {  // use separate for loop and redifend index
        if (almost_equal(nums[i], average, 2)) {  // compare to average
            grades_on_avg++;
        } else if (nums[i] > average) {  // if bigger than average
            upper_grades++;
        }
    }

    std::cout << "\nGrades Average = " << average << "\n";
    std::cout << "Grades above or equal the Average = "
              << (grades_on_avg + upper_grades) << "\n";
    std::cout << "Number of grades above the Average = " << upper_grades
              << "\n";
}

// Inputs:
//
// 5 1.1 3.3 5.5 7.7 9.9

// Outputs:
//
//   Grades Average = 5.5
//   Grades above or equal the Average = 3
//   Number of grades above the Average = 2
rturrado
  • 6,450
  • 6
  • 40
  • 60