...

View Full Version : calculating the average of given numbers.



muya08
11-09-2008, 03:50 AM
am trying to calculate the average and median of numbers keyed in by the user. written the code, can anyone please help me with the compilation errors. am using a bloodshed dev c++ kind of compiler.


/*Lesen Sie beliebig viele (max. 50) Zahlen (Abschluss mit dem Wert 0) ein, speichern Sie diese in einem Feld und geben Sie Mittelwert, Median und die Standardabweichung der Zahlen aus. (Bei der Ermittlung der Medians können Sie davon ausgehen, dass die Zahlen schon sortiert sind, vom Benutzer
also entweder in aufsteigender oder in fallender Reihenfolge eingegeben werden.)*/

#include<iostream>
#include<cmath>
using namespace std;

int main(){
int arr[50];
int zahl;
int sum = 0;
int average;
int j;
int median;


cout<<"please key integers not exeeding 50 and not a zero: ";
cin>> zahl;
int i;
for(;zahl!=0;i++){ // i is the actual count of given numbers i.e normally frm 1
arr[i] = zahl; //storage of given number
}
for(int k=0;k < i;k++){ // k is the storage of numbers in array
sum = sum + arr[k]; //sum = sum + the number stored at index 0 and the k++ helps move nxt position
average = sum/i; // divide by i the actual count
cout<<"The avearge/mittelwert = "<<average;
}

j=i/2;//calculating the median
if (i%2!=0){
median = arr[j];

cout<<"median is: "<<median;

else

median = (arr[j]+ arr[j-1])/2; // for even numbers
cout<<"median is: "<<median;
}
return 0;
}



thanks.

oesxyl
11-09-2008, 01:45 PM
what errors? you must provide errors message.

as far as I see you are missing some {,} in code:


j=i/2;//calculating the median
if (i%2!=0){
median = arr[j];

cout<<"median is: "<<median;

}else{

median = (arr[j]+ arr[j-1])/2; // for even numbers
cout<<"median is: "<<median;
}
}
return 0;
}


and please put your code between [ code] and [ /code], without spaces inside
regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum