Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    calculating the average of given numbers.

    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.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    what errors? you must provide errors message.

    as far as I see you are missing some {,} in code:
    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;
    }
    Edit: and please put your code between [ code] and [ /code], without spaces inside

    regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •