Averaging a sequence of numbers

I readily admit that this is a homework assignment and I'm not looking for any answers but rather just a direction to try and go in. I have to write a program that will have a user input the amount of numbers in a sequence and then type in those numbers. I then have to find the average of them and the range. Right now I have this:

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
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>

using namespace std;

int main ()

{
	const float MAX = 900000000;            // Assumes that no value the user inputs will be this large
	const float MIN = -2000000000;          // Assumes that no value the user inputs will be this small
	int numbersInSequence;                  // This is the total number of numbers in the sequence as input by user
	int count;                              // The count of the numbers that are being input
	float num;                              // This is a number in the sequence that the user inputs
	float min;                              // The smallest number input
	float max;                              // The largest number input
	float range;                            // The range of the values
	
	
	// Set the minimum value to 900,000,000 because it's assumed that no value being read
	//  in will be that large
	
	min = MAX;
	
	// Set the maximum value to -2,000,000,000 because it's assumed that no value being read
	//  in will be that small
	
	max = MIN;
	
	// Set the count equal to zero
	
	count = 0;
	
	// Get the number of numbers in the sequence from the user
	
	cout << "How many numbers are going to be in the sequence: " << endl;
	cin >> numbersInSequence;
	
	// Get the numbers from the user that are in the sequence
	
	cout << "What are the numbers in the sequence: " << endl;
	cin >> num;
	
	while (count <= numbersInSequence)
	{
		// Count the current number being input
		
		count++;
		
		// Check the current number to see if it is a new maximum
		
		if (num > max)
			max = num;
		
		// Check the current number to see if it is a new minumum
		
		if (num < min)
			min = num;
	}
	
	// Calculate the range of the values
	
	range = max - min;
	
	// Display the range of the values
	
	cout << "The range of the values is: " << range << endl;
	
	return 0;
	
}


I'm just wondering if anyone could help on what direction I need to go in with my loop statement. Basically they will input the numbers in the sequence and then on the next line they'll input each number with a white space in between. I need to read those numbers in and silmutaenously keep track of the sum of those numbers and the largest and smallest as they're being read in. Any advice on what direction to take this is welcome and I thank you in advance.

Edit: I've changed it to this but I keep getting a range of 0 as my return.
Last edited on
I assume line 40 needs to be moved into the while loop (which could be written as a for loop.)
Okay I've changed it up a little bit and I have this in my loop now:

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
while (count <= numbersInSequence)
	{
		// Count the current number being input
		
		count++;
		
		// Check the current number to see if it is a new maximum
		
		if (num > max)
			max = num;
		
		// Check the current number to see if it is a new minumum
		
		if (num < min)
			min = num;
		
		cin >> num;
	}
	
	// Calculate the range of the values
	
	range = max - min;
	
	// Display the range of the values
	
	cout << "The range of the values is: " << range << endl;
	
	cout << count << endl; // This is there just so I can see what the count is 


This is the output I get from the program:

1
2
3
4
5
6
7
8
9
How many numbers are going to be in the sequence: 
2
What are the numbers in the sequence: 
12
18
19
20
The range of the values is: 7
3


For some reason it's making me type in four numbers when I said that only two should be in the sequence. The range is off by 1 and the count is at 3 even though there should only be two numbers in the sequence and I type in 4.

Edit: The 7 actually isn't wrong. For some reason it's only reading the first three numbers I'm inputting and then making me input a fourth before it will display the values. I'm very confused.
Last edited on
@thechad90000

Some of the inputs had to be re-arranged. Here is your revised program.
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Average Sequence.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int main ()

{
	const float MAX = 900000000;            // Assumes that no value the user inputs will be this large
	const float MIN = -2000000000;          // Assumes that no value the user inputs will be this small
	int numbersInSequence;                  // This is the total number of numbers in the sequence as input by user
	int count;                              // The count of the numbers that are being input
	float num;                              // This is a number in the sequence that the user inputs
	float min;                              // The smallest number input
	float max;                              // The largest number input
	float range;                            // The range of the values


	// Set the minimum value to 900,000,000 because it's assumed that no value being read
	//  in will be that large

	min = MAX;

	// Set the maximum value to -2,000,000,000 because it's assumed that no value being read
	//  in will be that small

	max = MIN;

	// Set the count equal to zero

	count = 0;

	// Get the number of numbers in the sequence from the user

	cout << "How many numbers are going to be in the sequence: " << endl;
	cin >> numbersInSequence;

	// Get the numbers from the user that are in the sequence

	cout << "What are the numbers in the sequence: " << endl;
	cin >> num;
	if (num > max)
		max = num;

	// Check the current number to see if it is a new minumum

	if (num < min)
		min = num;
	count++;

	while (count < numbersInSequence)
	{
		cout << " The next number in the sequence is ? ";
		// Count the current number being input
		cin >> num;
		count++;

		// Check the current number to see if it is a new maximum

		if (num > max)
			max = num;

		// Check the current number to see if it is a new minumum

		if (num < min)
			min = num;
	}


	// Calculate the range of the values

	range = max - min;

	// Display the range of the values

	cout << "The minimum number was " << min << " and the maximum number was " << max << endl;
	cout << "The range between the two is : " << range << endl;

	return 0;
}
Thanks for the reply, but why do I need to repeat the if statement. I thought that was the point of the loop?
@thechad90000
The first if statement is for the initial num input. The min/max is adjusted, count increased by one, then the while loop is used to get the remaining 'numbersInSequence'. Count is increased, again by one, after each num input, min/max is checked, then the loop exits if count now equals 'numbersInSequence'. Then, of course, the min/max values are printed, along with the 'range' on the next line.
Okay I tried the change you made but for some reason it's still making me input 1 more number than I should have to. I'm entering in that I only want to enter 2 numbers in, but it's asking for 3 of them still. Other than that everything works fine.
@thechad90000
Sounds like your while loop reads while (count <= numbersInSequence) instead of just less than. (<)
If it is, then count would have to actually equal more than 'numbersInSequence' to justify the loop. If it isn't, please re-post your changed code, and I'll look into it.
Wow that fixed it right up. Thank you so much.
@thechad90000
You're very welcome..

You may want to mark this problen as 'solved'
Last edited on
Topic archived. No new replies allowed.