I have a problem with my code please

Discussion in 'C++' started by nanoooosha, Oct 7, 2009.

  1. nanoooosha

    nanoooosha New Member

    Joined:
    Oct 7, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    I have this function

    Code:
    void smallAverage(int *array, float *average, int *numSmallerThanAverage );
    which returns in the parameters, average and numSmallerThanAvergae, the corresponding result. average is the average value of all the elements of the array. numSmallerThanAverage is the number of elements in the array which are smaller than the average.
    I need to redefine the above function such that it utilizes pass by reference to return the results, i.e., the average and the number of elements smaller than the average.

    Now, I have the code for the pass by pointer function, even though I'm not really sure if its alright and its perfect and I would really appreciate it if someone would take a look at it for me...
    for the pass by reference function, I dont know how to do it, but I was guessing that it would be the same as my pass by pointer function except that I would change every
    Code:
    *
    with
    Code:
    &
    everywhere they are found in the function such as:

    Code:
    void smallAverage(int &array, float &average, int &numSmallerThanAverage );
    I dont know if I'm wrong or not and I really need help please..oh by the way I dont know if that would be helpful, but these two functions need to be tested in a main routine and print the results. The main function must accept the size of the array as a command line parameter. And the array should be initialized with random numbers.

    Here is my code:


    Code:
    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    #include <ctime>
     
    using namespace std;
     
     
    int main(int argc, char* argv[])
    {
    int size;
    int* myArray = NULL;
     
    if (argc >= 2) size = atoi (argv[1]);
    else {
    cout << "Enter array size in command line: ";
    return -1;
    }
    myArray = new int[size];
    srand(time(NULL));
    for(int i = 0; i < size; i++){ 
    myArray[i] = rand();
    }
    float* average;
    int* numSmallerThanAverage;
    smallAverage(myArray, average, int* numSmallerThanAverage);
    delete[] myArray;
    return 0; 
    } 
     
    /****************************************function # 1**********************************************************/
    void smallAverage(int *myArray, float *average, int *numSmallerThanAverage)
    {
    int size;
    int sum = 0;
    for(int i = 0; i < size; ++i){
    sum += myArray[i];
    }
    *average = (float)sum/(float)size;
     
    *numSmallerThanAverage = 0;
    for(int i = 0; i < size; i++)
    cout << myArray[i] << " ";
    for(int i = 0; i < size; i++) //check until condition i=size
    {
    if(*average > myArray[i])
    (*numSmallerThanAverage)++;
    }
    cout << "\nSearching..." << endl;
    //display the result...
    cout << "The number of elements smaller than the average value is = " << *numSmallerThanAverage << endl;
    } 
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    To change the code to pass-by-reference instead of pass-by-pointer you need to change * to & in the function parameters, and within the function use the variables as if they were local, so instead of
    Code:
    *numSmallerThanAverage = 27;
    
    as you would with pointers, you just write this instead:
    Code:
    numSmallerThanAverage = 27;
    
    This will work for the two scalars. Have a look at http://www.cplusplus.com/doc/tutorial/arrays/ to see how to pass an array by reference.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice