Max, min, average and sum of given sequence

Hello, I'm trying to develop a program that returns the max, min, average and sum of a sequence. The problem is that I need to give the size of the sequence and it is messing it up. The program is working fine but it always asks for an extra number even though it applies the sum, average, min, max operations to the first three numbers.

I know it has something to do with the fact I'm asking for the first the input outside the loop and when my 'i' reachs three it still asks for one more number and then it breaks. I just can't sort it out!

Any idea?

Best regards

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
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;



int main()
{


	int size;
	double sum, input;
	float avg;
	double max;
	double min;
	int i;

	cout << "Sequence size?  ";
	cin >> size;
	//cin.ignore();
	cout << "Type the sequence" << endl;
	cin >> input;
	max = min = input;
	//cin.ignore();

	while (i < size)
	{
		sum+= input;
		i++;
		if(input > max){
			max = input;
		}
		else if(input < min){
			min = input;
		}

		cin >> input;
		//cin.ignore();
	}


	if (i == 0)
		cout << "No data entry" << endl;
	else
	{


		cout << "soma: " << sum << endl;
		cout << "Media : " << (soma/i) << endl;
		cout << "Max: " << max << endl;
		cout << "Min: " << min << endl;
		cout << "Size: " << i << endl;

	}



}
Last edited on
If you want to input a sequence then you are going to need to use array or vector.

As it is your sequence can only be one number.

You need to initialise soma too. There are a few things amiss with the code.
What is the float for? Do we need floats when we have doubles?

No? http://stackoverflow.com/questions/2386772/difference-between-float-and-double
Last edited on
You don't need an array or a vector for this. The calculations do not need the numbers stored in order to get the result.

As above, you need to initialize sum. soma doesn't even exist as a variable in your program.

Now for the answer to your question. What is i when you start the loop?

You should initialize your variables when you declare them. As is, they could be 5394757, and you wouldn't know until the program ran. The only reason things work out now is because your compiler caught your error of not initializing them, or (unlikely) the previous value in that address was 0. Good practice is to always initialize variables to a value in this language
I misstyped some of the variables cos I translated for better understanding.
I did sort it out.
First I initialized i = 0 and added this:
1
2
3
if (i == size){
			break;
		}


after this:

1
2
3
4
5
6
if(input > max){
			max = input;
		}
		else if(input < min){
			min = input;
		}


Working as intended now, I guess.

Thanks for the tips and patience.

Best regards
I guess if it is working for you, then that's great, but you should have just incremented i after getting your first input. That would have worked without the break statement and extra check.
.
Last edited on
But my first input is out of the while loop. Would that work?
Yeah. You'll increment i outside of the while loop, so when you increment it after the last input, i will be equal to the size. The problem was that your i was equal to size-1 after the last input because it wasn't incremented after every input. If you increment it after the first input, i should be equal to size after the last input.
Topic archived. No new replies allowed.