Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   I have a problem with my code please (http://www.go4expert.com/forums/i-code-t19699/)

nanoooosha 8Oct2009 00:34

I have a problem with my code please
 
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;
}


xpi0t0s 8Oct2009 12:35

Re: I have a problem with my code please
 
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.


All times are GMT +5.5. The time now is 12:28.