Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem: Write a program that reads in integers and counts the number of those i

ID: 3886207 • Letter: P

Question

Problem: Write a program that reads in integers and counts the number of those integers that are less than the average. Output a single number, the count, with no other text. Your program must read from standard input and write to standard output. Read until EOF or cin goes bad. Hint: prime the loop and use “while (cin)”. For simplicity, assume there will be at least one number entered. Your solution doesn't need to be Object-Oriented!

Some examples:

Input: 1 2 3 4 5 x     Output:2

Since the average is float(15) / 5 = 3.0 and 2 numbers are less than the average, which are 1 and 2.

Input: -3 6 8 -1 18 0 qOutput:3

Since the average is float(28) / 6 = 4.6667 and 3 numbers are lessthan the average, which are -3, -1 and 0.

Input: 2 2 2 2 2 a Output: 0

Since the average is 2.0 and all numbers are not less than the average.

Explanation / Answer

// C++ code
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;


int main( )
{
int array[1005];

double average = 0;
double number;
int size = 0;

cout << "Input numbers: ";
while(cin >> number)
{
average = average +number;
array[size] = number;
size++;
}

average = average/size;
int count_morethan_average = 0;

for (int i = 0; i < size; ++i)
{
if(array[i] < average)
count_morethan_average++;   
}

cout << "Total Numbers less than average: " << count_morethan_average << endl;
return 0;
}

/*
output:

Input numbers:
1
2
3
4
5
x
Total Numbers less than average: 2


Input numbers:
-3
6
8
-1
18
0
q
Total Numbers less than average: 3


Input numbers:
2
2
2
2
2
a
Total Numbers less than average: 0


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote