Hello everyone!
My program is designed to take 3-30 values from the user and have it run through a handful of functions to compute average, median, and Standard Deviation.
However, once I put a set of test case values, my output results in a jumbled mess of what I assume to be an infinite loop.
I am completely lost on what exactly I did wrong. I would greatly appreciate any help.
#include <iostream>
#include <iomanip> // for Setprecision
#include <cmath> // for pow and sqrt
usingnamespace std;
int main()
{
// Declare Local Variables
constint SIZE=30.0;
double array[SIZE];
double average;
double median;
double std;
int count = 0;
// Set Precision for Decimal Points
cout << setprecision(1) << fixed <<showpoint;
//Module: inputArrayValues
//Description: Ask the user to input values between 3 and 30 elements for the array to hold. Also validates that the user inputs a total of values between 3 and 30.
void inputArrayValues (double array[], int SIZE, int &count)
{
double number = 0;
constdouble SENTINEL = -1.0;
// Basic information about what the user can input. Does not repeat.
cout << "Please enter values one at a time." <<endl;
cout << "Up to a maximum of 30 values and a minimum of 3 values." << endl;
cout << "Only positive values are accepted or the program will not work." << endl;
cout << "With the exception, please enter the value -1.0 to stop entering values." << endl;
// While Loop
// Variable count is counting how many values the user inputs for later calculation
for (int i = 0; i < SIZE; i++)
{
while (number!= SENTINEL)
{
cout << "Please enter a value or enter -1.0 to stop entering values" << endl;
cin >> number;
array[i] = number;
if (number != SENTINEL)
{
count++;
}
}
}
if (count < 3 || count > SIZE)
{
cout << "Invalid total number of values." << endl;
cout << "The total number of values must between 3 and 30 values." <<endl;
cout << "This program will now close..." << endl;
cout << "Thank you for using this program." << endl;
}
}
//Function: comupteAverage
//Description: Computes the average of the given inputs.
double computeAverage (double array[], int count)
{
double sum = 0.0;
double resultA;
for (int i =0; i < count; i++)
{
sum = sum + array[i];
}
resultA = sum / count;
return resultA;
}
//Function: computeMedian
//Description: Computes the Median of the given inputs.
double computeMedian (double array[], int count)
{
double resultM;
if ((count % 2) == 0)
{
resultM = (array[count/2] + (array[count/2] -1.0) /2.0);
}
else
resultM = array[count/2];
return resultM;
}
//Function: computeSTD
//Description: Computes the Standard Deviation of the given inputs.
double computeSTD (double array[], int count, double average)
{
double temp;
double sum = 0;
double resultV;
for(int i = 0; i < count; i++)
{
temp = pow((array[i] - average), 2);
sum = sum + temp;
}
//Account for Sample Standard Deviation N-1
resultV = sqrt(sum/(count -1));
return resultV;
Test Case numbers I used.
73.3
83.4
58
11.9
25.1
69.9
45.7
95.0
44.4
-1.0 // To stop entering values
i am confused about your code, it seams the functions are defined in the main function. i never see somebody write code in this style. they should be defined outside the main function. also i don't see you call your functions in main().
i think there is a mistake in inputArrayValues (), the while loop will never stop in every loop of the for loop, until you input -1.0. in your code, you shoud input data like this 73.3 -1.0 83.4 -1.0 ....until the variable i is 30.
i paste my code as follows:
count = 0;
cin >> num;
i = 0
while(num != -1.0){
array[i]= num;
cin >> num;
i++;
if(i>= 30){
cout << "you have input " << i << "values" <<"no more should be inputed"<<endl;
break; //end while loop, input number of data should less than 30
}
}
if(i < 3){
cout << "The total number of values is " << i << , "and must between 3 and 30"<<endl;
}
my English is not good, there maybe some mistakes in my description, but i wish it could help you.
@coder777 I apologize for the sloppy representation. I absolutely had no clue on what was causing it and failed to reduce the code well enough for it to be repeatable. I will watch out when I post huge blocks of code next time. Sorry for the inconvenience.
@dhayden Unfortunately I cut too much of my code out that I did not include the sorting module. However, the sorting is working.
@mzzz Your English was clear and understandable! I did call my modules and functions in my main function. Your advice reassured me that the changes made are good changes.
To all, I apologize for the delay in my response. I had to sleep for class at 6 AM. However, after a brief break I tackled the problem again and discovered that it was my while sentinel loop that did not go according to plan. I instead replaced the while sentinel loop for a "for" loop and nested a if loop to check for the sentinel value. All things are working except my calculation for median. Despite my best attempts. My calculation for median is giving me either too little or too large than the expected amount.
Since the program can take a total number of values between 3 and 30. I used a variable named "count" to tally up the total amount of times the user enters a value. However, once I pass this variable to computeMedian the calculation seems to go wrong. I have searched online and asked friends about this problem. However, they advice methods and steps that I was not taught in my class and so I should not use them.
I thank each of you for spending some time looking at my code. I really do appreciate your replies!