In: Computer Science
trace through the program and what the output would be. If there is an error explain what to change. #include <iostream> using namespace std; int fun(int c, int b); int main(){ int a = 0, b= 5, c = 10; cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; b=fun(a, c); cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; while(b==21){ int a = 3; b = a; cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; } cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; return 0; } int fun(int c, int b){ b = 2*b; c = b+1; cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; return c; }
int fun(int c, int b){
b = 2*b;
c = b+1;
cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; //
‘a’ was not declared in this scope
return c;
}
Correct Code
#include <stdio.h>
#include <iostream>
using namespace std;
int fun(int c, int b);
int main(){
int a = 0, b= 5, c = 10;
cout<<"a is: "<<a<<" b is: "<<b<<" c
is: "<<c<<endl;
b=fun(a, c);
cout<<"a is: "<<a<<" b is: "<<b<<" c
is: "<<c<<endl;
while(b==21){
int a = 3;
b = a;
cout<<"a is: "<<a<<" b is: "<<b<<" c
is: "<<c<<endl;
}
cout<<"a is: "<<a<<" b is: "<<b<<" c
is: "<<c<<endl;
return 0;
}
int fun(int c, int b){
b = 2*b;
c = b+1;
int a=0; // declared
variable a
cout<<"a is: "<<a<<" b is: "<<b<<" c
is: "<<c<<endl;
return c;
}
Output:
a is: 0 b is: 5 c is: 10
a is: 0 b is: 20 c is: 21
a is: 0 b is: 21 c is: 10
a is: 3 b is: 3 c is: 10
a is: 0 b is: 3 c is: 10