Click here to Skip to main content
14,880,638 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
count = arry[0];
           for (i = 0; i < 18; i++)
           {
               if (count==50);
               {
                   count;
               }
           }
       
        cout << "Total Count of the numbers greater than or equal to 50 : " <<count ;


What I have tried:

C++
int sum = 0.0;
    double average=0.0;
    int i, max, min, count{ 0 };
    int arry[18] = { 16,24,55,25,41,63,90,62,42,28,99,86,13,83,33,91,58,16 };
    for (i = 0; i < 18; i++)
        sum += arry[i];
    for(i = 0; i < 18; i++)
    average = sum / 18;
    cout << "Sum of the numbers : " << sum << endl;
    cout << "Average of the  numbers : " << average << endl;
    max = arry[0];
    for (i = 0; i < 18; i++)
    {
        if (max <= arry[i])
        {
            max = arry[i];
        }
    }
    cout << "Maximum value of the numbers : " << max << endl;
    
    min = arry[0];
    for (i = 0; i < 18; i++)
    {
        if (min >= arry[i])
        {
            min = arry[i];
        }
    }
       cout << "Minimum value of the numbers : " << min << endl;
          count = arry[0];
           for (i = 0; i < 18; i++)
           {
               if (count==0);
               {
                   count;
               }
           }
       
        cout << "Total Count of the numbers greater than or equal to 50 : " <<count ;
        return 0;
Posted
Updated 9-Oct-20 4:50am
v3
Comments
Richard MacCutchan 9-Oct-20 9:12am
   
You forgot to count the numbers > 50. And if you think about it you could do all four operations (sum, count, min and max) within the same loop.
Japanese Relaxation 9-Oct-20 9:16am
   
would you mind can you show me plz?
Richard MacCutchan 9-Oct-20 9:21am
   
See below
Andreas Gieriet 9-Oct-20 9:26am
   
You tagged as C++. So, you might want to look for something like this: http://www.cplusplus.com/reference/algorithm/count_if/
Rick York 9-Oct-20 11:45am
   
The number 18 appears in your code six times. What would happen if you were told to use 20 numbers? This is why you should avoid using literal values in your code as much as possible. Since this is c++, you could declare a const value for that size and use it everywhere.

C++
int arry[18] = { 16,24,55,25,41,63,90,62,42,28,99,86,13,83,33,91,58,16 };
int i, min, max, sum, count;
min = arry[0];
max = arry[0];
sum = 0;
count = 0;
for (i = 0; i < 18; i++)
{
    if (min > arry[i])
    {
        min = arry[i];
    }
    if (max < arry[i])
    {
        max = arry[i];
    }
    if (arry[i] >= 50)
    {
        count++;
    }
    sum += arry[i];
}
int avg = sum /18;
// print the results here.
   
v2
Comments
KarstenK 9-Oct-20 9:24am
   
I would define the 19 as size_array to avoid future changes.
Andreas Gieriet 9-Oct-20 9:29am
   
If you want to make it robust, you also have to consider empty arrays. I.e. what to do for min/max/avg on empty arrays? Not to talk about overflow when calculating avg for large arrays/big numbers?
Richard MacCutchan 9-Oct-20 9:35am
   
I specifically did not want to make it robust as that adds levels of complication that detracts from the actual answer.
Richard MacCutchan 9-Oct-20 9:33am
   
It's not supposed to be robust code, just an example of counting.
Japanese Relaxation 9-Oct-20 9:35am
   
thank you so much professor #Richard MacCutchan all of you.
for help me .
Richard MacCutchan 9-Oct-20 9:36am
   
You are welcome.
CPallini 9-Oct-20 10:31am
   
5.
Richard MacCutchan 9-Oct-20 10:40am
   
Thanks, my K&R C skills still in demand. :)
Try also (just to have a glimpse of modern C++, Richard implementation, with just one traversal, is more efficient)
C++
#include <iostream>
#include <algorithm>
#include <array>
#include <numeric>
using namespace std;


int main()
{
  array<int,18> a{ 16,24,55,25,41,63,90,62,42,28,99,86,13,83,33,91,58,16 };

  const auto beg = a.begin();
  const auto end = a.end();


  auto min = *min_element(beg, end);
  auto max = *max_element(beg, end);
  auto ge50 = count_if(beg, end, [](int x) { return x>= 50;});
  auto sum = accumulate(beg, end, 0);

  cout << "min " << min << ", max " << max << ", ge50 " << ge50 << ", sum " << sum << endl;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900