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

My source code is NOT WORKING C++ $ ./average How many numbers? 3 Enter Number 1

ID: 3880872 • Letter: M

Question

My source code is NOT WORKING C++

$ ./average
How many numbers? 3
Enter Number
1
5
6
The average is 4
The numbers less than the average are -2.06623e-19 The numbers less than the average are 4.59163e-41 The numbers less than the average are 5.88117e-39

Why am I getting ridiculously tiny numbers?

// This program calculates the average of any number of numbers.
// Using the for structure
#include

#include

int main()
{
int n;
float sum, avg;

sum = 0;
std::cout << "How many numbers? ";
std::cin >> n;
int size= n;
float array[size];
std::cout << "Enter Number" << std::endl;

for (int j = 0; j   
std::cin >> array[n];
sum = sum + array[n];
} //end for

avg = (float) sum / n;
std::cout << "The average is " << avg << std::endl;


for(int i = 0; i < size; i++) {

if(array[i]< avg){

std::cout << "The numbers less than the average are " << array[i] << " ";

}

}

return 0; //successful termination

}//end main

Explanation / Answer

Hey while taking input you are taking input in array[n] which should be array[j] . n is contant and wont change while you are iterating you need to store input in array[j]. If you have any doubts please do comment. Below is the working code

#include <iostream>

int main()
{
int n;
float sum, avg;

sum = 0;
std::cout << "How many numbers? ";
std::cin >> n;
int size= n;
float array[size];
std::cout << "Enter Number" << std::endl;

for (int j = 0; j<n;j++){
std::cin >> array[j];
sum = sum + array[j];
} //end for
avg = (float) sum / n;
std::cout << "The average is " << avg << std::endl;

for(int i = 0; i < size; i++) {
if(array[i] < avg){
std::cout << "The numbers less than the average are " << array[i] << " ";
}
}

return 0; //successful termination
}//end main