pointers and references

Hello,

I have this function

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 the parameters' names such as:

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
	cout << "Average: " << *average << '\n';
	*numSmallerThanAverage = *(myArray+0);
	for(int i = 0; i < size; i++)
		cout << *(myArray+i) << " ";
	for(int i = 1; i < size; i++)									//check until condition i=size
	{
		if(*numSmallerThanAverage > *(myArray+i))
			*numSmallerThanAverage = *(myArray+i);
	}
	cout << "\nSearching..." << endl;
	//display the result...
	cout << "The number of elements smaller than the average value is = " << *numSmallerThanAverage << endl;
	system("pause");
}	


int main(int argc, char* argv[])
{
	int* myArray = NULL;
	int size;
	cout << "Enter numbers: ";
	cin >> size;
	myArray = new int[size];
	for(int i = 0; i < size; i++){ 
		myArray[i] = 0;
	}
	void smallAverage(int *myArray, float *average, int *numSmallerThanAverage);

	delete[] myArray;
	return 0; 
} 
myArray should be declared as
void smallAverage(int *myArray[], float *average, int *numSmallerThanAverage)
which is a pointer to an array of ints; or
void smallAverage(int **myArray, float *average, int *numSmallerThanAverage)
which is a pointer to a pointer to int; which is the same thing, essentially.

The way you're calling smallAverage is wrong aswell; you want to do
smallAverage(myArray, float* average, int* numSmallerThanAverage)
because you have declared myArray already, and you've also declared smallAverage previously aswell.

So you're trying to figure out the number of numbers which are smaller than average?

Then you want to fill myArray with numbers; find the median (middle-most number) by dividing the highest value by 2. Doing integer division will truncate the decimal and everything after it, so you want to do that with floats and then convert to an integer. In doing so you will add 0.5 -- if the number is < x.5 it will be truncated to x.0; if it is > x.5 it will be rounded to x + 1; e.g.
1
2
3
4
float x = 0.5;
int y = (x + 0.3);

std::cout << y == 1;

will be false, because x + 0.3 == 0.8 which will be truncated to 0.
However if you did x + 0.5 == 1.0, y will == 1. If you did x + 0.6 (or higher), y would be truncated to 1 anyway.

Right so at this point you should have the median value; now you want to find the numbers which are smaller than median (which is stored in average). Then you need to add 1 to numSmallerThanAverage (you want to add to the value, not the address/reference by the way) for every value smaller than the median value.

You should have a float* average and int* numSmallerThanAverage in main so that you can print them. Your function should not be printing anything; or it should just not have average and numSmallerThanAverage as parameters because that doesn't make sense.

The point of having pointers as parameters is so that the function can edit them -- that way if you have "float* average" in main and you want smallAverage to change it; it should be passed as a pointer...

In conclusion, your use of pointers as parameters doesn't really make much sense...
Last edited on
sorry, but I dont get how you explained it to me, because when I do what you have told me to do for the declaration of myArray I get many errors...but when its the way that you see it above in my code its fine and I get no errors!!

Another thing is that this function name is given like that ... so I cannot really change the parameters or like ignore some of them and declare them somewhere else..you know what I mean?

Please can you show me in code what you have explained about the calculation because I cant get it in words...I'm so new to C++ and I dont really understand it that much..also when you explained the clculation of the median, where do I put that calculation? in the function or the main?
There's nothing wrong with the way you've passed the array, but it shouldn't work anyway because in your function you don't assign size a value. You need to pass that to your function as well. Also, instead of using *(myArray + i) you can index the pointer just like myArray[i].

Ideally you wouldn't have output or that system("pause") in your function. You should do your output in main using the values that have been passed back. For example:

1
2
3
4
5
6
7
// inside main
int array[] = {1, 2, 3, 4, 5};
int size = 5;
float average = 0.0f;
int numSmallerThanAverage = 0;
smallAverage(array, size, average, numSmallerThanAverage);
cout << "insert stuff here" << endl;


Your function's prototype would look like this:
void smallAverage(int *array, int size, float &average, int &numSmallerThanAverage);
Hello Chewbob,

The problem is that we are not allowed to change the function name, so do you mean I pass the size to my function like inside it or with its parameters? and when you say assign size a value, I thought I cant because its user defined and we dont declare a specific value for it?

so like when you wrote:

"Your function's prototype would look like this:"
void smallAverage(int *array, int size, float &average, int &numSmallerThanAverage);
do you mean by that the function for pass by reference and why did you add size as one of the parameters? since that we are not allowed to change the function declaration?
When the array is passed to the function, there is no way of knowing how big it is unless you tell it. In your code you have this:

1
2
3
4
5
6
7
8
9
void smallAverage(int *myArray, float *average, int *numSmallerThanAverage)
{
    int size;
    int sum = 0;
    for(int i = 0; i < size; ++i){
        sum += *(myArray+i);
    }
    // etc.
}


You declare the variable called size but at no point it is assigned a value and in your loop you are comparing i and size. That's why you would need to pass the size into the function as an argument as well as the actual array.

The & operators make the parameters references. So, for example, the output from this code would be 5.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

void change(int &i)
{
    i = 5;
}

int main()
{
    int a = 1;
    change(a);
    std::cout << a;
    return 0;
}


That is what passing by reference is.
ok hello,

so far i have done the following and I hope that is better from before:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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, &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;
}



I just still dont know how I would redefine the this function for pass by reference, I mean do I just copy paste the function with whatever inside it and just change every * with &?

Thanks
ok I have this idea for the size , becaue in my function the variable 'size' is undefined...so one way to know the size of the array in the function is to use a while loop. First you need to make the array 1 size bigger than what the user inputs and set the very last element value to a negative number. Then in the function use a while loop such as while(myArray[i]!=-1)

now, I know it by words as i have wrote it, but i dont know to covert that into code? please I need help anyone?
Topic archived. No new replies allowed.