Question

In: Computer Science

Can someone please explain the following program to me? I have ran it, but do not...

Can someone please explain the following program to me? I have ran it, but do not understand how the answers are being derived.

#include<iostream>
using namespace std;
int num1 = 1;
int quiz(int num)
{
static int n1 = 10;
int n2 = 20;
n1--;
n2++;
num1++;
if (num == 1)
return num1;
else if (num < 3)
return n1;
else
return n2;
} // quiz
main()
{
cout << quiz(num1) << endl;
cout << quiz(num1) << endl;
cout << quiz(1) << endl;
cout << quiz(num1) << endl;
} // main

Solutions

Expert Solution

In this code there is function i.e quiz function. This function is called 4 times in the main method. Let us see one by one what happens on every function call.

This Table shows how the program gets executed:

Function call

no.

Num

N1

N2

Num1

N1--

N2++

Num1++

If(Num==1)

Return num1

Elseif(Num<3)

Return n1

Else

Return n2

Output

1.quiz(num1)

1

10

20

1

9

21

2

1==1

True

return 2

2

2.quiz(num1)

2

9

20

2

8

21

3

False

2<3

True

Return 8

8

3.quiz(1)

3

8

20

1

7

21

4

1==1

True

return 4

4

4.quiz(num1)

4

7

20

4

6

21

5

False

False

return 21

21

Explanation for the table is given below:

  

1st function call quiz(num):

Here num1 =1 ,So this value is given to the function.

Now inside the function

n1=10 (this is a static variable so it creates the copy i.e It has one memory address and if the value of n1 is changed then during next function call it will use that changed value )

n2=20(This is not static , so for every function call it will remain same)

Now ,the value of n1 =9 (As it is decremented by 1)

Value of n2=21(As it is incremented by 1)

num1=2 (As it is incremented by 1 )

Condition check is If(num==1) (Value of num1 will be the one which is passed through the function i.e 1)

So ,(1==1) which is True  

Now it will return the value of num1 =2 (The incremented value)

For the 1st function call we get value as 2.

2nd function call quiz(num):

Here num1 = 2 ,So this value is given to function.(We got num1 =2 after incrementing it in the 1st function call).

Now inside the function

n1=9 (As it is static so it stores the previous value )

n2=20 (As it is not static so it will start from 20 , not from 21)

Now the value of n1=8(decremented by 1)

Value of n2=21(Incremented by 1)

num1=3(Incremented by 1)

Condition check is If(num==1) (Value of num1 will be the one which is passed through the function i.e 2)

So,The if condition is not True

It will check the else if (num<3)

So, (2<3) which is True.

Now it will return the value of n1=8 (The one which we got after decrementing)

For the 2nd function call we get value as 8.

3rd function call quiz(1):

Here 1   is given  to function.

Now inside the function

n1 = 8(As it is static so it stores the previous value )

n2=20(As it is not static so it will start from 20 , not from 21)

num1=3(We got num1 =3 after incrementing it in the 2nd function call).

Now the value of n1=7(decremented by 1)

Value of n2=21(Incremented by 1)

num1=4(Incremented by 1)

Condition check is If(num==1) (Value of num will be the one which is passed through the function i.e 1)

So ,(1==1) which is True .

Now it will return the value of num1 =4 (The incremented value)

For the 3rd  function call we get value as 4.

4th function call quiz(num1):

Here num1 = 4 ,So this value is given to function.(We got num1 =4 after incrementing it in the 3rd function call)

Now inside the function

n1 = 7(As it is static so it stores the previous value )

n2=20(As it is not static so it will start from 20 , not from 21)

num1=4(We got num1 =4 after incrementing it in the 3rd function call).

Now the value of n1=6(decremented by 1)

Value of n2=21(Incremented by 1)

num1=5(Incremented by 1)

Condition check is If(num==1) (Value of num1 will be the one which is passed through the function i.e 4)

So,The if condition is not True

It will check the else if (num<3)

So, else if condition is not True

Now, it will go to the else part and the return the value n2=21

For the 4th function call we get value as 21.

Final Output:

2

8

4

21


Related Solutions

Can someone please explain the edgeworth box to me by typing in a way i can...
Can someone please explain the edgeworth box to me by typing in a way i can see and understand? what determines the contract curves path?
Can someone please walk through this program for me and explain out the outputs are generated....
Can someone please walk through this program for me and explain out the outputs are generated. #include <iostream> #include <iomanip> using namespace std; const int size = 5; int fun(int arz[size], int jump) { int i, j = 0; for(i = 0; i < size; i += jump) j += arz[i]; return j; } main() { int arx[size] = {1, 2, 4, 8, 16}; int ary[size] = {10, 20, 40, 80, 160}; cout << fun(arx, 1) << endl; cout <<...
Can someone please explain how to do the steps for this? I already have part a...
Can someone please explain how to do the steps for this? I already have part a completed but I need help with b-e. Thank you. Problem 1: (a) What is spurious regression? Explain. (b) Assuming , randomly generate 1000 observations of variables X and Y using the following equations: Report the graphs of X and Y. (c) Run the regression: and report the estimated results. (d) What did you expect about the magnitudes and R-square? How are the estimated values...
Can someone please tell me on how can I have a double and character as one...
Can someone please tell me on how can I have a double and character as one of the items on my list? Thanks! This is what I got so far and the values I am getting are all integers. #pragma once #include <iostream> class Node { public:    int data;    Node* next;       // self-referential    Node()    {        data = 0;        next = nullptr;    }    Node(int value)    {        data...
Please can someone explain this to me. I am stuck at the last two questions Use...
Please can someone explain this to me. I am stuck at the last two questions Use the following information to calculate your answers to questions 10 through 14. Test scores of 10 individuals before and after a training program are shown below. Note: despite the sample size, assume that the sampling distribution of T+ can still be approximated by a normal distribution. Individual Score After the Program Score Before the Program 1 57 59 2 62 57 3 60 60...
Can someone please assist me with this question. The information that I found was not of...
Can someone please assist me with this question. The information that I found was not of help. Discuss how using systems thinking will influence your decision-making. I have to do a full paragraph on this question.
Can someone please explain how to calculate this? I truely do not understand how to do...
Can someone please explain how to calculate this? I truely do not understand how to do theoretical yield... calculate theoretical yield and percent yield for anthracene-9-methylmalemide and N-methylmalemide in water amounts used were 0.070 g of anthracene-9-methanol and 50 mL of water and 0.103 g of N-methylmaleimide. the weight of the product was 0.043 g
Can someone please show me how to do the work on this please. Question The transactions...
Can someone please show me how to do the work on this please. Question The transactions of the Fury Delivery Service are recorded in the general journal below. Instructions: 1. Post the journal entries to the attached general ledger “T” accounts. 2. Prepare a trial balance on the form provided after the “T” accounts. General Journal Date Account Titles and Explanation Debit Credit 2017 Sept. 1 Cash Common Stock (Stockholders invested cash in business) 25,000 25,000 4 Equipment Cash Notes...
Can someone explain to me the program step by step next to each statement in the...
Can someone explain to me the program step by step next to each statement in the program by using comment \\ and do the program using the basic c++ cuz down program looked messy advance? a. Request a five-letter string value from the console. b. If the input string is not a five-letter word, print that the word is not a five-letter word. c. If the input string is a five-letter word,determine if the word is or is not a...
Hello! Can someone please give me an in depth explanation of a SWOT please? I know...
Hello! Can someone please give me an in depth explanation of a SWOT please? I know the basic 4 but it didn’t go well. My SWOT is on Glossier.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT