Finding Median

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mehwishobaid
    New Member
    • Mar 2008
    • 3

    #1

    Finding Median

    i dont know wat is wrong with my code. when i compile. i get the error

    saying line 29: error: expression must have pointer-to-object type


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

    double* median(double a[], int a_size) // Function Median
    { // Start of Function ( Median )
    double* median;
    int i,j;
    double temp;
    int numbers; // Variable Declared Median
    if (a_size == 0) // Start of If Statement
    return NULL; // if the size of the array is zero the pointer returns NULL


    for (i = a_size-1; i >= 0; i--) //sorts out the array in an ascending order
    { // Start of For Loop
    for (j = 1; j <= i; j++) // For Loop
    {
    if (a[j-1] > a[j]) // If Statement to Check the VAlue of A
    { // Start of IF Statement
    temp = a[j-1]; // Assign Temp value to a
    a[j-1] = a[j]; // Array in Descending Order
    a[j] = temp; // Assign array to Temp
    } // close of IF Statement
    } // Close of For Loop
    } // close of For in For Loop
    //if the size is even number than takes the average else returns the middle number
    if (a_size % 2 == 0) //compares the size for even number
    median = ((numbers[a_size/2]) + (numbers[(a_size+1)/2])/2);// Define Median
    else // else odd size
    median = numbers[a_size+1]; // Assgin median for Odd Number of Array

    return median; // Return Median value to Main
    } // Close of Function ( Median )

    int main() // Function main
    {

    int first;
    int num2;
    int i; // Variable Declared ( int )
    int Exit; // Variable Declared ( Dummy )

    cout << "Enter The Length of First vector"; // Output Statement for the Length of 1st Vector
    cin >> first; // User Input for the Length of 1st Vector
    cout<<endl; // Blank Line
    vector <int> a(first); // Vector First


    for (i=0; i<first; i++) // Start of For Loop for User Input 1st Vector
    {
    cout<< "Please enter element "<<i+1<<" of the First Vector";// Output For Entering Vector
    cin>>num2; // User Input
    cout<<endl; // Blank Line
    a[i] = num2; // Adding Element in Vector A
    }


    cout <<"The Median of the Data is :" << median*(a, first) << "\n"; //Displays the Medianimum value out of the array
    cout<<" Please Enter Any Key to Exit";
    cin>>Exit;
    return 0;
    }
  • Sick0Fant
    New Member
    • Feb 2008
    • 121

    #2
    You have a thing or two to learn about pointer aritmetic. The ar[x] always returns a value, not an address (unless it's an array of addresses, which I doubt numbers is). You should try (ar + x)

    Comment

    • mehwishobaid
      New Member
      • Mar 2008
      • 3

      #3
      if i try ( ar + x ) the line gives me the error of

      expression must have (pointer-to-) function type
      :S

      i also need some help in callin the function Median.. when it says..
      cout <<"The Median of the Data is :" << median*(a, first) << "\n";
      the error associated with this cout statement is

      error: expression must have arithmetic or enum type

      Comment

      • mehwishobaid
        New Member
        • Mar 2008
        • 3

        #4
        thankyou for you help :).

        Comment

        Working...