Need help with these C++ problems: Part I: What is output by the following? Assu
ID: 3730986 • Letter: N
Question
Need help with these C++ problems:
Part I:
What is output by the following? Assume the necessary header files are included.
1. int funct_1(int, int);
int main() {
int num1=2, val;
val = funct_1(8, num1) + funct_1(num1, 4);
cout << val;
return 0;
}
int funct_1(int x, int y) {
return x + y;
}
2. void dostuff(int cnt, int extra);
void main(void) {
int a=3, b=2;
dostuff(a, b);
dostuff(2, 4);
}
void dostuff(int cnt, int extra) {
int i;
for(i=0; i<=cnt; i++) {
cout << i + extra << " ";
}
cout << endl;
}
3. int funct_1(int, int);
void main(void) {
int num1=2, val;
val = funct_1(8, funct_1(num1, 5) );
cout << val << endl;
cout << funct_1(val, num1);
}
int funct_1(int x, int y) {
int temp;
temp = x + y + 3;
return temp;
}
4. int dostuff(int cnt);
void main(void) {
int a=3, c;
c = dostuff(a);
cout << c << endl;
cout << dostuff(4);
}
int dostuff(int cnt) {
int i, sum=0;
for(i=0; i<=cnt; i++)
sum += i;
return sum;
}
5. void userInOut(int);
void main(void) {
int cnt=3;
userInOut(cnt);
}
void userInOut(int cnt) {
int num, sum=0;
for(int i=1; i<=cnt; i++) {
cout << "Enter a number: "; // assume user enters 2, 14, -5
cin >> num;
sum += num;
}
cout << "Sum is " << sum;
}
6. float userIn(int);
void main(void) {
int cnt=2;
float avg;
avg = userIn(cnt);
cout << avg;
}
float userIn(int count) {
int num, sum=0;
for(int i=1; i<=count; i++) {
cout << "Enter a number: "; // assume user enters 6, 7
cin >> num;
sum += num;
}
return static_cast<float> (sum) / count;
}
7. int reference(int, int, int &);
void main(void) {
int x=5, y=6, z=3;
x = reference(4, y, z);
cout << x << ", " << y << ", " << z;
}
int reference(int x, int y, int &c) {
c = x + y;
return y;
}
8. void funct_stat(int);
int main(void) {
funct_stat(2);
funct_stat(3);
}
void funct_stat(int x) {
static int num = 2;
num = num + x;
cout << num << ", ";
}
Explanation / Answer
1.
Output:
16
val = funct_1(8, num1) + funct_1(num1, 4); will result in val = funct_1(8,2) + funct_1(2,4) = 10 +6 = 16
the function funct_1() returns the sum of its two arguments.
2.
Output:
2 3 4 5
4 5 6
dostuff(3,2); call will execute for loop 4 times(0 to 3) display 0+2,1+2,2+2,3+2 ie 2 3 4 5
dostuff(2, 4); call will execte for loop 3 times(0 to 2) display 0+4,1+4,2+4 ie 4 5 6
3.
Output:
21
26
val = funct_1(8, funct_1(num1, 5) ); will result in val = funct_1(8, funct_1(2, 5) );
val = funct_1(8, 10 ); val = 21 (8+10+3) , val = 21 is displayed
cout<<funct_1(val, num1); will display 21+2+3 ie 26
4.
Output:
6
10
dostuff(3); will return sum = 0+1+2+3 = 6
dostuff(4); will return sum = 0+1+2+3 +4= 10
5.
Output:
Enter a number:2 Enter a number:14 Enter a number: -5 Sum is 11
3 numbers are inputted in the function's for loop using argument cnt. sum in the function = 2+14-5 = 11
6.
Output:
Enter a number: 6 Enter a number:7 6.5
The function userIn returns the average of 2 numbers 6 and 7 by sum = 6+7 and then by casting the int sum into float data type so result = 6.5
7.
output:
6, 6, 10
x = reference(4, 6,3); will return y ie 6 so x = 6;
y = 6;
z = 4+6 = 10 and as it is refrence variable so the value will be retained even after the function returns
8.
Output:
4, 7,
funct_stat(2); call will set num = 2+2 = 4
funct_stat(3); call will set num = 4+3 = 7. This is because num is declared static variable so it retains its value between two function calls.
Do ask if any query, Please upvote if the answer is useful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.