I'm getting stuck with the counter

Write your question here.
When I input these 5 numbers 50 60 70 80 90 I should get 40 but it is not happening.
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
  #include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
	int t,counter;
    cin>>t;
    double num[t];
    double sum =0,avg,excellence;

    for(i=0;i<t;i++){
        int counter = 0;
        cin>>num[i];
        sum+=num[i];
        avg=sum/t;}

        for(int a=0;a<t;a++){

            counter = 0;
            if(num[a]>avg){
                counter++;}
                
            }
            excellence= counter/ t;
        cout<<excellence<<endl;
	return 0;
}
Last edited on
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
#include <iostream>

int main() {

    // in C++, the size of an array must be a constant known at compile time
    // std::size_t: http://en.cppreference.com/w/cpp/types/size_t
    const std::size_t MAX_SZ = 1024 ; // maximum possible
    double numbers[MAX_SZ] {} ; // initialise to all zeroes

    std::size_t sz = 0 ; // actual count of numbers
    std::cout << "how many numbers? " ;
    std::cin >> sz ;

    if( sz > MAX_SZ ) sz = MAX_SZ ; // sanity check (at most MAX_SZ numbers)
    if( sz < 2 ) sz = 2 ; // sanity check (at least two numbers)

    std::cout << "enter " << sz << " integers:\n" ;
    double sum = 0.0 ;
    for( std::size_t i = 0 ; i < sz ; ++i ) {

        std::cin >> numbers[i] ;
        sum += numbers[i] ;
    }

    const double average = sum / sz ;

    std::size_t counter = 0 ; // count of numbers > average
    for( std::size_t i = 0 ; i < sz ; ++i ) if( numbers[i] > average ) ++counter ;

    const double excellence = double(counter) / sz ; // **** avoid integer division

    std::cout << "fraction of numbers greater than average: " << excellence
              << " (ie. " << excellence * 100.0 << "%)\n" ;
 }
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>
#include <vector>
#include <numeric>
#include <algorithm>

using namespace std;

int main()
{
  vector<double> numbers; // prefer vector over arrays
  double tmp;

  while (cin >> tmp) // terminate with ctl+z or F6 on windows
    numbers.push_back(tmp);
  
  // ES.1: Prefer the standard library to other libraries and to “handcrafted code”
  // https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-lib
  double sum = accumulate(numbers.begin(), numbers.end(), 0.0);
  double avg = sum / numbers.size();

  int counter = count_if(numbers.begin(), numbers.end(),
                         [avg](const int num){ return num > avg;});
  
  // copied from JLBorges
  const double excellence = double(counter) / numbers.size(); 

  cout << "Average: " << avg << "\n";
  std::cout << "fraction of numbers greater than average: " << excellence
    << " (ie. " << excellence * 100.0 << "%)\n";
}


Output:
50 60 70 80 90^Z
Average: 70
fraction of numbers greater than average: 0.4 (ie. 40%)
Topic archived. No new replies allowed.