Hey i am new here and i am new to coding. My instructor gave me a question which i am close to cracking it i have a problem.
here is the question:Write a function that reads 10 numbers and finds the average of all the numbers that are greater
than 20 and less than or equal to 50. Return the average.
here is what i have so far-
#include<iostream>
using namespace std;
int printAvg(int,int);
int main()
{
int size=9;
int array[size];
int sum=0;
for(int i=0;i<=size;i++)
{
cout<<"Enter your values "<<i+1<<endl;
cin>>array[i];
sum=sum+array[i];
}
getting there.
what you need is just a little something more. a counter to track how many values fit the criteria, and a condition to check the criteria... something like this:
if( value > 19 && value < 51)
{
sum += value;
counter ++;
}
and at the end, average is / counter, not size
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I reworked your program along with jonnin's and icy1's suggestions with an addition of my own. The comments should explain most everything:
#include<iostream>
#include <iomanip> // <--- Added.
//using namespace std; // <--- Best not to use.
double printAvg(int, int); // <--- Returns a double per icy1's suggestion.
int main()
{
constexprint SIZE = 10; // <--- To use properly this needs to be this way or you can use "const". The size should be 10 not 9. 9 will only give you 8 elements in the array.
// Best not to use the name "array" for the array name. Potential problem when "using namespace std;" is used.
// <--- Also the numbers here are used when testing. You can leave them, but best to remove them for normal run and just leave the empty {}s.
int array[SIZE]{ 9, 10, 22, 15, 44, 60, 33, 50, 55, 40 };
int sum = 0;
int count{}; // <--- Added per what jonnin's suggested.
for (int i = 0; i < SIZE; i++) // <--- Changed to "<" from "<=" Using "<=" will put you one past the size of your array.
{
//std::cout << "Enter your values " << i + 1 << ": "; // <--- The comments are for testing. And I did change the "cout" look.
//std::cin >> array[i];
if (array[i] > 20 && array[i] <= 50) // <--- Added per what jonnin's suggested.
{
sum += array[i];
count++;
}
}
std::cout << std::fixed << std::showpoint << std::setprecision(4); // <--- Added. Requires the <iomanip> header file.
std::cout << "\nAverage=: " << printAvg(sum, count) << std::endl; // Changed what is sent to the function.
// The next line may or may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue. ";
std::cin.get();
return 0;
}
double printAvg(int sum, int count) // <--- Changed per icy1's suggestion.
{
return sum / static_cast<double>(count); // <--- This will change the math from an "int" to a double before the result is returned.
}
The two to three lines just above the return is a way for many people to keep the console window open before return ends the program.
Something that may help would be to save an extra copy where you can remove the comments and just see the code.
I am not trying to say that all of the changes are needed for this program, but something to consider for the future.