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;
}
}
|