i need a bit of help in one of my codes.

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];
}

cout<<"Average=: "<<printAvg(sum,size);

return 0;
}
int printAvg(int sum,int size)
{
return sum/size;
}
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

Last edited on
Thank you very much jonnin i will give it a try and see if it works, i really appreciate it.
for average you probably want to cast one of them to a double or else you'd be doing integer division.

integer division:
1
2
int x=5, y=4;
int avg = x/y;  // 1 


division with casting one of numbers to a double:
1
2
int x=5, y=4;
double avg = (double)x/y;  // 1.25 


so perhaps also rename your method to double GetAverage(int x, int y) or similar (it doesn't actually print anything and should return a double)
Hello roro samuda,

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:
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
35
36
37
38
39
40
41
42
43
44
#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()
{
	constexpr int 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.

Hope that helps,

Andy
thanks you all are right here thanks for your support http://www.windowstech.co/
Topic archived. No new replies allowed.