Using loops to find min/max, arithmetic and geometric means

I'm working on the following code and our teacher has assigned us to use loops to find the above mentioned items. I'm having issues with my Visual Studios C++ compiler giving me errors before I even compile for using 'return sums'. Any advice on how to fix this?? (or anything that looks of issue?) Side note, geometric means cannot be computed if there is a negative.

Thanks for your input in advance!

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
45
46
  
void stat()
{
	unsigned	counter			= 0;
	double		sums			= 0;
	double		product			= 0;
	double		next			= 0;
	double		min;
	double		max;
	min = max = next;
	

	double		arith_mean;
	double		geo_mean;
	bool		found_neg = false;


	cout<<"Give me some numbers, non-number to quit: ";
	if(!(cin>>next))
		cout<<"no data"<<endl;

while( next )
{	
	sums += next;
	counter++;

	return sums;
}

cout<<"Arithmetic Mean: "<<arith_mean = sums/counter<<endl;


while ( next >= 0 )

{
	product *= next;
	counter++;

	return product;
}

cout<<"Geometric Mean:"<<geo_mean = pow(product, 1./counter)<<endl;



}
Do you understand what the return statement does? Your function declares that it returns void, which means it does not return anything.

Also, you have several logical errors - for instance, the loop on line 22 will be infinite if "next" is not 0 - you never do anything inside the loop to change the value of "next".
Last edited on
closed account (3qX21hU5)
void stat()

You are telling the compiler that your function isn't going to return anything when you declare it void.

So when you say return sums; you are violating that and that is why the compiler is giving you a error.

If you want to be able to return sums from your function change it to double stat()

EDIT: lol damn you LB you this is the third time someone ninja'ed my post with the same answer just a few seconds before. First Miini did it 2 times now you ;p.
Last edited on
Sadly, I cannot change the void stat(). It's one of the parameters given by my professor for the problem, which he probably figured someone would do what I just did... Back to the drawing board. thank you for your input.
You're probably not supposed to use "return" then ;)
Yeah, I'm just trying to get the hang of these loops... I know this is basic logic, but the there are holes in my understanding that seem incredibly important... It's stupidly frustrating...
What's the first thing you're stuck on?
I can watch someone write out code and get the general idea of what it is doing, but writing code I cannot do for beans (see above). I what each loop is SUPPOSED to do, but I have no idea how to make it work...
closed account (3qX21hU5)
I think you are thinking that you need to return a variable from a loop to be able to use it outside of that loop?

If not please ignore all that I am about to say. Basically that is kind of right, but you can't really return a variable from a loop.

First loops have their own scope, meaning anything that you declare inside of your loop will be destroyed after the loop is finished.

So for example

1
2
3
4
5
6
7
8
9
10
11
12
13
// The loop will run only when the condition it has is true. So when we specify 
// (true) we are saying run this loop forever. Usually this isn't a good idea
// but this is just a example.
while (true)
{
    int number;
    
    // We can use the variable number inside the loop.
    number = 55;
}

// But if we try to outside of the loop it won't work
number = 10;    // Error 


So when you want to be able to use a variable inside a loop and still be able to use it when the loop finishes you need to declare that variable before and outside of the loop. For example

1
2
3
4
5
6
7
8
9
10
11
12
// Here we declare number
int number = 0;

while (number != 9)
{
    // Notice we can use number inside of the loop.
    number = 9;
}

// We can still use the varaible number even after the loop is done.
// Since we declared number before the loop and outside of the loop.
number = 55;


Hope that helps a bit. If not just let us know what you don't understand about loops and we would be more then willign to help.
Last edited on
that does help quite a bit, thank you Zereo. Would it be better for me to use a for loop rather than a while loop for this problem? I would like to learn what tools to use where. Thanks all for your input.
When you know how many things you're dealing with in advance, you use a for loop.

When you don't yet know how many things you're dealing with, but you're going to find out in the loop, use a while loop.
I guess the hard part is what do I set up as the condition to be met. I know that the loop needs to end once there are no more numbers to be read then out put the sum and the amount of numbers counted to get the arithmetic mean...
Thank you so much L B! :D I wish that was written in text books.
So in my problem I am to ask the user to enter a non number to quit. How can I set that as the condition?
closed account (3qX21hU5)
When the user entera a non number into a integer variable the stream will fail. Look at line 19 in your code that should give you a idea what to do.

I would also suggest reading up on how streams work in C++ since they are very handy to know about.

Here is a quick example


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()
{
    int number;
    int sum = 0;

    cout << "Enter your numbers or a non number to stop!" << endl;

    // The condition is saying keep the loop running 
    // until cin >> number is not true. So since when we 
    // enter anything but a number into a integer the stream break
    // the condition will be false since we are no longer able to read
    // numbers into the variable.
    while (cin >> number)
    {
        sum += number;
    }

    cout << sum << endl;
}


You are also going to have to take a look at how you want your program to run again. Remember that you will need to find the max number entered, min number enters and the means.

What I do is before I start to code anything I grab a notepad or go into a text editor and write down all the requirements of my program. So your would look like this

1) Find sum
2) Find max
3) Find min
ect ect.

I then write down anything I will need to do that requirement, or any ideas I have for it. Like this.

1) Find sum: Need to have a variable to store the sum of all the numbers in. When inputting the numbers just += the inputted number to sum IE (sum += number).

2) Find max: Need a variable to hold the largest number entered. To find this largest number I need conditional operators to compare all the numbers entered so far.

ect. ect.

For large projects its also good to break the problem down into little managable problems and then make a plan of how to go through it like this.

Problem: Get each students name, age, and grade from their test. Then find the median of the students grades.

1) Create a class type that will be used for each student.

2) Get input from the user about each student. I'm going to use a vector<Student> to hold all of the students

3) Sort the vector<Student> by their grades. Use the std::sort() function

4) Find the median of the grades.

Now obviously that is a very simplified version of it but it shows how it can help. That way before I every start coding I know exactly what I need to do step by step, what variables I will need and the tools I need. So I go in with a plan which makes it much easier to solve each problem.
Last edited on
Topic archived. No new replies allowed.