### can you guys check this array problem??

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.

73.3 99.0 83.4 58.0 25.1 69.0 1.9 95.0 74.8 55.0
49.5 47.4 95.6 58.9 62.6 37.7 93.8 19.9 23.9 77.2

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..
------------------------------------------------------------------------------

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

void programInfo();
void printList(double a[], int n);
void bubbleSort( double a[], int n);

void inputValue(double value);
double medianSortedList(double a[], int n);
double averageList(double a[], int n);
double stdDeviation(double a[], int n);

using namespace std;

int main()
{
int count = 0;
double scores[20];
double value;

cout << fixed << showpoint << setprecision(1);

programInfo();

cout << "Input a list of up to 20 values (-1.0 to stop) : ", cin >> value;

//inputValue(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;

}

}

//ascending sort
bubbleSort(scores, count);

cout << "The list of values: " << endl;

//print a list of values in an array
printList(scores, count);

//average
cout << "\nAverage: " << averageList(scores, count) << endl;

// median
medianSortedList(scores, count);

//stdDeviation
cout << "\nStandard Deviation: " << stdDeviation(scores, count) << endl;

cout << endl << endl;

return 0;
}

/*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;

stdDeviation = sqrt(var);

return stdDeviation;
}
Last edited on
try code tags
 `[code][/code]`
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
 ``12345678`` ``````double input[20]; unsigned int 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);``````

Haven't tried but should work. I'm tired though
Last edited on
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.

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

void printList(double a[], int n);
void bubbleSort( double a[], int n);

void inputValue();
double medianSortedList(double a[], int n);
double averageList(double a[], int n);
double stdDeviation(double a[], int n);

using namespace std;

int main()
{
int count = 0;
double scores[20];

cout << fixed << showpoint << setprecision(1);

inputValue();

//ascending sort
bubbleSort(scores, count);

cout << "The list of values: " << endl;

//print a list of values in an array
printList(scores, count);

//average
cout << "\nAverage: " << averageList(scores, count) << endl;

// median
medianSortedList(scores, count);

//stdDeviation
cout << "\nStandard Deviation: " << stdDeviation(scores, count) << endl;

cout << endl << endl;

return 0;
}

void inputValue()
{
double scores[20];
int count = 0;
double value;

do
{cout << "Input a list of up to 20 values (-1.0 to stop) : ", cin >>value;

scores[count] = value;
count = count + 1;

if(value < -1.0 || value > 100.0)
cout << "The value cannot be negative and over 100.0." << endl;

}
while( value != -1.0 && count < 20);

}

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;

stdDeviation = sqrt(var);

return stdDeviation;
}

-------------------------------------------------------------------------------------
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:
 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include void referenceFunction( unsigned int& ); const unsigned int returnFunction1( void ); const unsigned int returnFunction2( unsigned int ); int main( void ) { unsigned int 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 <

Secondly you should put your while loop more like this
 ``123456789101112131415`` ``````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);``````
Last edited on
Thanks man. It helped a lot :)
Topic archived. No new replies allowed.