Determine the output of the following programs (assuming a using namespace std
and #include <iostream>
are provided at the top). If there are errors, make reasonable changes to fix the error and then determine the output.
int foo(int & a, int & b) {
a = b + 4;
return b;
}
int main() {
int a = 1, b = 2;
cout << foo(a, b);
cout << a << b;
}
int foo(double & a, double b) {
double c = a;
a = c / b;
return static_cast<int>(c) % static_case<int>(b);
}
int main() {
int a = 2, b = 3;
cout << foo(a, b);
cout << a << b;
}
int foo(int & a, int & b) {
a += b;
b /= a;
return a * b;
}
void bar(int & a, int b, int & c) {
c = foo(a, b);
}
int main() {
int a = 1, b = 2, c = 3;
cout << bar(a, b , c);
cout << bar(a, b , c);
cout << a << b << c;
}
void foo(int & num) {
for (; num > 0; --num) {
cout << num << ", ";
}
}
int main() {
int a = 4;
foo(a);
cout << a << endl;
}
Determine the header for each of the following function bodies.
// What goes here?
{
a += 3 * b;
}
// What goes here?
{
for (int i = a; i > 0; --i) {
cout << i * 2 << endl;
}
return a;
}
// What goes here?
{
cout << "Enter a number less than 10: ":
while (true) {
cin >> num;
if (num < 10) {
break
} else {
cout << "Try again: "
}
}
}
Is program 2 suppose to have 2 errors that you are testing students on fixing?