Function Overloading & Array Problem

Hi guys, I'm fairly new to C++ (Just started a couple weeks ago) and I've run into much trouble with this question involving function overloading and arrays. I've already seen a glimpse of the finished code my prof showed in class but did not see it long enough to copy it down. From what i saw it looked rather small and simple. Can someone please fix this so I can understand how to run it? The description is below.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
  /*** Quiz 2, CTEC-208-061, 13W

********************************************************************
This question is about overloaded functions.

You are to write two functions, with the same name 'triple' to handle
the tasks requested, as shown in main(). The function(s) will triple
either one integer or all entries in an array of integers.

When called to triple one integer, that integer is passed to it, and
the function returns changes the number to three times its original.

When called to triple the entries of an array, the array name and the
number of entries to be changed are passed to the function.
The function changes the requested number of entries, but leaves the
rest unchanged.

Study the sample run carefully.

DO NOT CHANGE ANY THING IN main()

------Sample run:
New value of n = 30
New array values:
3
6
9
12
5
6

*/
#include <iostream>
#include <iomanip>
using namespace std;

void triple(int x);
void triple(int y, int z);

// DO NOT change any thing in main()
void main() {
	int a[] = {1, 2, 3, 4, 5, 6};
	int n = 10;
	triple(a, 4);	// multiply the first 4 elements(only) of the array by 3
	triple(n);		// multiply n by 3
	cout << "New value of n = " << n << endl;
	cout << "New array values:\n";
	for(int i = 0; i < 6; i++)
		cout << a[i] << endl;
}


void triple(int x){

	cout << x * 3 << endl;
}


void triple(int y, int z){

	for(int i = 0; i < z; i++){
		cout << y[z] * 3 << endl;
	}
}

Last edited on
Your second triple function does not take arrays as a parameter. You need the syntax void funct(int array[], int arraysize), of course replacing the names in my example with what you need.
ahh I see, that makes sense. It compiles now but it still doesn't multiply the first 4 elements of the array. Can't really figure out what's wrong.
Can you expound on what happens when you run the program? Is the wrong number of values being multiplied, does it do nothing, etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
void triple(int y[], int z){

	for(int i = 0; i < z; i++){
		cout << y[i] * 3 << endl;
	}

}

void triple(int x){

	cout << x * 3 << endl;
}


This is what i did to "fix" it. When I run it, i get this:

3
6
9
12
30
New value of n = 10
New array values:
1
2
3
4
5
6


What i take from it is that it actually did multiply the first 4 elements by 3. (3, 6, 9, 12) and it multiplied n by 3. (30) but it puts them on top instead of replace the old values. Any idea whats wrong?

Oh okay, I see now.

...it puts them on top instead of replace the old values.

Your functions only print out the result of a multiplication. When you called triple(a,4) and the triple(n) functions, they did exactly what they were supposed to do: print out the results.
Now what you want to do is modify the variables. Since this is a quiz, all I will say is to make use of a symbol that dennotes "memory address" and perhaps get rid of the cout statements inside the triple functions if you do not want to display the results prematurely.

Edit:
Don't want to confuse you. By memory address, I meant something that starts with an "r."
Last edited on
Memory address as in a pointer? Is it necessary in this program? How would i go about implementing it?
Actually, I realized that my hint was not that accurate, which is why I editted it. There is a different kind of variable you can use whose name begins with "r."
hmm, pass by reference? I was thinking of that but I would have to modify the main function but the quiz says we can't.

I got the array multiplication to work properly by just removing cout and making it reinitialize but the triple n still does not work! i MUST be missing something, i just can't see it.

1
2
3
4
5
6
7
8
9
10
11
void triple(int y[], int z){

	for(int i = 0; i < z; i++){
		y[i] = y[i] * 3;
	}

}

void triple(int x){
		x = x * 3
}
Last edited on
You do not need to modify the main function at all to use reference variables. Just remember that they are a way to modify variables directly, i.e. passing by reference versus passing by value.
Got it!!! Thank you sir you have been great help to me tonight! I appreciate it and will definitely be on this site often from here on
Topic archived. No new replies allowed.