Question

In: Computer Science

trace through the program and what the output would be. If there is an error explain...

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

Solutions

Expert Solution

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  


Related Solutions

Trace through the program and what the outputs will be. If there is an error explain...
Trace through the program and what the outputs will be. If there is an error explain what must be fixed and continue to trace through. #include <iostream> using namespace std; void beans(int y, int& n, int size); void spam(int& n, int& y); int main(){ int m = 7; int n = 4; cout<<"m is "<<m<<" n is "<<n<<endl; beans(n, m, 3); cout<<"m is "<<m<<" n is "<<n<<endl; spam(m, n); cout<<"m is "<<m<<" n is "<<n<<endl; beans(m, 2, n); cout<<"m is...
1. Consider the program below. a. What would the output be in a language with static...
1. Consider the program below. a. What would the output be in a language with static scoping rules? Explain your answer. b. What would the output be in a language with dynamic scoping rules? Explain your answer. int x; //global int main() {    x = 11;    fun1();    fun2(); } void fun1() {    int x = 19;    fun3(); } void fun2() {    int x = 4;    fun3(); } void fun3() {    print(x); }
Trace the pathway of light through the eye to the retina, and explain how light is...
Trace the pathway of light through the eye to the retina, and explain how light is focused for distant and close vision.
Describe what would be a type I error and what would be a type II error:...
Describe what would be a type I error and what would be a type II error: A restaurant claims more than 42% of its new patrons return at least once within a month.
die's job is to output an error message alarmingly and terminate the program. You may copy...
die's job is to output an error message alarmingly and terminate the program. You may copy my function definition, or use your own: // The following 4 lines should be present if we have a die function: #include <iostream> // cout, endl #include <string> // string #include <cstdlib> // exit, EXIT_FAILURE using namespace std; bool die(const string & msg){ cout <<"Fatal error: " <<msg <<endl; exit(EXIT_FAILURE); } 4. Use rand to randomly choose one of the five operators * /...
What would happen if there were an error in the DNA complementary base pairing? explain.
What would happen if there were an error in the DNA complementary base pairing? explain.
Explain what the difference is between a process and a program. Explain what would happen if...
Explain what the difference is between a process and a program. Explain what would happen if a parent process exits immediately after calling the fork() system call.
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
Identify the directives and statements in the following program. What is the output of the program?...
Identify the directives and statements in the following program. What is the output of the program? [2 points] #include <stdio.h> int main (void) { printf(“Parkinson’s Law: \n Work expands so as to \t”); If i and j are positive integers, does (-i)/j always have the same value as –(i/j)? Justify your answer. [2 points] printf(“fill the time \n”); Supply parenthesis to show how a C compiler would interpret the following expressions: [2 points] a * b – c *d +...
What is the output produced by each statement below? If the statement causes an error, comment...
What is the output produced by each statement below? If the statement causes an error, comment it out and write either "compiler error" or "runtime error" as appropriate. For each line make your guess and then move the open comment down a line to test your theory. public class Main { public static void main(String[] args) { First var1 = new Second(); First var2 = new Third(); First var3 = new Fourth(); Second var4 = new Third(); Object var5 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT