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.
// Project 5 stats.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int _tmain(int argc, _TCHAR* argv[])
{
constint 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];
}
elseif(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;
}
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.
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.
// Project 5 stats.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
usingnamespace 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;
}
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.