C++ Array and Statistics

Hello all I am new to C++. I have been given a problem domain. I must prompt the user for up to 100 values. Negative values will be used as sentinel value.we must Prompt the user repeatedly for positive integers, until the sentinel value is reached. From that array we must determine and print out the following for that set of values: Minimum, Maximum, Range, Mean (average), Median. The values for minimum and average are inaccurate and Im not sure why.
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

// Project 5 stats.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
const int SIZE = 100;
int numbers[SIZE];
int count = 0;

cout << "Please enter your integers. " << endl;
cout << "You may enter up to 100 integers. " << endl;
cout << "Negative numbers will be the sentinal value. " << endl;

do
{
		cout << "Enter your integer" << endl;
		cin >> numbers[count];
		cout << "Enter a negative number to stop accepting array input \n Or positive number to continue.";
		count = count + 1;

} while (numbers[count-1] > 0);

//The Min and Max values.


int mn, mx;


mn = numbers[0];
mx = numbers[0];

for(int count=1; count<100; count++)
{
	if(mn>numbers[count])
	{
		mn=numbers[count];
	}
	else if(mx<numbers[count])
	{
		mx = numbers[count];
	}
}



cout << "the max number is: " << mx << endl;
cout << "the min number is: " << mn << endl;

int average=0;
int sum=0;

for (int i = 0; i < SIZE; i++)
{
	sum+=numbers[i];
average = sum/SIZE;
	cout << "average is: " << average; 
}



	return 0;
}


Any help would be appreciated, thank you.
Line 44, not "else if" just "if"

Line 61 and 62 should be moved to 64 and 65 respectively

EDIT: also, when you input the negative number to end the input, that negative number still gets stuffed in the array. So it'll be your minimum regardless.

Cheers!
Last edited on
Thank you for your response. are you wanting me to move line 61 and 62 outside of the for loop, if so , why?

How do i change the code so that the negative number wont get stuffed in the array?
the output im getting for both min and max is -858993460
ok i changed up my code a bit. i changed
while (numbers[count-1] > 0);
to
while (numbers[count-1] >= 0);

the minimum is printing out fine but the max shows the same value as minimum
line 38: for(int count=1; count<100; count++)
You haven't necessarily entered 100 values, so you want to stop when count is less than the number entered instead of at 100. Plus in the original do while() loop you could enter more than 100 values which would exceed the bounds of numbers[] so that's something else to fix. Both problems can be fixed by declaring a variable to store the number of values entered.
ok guys ive improved the code. now im trying to figure out how to find the median of the array.

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
// Project 5 stats.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
int numbers[100];
int count = 0;
int min = -1;
int max = -1;
int sum = 0;
int average = 0;
int range = 0;
float median = 0;

cout << "Please enter your integers. " << endl;
cout << "You may enter up to 100 integers. " << endl;
cout << "Negative numbers will be the sentinal value. " << endl;

do
{
		cout << "Enter your integer" << endl;
		cin >> numbers[count];
		cout << "Enter a negative number to stop accepting array input \n Or positive number to continue.";
		
		if (numbers[count] < 0)
		{
			break;
		}
		else
		{
			if (count == 0)
			{
				min = numbers[count];
				max = numbers[count];
			}
			else 
			{
				if (min > numbers[count])
				{
					min = numbers[count];
				}
				
				if (max < numbers[count])
				{
					max = numbers[count];
				}
			}
		}
		
		sum  = sum + numbers[count];
		count = count + 1;
		
} while (numbers[count-1] >= 0) ;



cout << "the min number is: " << min << endl;
cout << "the max number is: " << max << endl;

range = max - min;
cout << "The range is: " << range << endl;


average = sum / count;
cout << "The average is: " << average << endl;


	return 0;
}
Erm... It's been a while since school... is median simply the number that's in the middle? or is it (lowest+highest)/2?

EDIT:
If it's A) median=array[numberofthings/2];
If it's B) median=(max+min)/2;
Last edited on
If our sample set of values were sorted (that is a hint to help you solve this) they would look like this:
2, 3, 3, 3, 4, 4, 6, 7, 8, 9, 11, 12
The Median is the value that would come between the left six values (2, 3, 3, 3, 4, and 4) and the right six values (6, 7, 8, 9, 11, and 12). It is actually simpler than that. The Median is the average of 4 and 6. That is (4+6)/2 or 5. If there are an odd number of values, the median is the middle value of the sorted values.
So you need to sort your list. Simplest way I can think of off the top of my head is the bubble sort algorithm. Wikipedia has a nice article on it: https://en.wikipedia.org/wiki/Bubble_sort
Once the list is sorted finding the median is relatively easy.

Also in your while() test you need to account for count, right now the user can still enter over 100 values which is bad.
Topic archived. No new replies allowed.