im working on this like 30 hours.. sigh.. but this is still having lot of problems..
write a program so that user can use it to input, compute, and display various statistics for a list of at least 3 values(display an error message and stop the program if fewer than 3 values were entered) and up to 20 positive values between 0.0 and 100.0 of type double (reject invalid values and continue with next value). also input another array from the list below as another test case.
program shall display the original array( maximum 5 values per line), sorted array in ascending order( maximum 5 values per line)
i have to use the function..
-input a list of values (void inputList (double a[], int & n); )
- input and validate one value ( reject bad value other than -1.0)
- print a list of values in an array ( max 5 values per line)
rest things are done already. but.. i keep failling those 3 functions..
------------------------------------------------------------------------------
/*void inputValue(double value)
{
double scores[20];
int count = 0;
cout << "Input a list of up to 20 values (-1.0 to stop) : ", cin >> value;
while( value != -1.0 && count < 20 );
{
scores[count] = value;
count = count + 1;
cout << "Input a list of up to 20 values (-1.0 to stop) : ", cin >>value;
if(value < 0.0 || value > 100.0)
{
cout << "The value cannot be negative and over 100.0." << endl;
}
}
}*/
void printList(double a[], int n)
{
for (int i = 0; i < n; i++)
{
cout << setw(8) << a[i];
if ( i % 5 == 0 )
{ cout << "\n " << endl;
}
}
}
void bubbleSort( double a[], int n)
{
int i, j, temp;
for( i = 0; i < n - 1; i++)
{
for( j = 1; j < n - i; j++)
{
if(a[j - 1] > a[ j ])
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
}
double medianSortedList(double a[], int n)
{
int median, median1;
if( n % 2 == 1)
{
median = (n / 2 );
cout << "median: " << a[median] << endl;
}
else
{
median1 = (n / 2) - 1;
median = (n / 2);
cout << "median: " << a[median1] << " and " << a[median] << endl;
}
return a[median];
}
double averageList(double a[], int n)
{
int i;
double total = 0.0, average;
for(i = 0; i < n; i++)
total += a[i];
average = total / n;
return average;
}
double stdDeviation(double a[], int n)
{
int j;
double sum = 0.0, var,stdDeviation;
for(j = 0; j < n; j++)
sum += (a[j] - averageList(a,n) ) * (a[j] - averageList(a,n));
var = sum / n;
or the <> button in the edit/reply
You should also try to indent it is very hard to read it like that.
btw you are calling the function programinfo() but you never actually did anything with the function so that's pointless. Your input method also is ill formed and is probably supposed to be a semi-colon not a comma I am pretty sure you have a couple of syntax errors. Oh and your while statement could be better you say if its not -1 but don't think about -2, -1.2 ect..so why not try something like if it's less than 0? and ps with doubles it will never be exactly -1 it will probably be like -1.0000000000000001 or something. Happens when you use doubles. Btw on your input you are saying it can only be 1-99 not 0-100 you would have to do greater than -1 and less than 101 or greater than or equal to 0 and less than or equal to 100 to get a range of 0-100 ps might want to use a do/while loop to get the 20 inputs.
eg
1 2 3 4 5 6 7 8
double input[20];
unsignedint i = 0;
do
{
std::cout << "Please enter a number. " << std::flush;
std::cin >> input[i];
i++;
} while( i < 20 && input[i] > -1 && input[i] < 101);
yeah i know i deleted programinfo() and when i checked there was no syntax errors. but i wanna learn how to utilize functions of "input a list of values and input ( void inputList(double a[], int &n)" and "validate one value (reject bad value other than -1.0 ( double inputValue(); )"
and yeah thanks for your helping but i dont know that code didnt work well..
This one worked. thanks for suggestion to use do while. but there are two problems. if i put that codes which is in inputValue module in main module, this program worked. however when i call the inputValue(), this program doesnt work. and when i enter -1, program is stopped but i can see -1 with comment.
firstly you are not actually assigning the values from the input to the array in the main function to do that you could do a few things. Here are a couple of ways to do that:
#include <iostream>
void referenceFunction( unsignedint& );
constunsignedint returnFunction1( void );
constunsignedint returnFunction2( unsignedint );
int main( void )
{
unsignedint a = 0;
std::cout << "a as default value: " << a << std::endl;
referenceFunction( a );
std::cout << "a after referenceFunction: " << a << std::endl;
a = 0;
std::cout << "a as default value: " << a << std::endl;
a = returnFunction1();
std::cout << "a after returnFunction1: " << a << std::endl;
a = 0;
std::cout << "a as default value: " << a <<std::endl;
a = returnFunction2( a );
std::cout << "a after returnFunction2: " << a << std::endl;
}
void referenceFunction( unsignedint &a )
{
a++;
}
constunsignedint returnFunction1( void )
{
return( 1 );
}
constunsignedint returnFunction2( unsignedint a )
{
a++;
return( a );
}
a as default value: 0
a after referenceFunction: 1
a as default value: 0
a after returnFunction1: 1
a as default value: 0
a after returnFunction2: 1
Process returned 0 (0x0) execution time : 0.195 s
Press any key to continue.
Secondly you should put your while loop more like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
do
{
std::cout << "Input a list of up to 20 values (-1.0 to stop) : " << std::flush;
std::cin >>value;
if(value >= 0 && value <= 100.0) //this might not work even with the equal signs because of the doubles
//I would suggest on normal ints value > 0 && value < 101
//but that wouldn't always work with doubles
{
scores[count] = value;
count++;
} else {
std::cout << "The value must be between 0 and 100." << std::endl;
}
} while( value != -1.0 && count < 20);