Question

In: Computer Science

Assume m=5 and n=2 in each case. Answer the questions.. What are the output of the...

Assume m=5 and n=2 in each case. Answer the questions..

What are the output of the codes:

1) m*=n++; cout<<(--m)++<<","<<--n<<'\n';

2) Values of m and n after execution of codes: m*=n++;

3) What values of m, n will be after the statement: m+=--n; ?

4) What values of m and n will be after the statement: m*=n; ?

5) What values of m and n will be after the statement: m /=++n; ?

Solutions

Expert Solution

The output of the following programs are as follows:

1. 9,2

It is beacause initially the value of m = 5 and then m = m*n++

                                                                         -> m = 5*2++

                                                                                  = 10

        This is use then increment operator as ++ comes after the operand. Now, we execute the statement --m which means that decrement and then use. So, the current value of m is 10 which will be decremented to 9 due to the operator --m.

Regarding the second output after 9 that is 2. It comes because --n is in the pattern "--n", Now in the first statement that is

m*=n++, the value of n is 2 because it is post increment use and increment. Now, after this --n comes. As a result, the value of n will be decremented and then it will be suddenly decremented again due to pre-increment operator.

In other words, m*=n++;

                      The value of m is 10 and n is 2

                      cout<<(--m)++;   The value of m is 9 (pre increment ie, increment comes first).

                      Also, cout<<--n<<'\n';   The value of n is 2 (because now n is incremented due to n++ in the statement [ m*=n++] to 3 and again decremented to 2 [due to --n] in the statement.

2. values of m and n after execution of codes m*=n++ is

   m =   5*2   =   10

    because initial value of m is 5 and n will not increment and remain as 2 in this step .

The value of n is 2 itself.

3. The value of m and n after the statement m+=--n; is as follows:

    Let us say the current (initial value of m and n is 5 and 2 respectively)

     m = m+--n;

          = 5+(--2)

= 5 + 1

= 6 ,

The value of n is n=n-1=2-1=1

m=6 and n=1

because the value of n will be decremented ( decrement and use pre increment strategy).

4. The value of m and n after the statement m*=n; is as follows:

   Let us say the initial values of m and n are 5 and 2 respectively.

   m = m*n;

        = 5*2

       =   10

   n = 2

m = 10 and n =2

5. The values of m and n after statement m /=++n is as follows:

   Let us say the initial values of m=5 and n=2. Then,

   m = m/++n

       = 5/ (++2)

       = 5/ (2+1)     /* Pre Increment ie increment and then use */

        = 5/3

        = 1.6 , but

   if m is declared as int then the value of m is 1

if m is declared as float, then the value of m is 1.6

   However, the value of n is 3 ( n=n+1).

In general, questions from 1 to 5 is based on the concepts of post and pre increment/decrement operators.

The concept is this, in case of pre increment/decrement operator, initially the operand will be performed by the operation and the we will use it. As far as post increment/dcrement is concerned, the operand will be used first and then only increment or decrement will be done on the operand. You just understand these concepts.

       

     


Related Solutions

Illustrate each of the following scenarios. In each case, assume that the corresponding output market is...
Illustrate each of the following scenarios. In each case, assume that the corresponding output market is competitive. a. Equilibrium in a perfectly competitive labor market (be sure to draw both a market and a firm graph). b. Equilibrium in a monopsony labor market. c. Equilibrium in a monopoly labor market (for example, a labor market with a union maximizing economic rent).
Assume a normal distribution of the form N(100, 100), answer the following questions: What proportion of...
Assume a normal distribution of the form N(100, 100), answer the following questions: What proportion of the distribution falls within 1 standard deviation of the mean (e.g., within -1 and 1 standard deviation)? What is the probability of a single draw from that distribution has a value greater than 115? What is the range that captures the middle 95% of the population distribution? If I randomly sample 10 observations from this distribution and calculate a mean, what is the probability...
Identify all allowable combinations of quantum numbers for an electron. n=3,n=3, ?=2,?=2, m?=2,m?=2, ms=−12ms=−12 n=5,n=5, ?=4,?=4,...
Identify all allowable combinations of quantum numbers for an electron. n=3,n=3, ?=2,?=2, m?=2,m?=2, ms=−12ms=−12 n=5,n=5, ?=4,?=4, m?=−1,m?=−1, ms=−12ms=−12 n=3,n=3, ?=−2,?=−2, m?=2,m?=2, ms=+12ms=+12 n=6,n=6, ?=6,?=6, m?=1,m?=1, ms=+12ms=+12 n=4,n=4, ?=3,?=3, m?=4,m?=4, ms=+12ms=+12 n=2,n=2, ?=0,?=0, m?=0,m?=0, ms=−1
Read the case study, then answer the questions that follow. Case study (questions 5-9) Susannah’s husband...
Read the case study, then answer the questions that follow. Case study (questions 5-9) Susannah’s husband Andrew has found out that Joe has been offering lifts to Susannah and the children. He has made a formal complaint to Joe’s supervisor, Betsy. He has threatened to stop his wife and children attending the centre if Joe continues to work with Susannah and has requested a female worker to support his family.   Susannah has requested a female support worker, saying she would...
Q 2) Read the case and answer the questions below.                                  &
Q 2) Read the case and answer the questions below.                                        (Marks: 10)     Case: Toyota Motor Corporation is a Japanese multinational automotive manufacturer headquartered in Toyota, Aichi, Japan. Recently a conflict arises from the management styles of two managers, Ralph and George, who head two different departments in the same organization. Ralph’s problem with George is that the latter is taking staff from his department without his approval. On the other hand, George feels that he is doing the right thing...
Fill in each blank with the correct answer/output. Assume each statement happens in order and that...
Fill in each blank with the correct answer/output. Assume each statement happens in order and that one statement may affect the next statement. Hand trace this code instead of using your IDE. String s = "abcdefghijklmnop"; ArrayList r = new ArrayList(); r.add("abc"); r.add("cde"); r.set(1,"789"); r.add("xyz"); r.add("123"); Collections.sort(r); r.remove(2); The first index position in an array is __________. System.out.print( s.substring(0,1) ); // LINE 2 System.out.print( s.substring(2,3) ); // LINE 3 System.out.print( s.substring(5,6) ); // LINE 4 System.out.print( r.get(0) ); // LINE...
Assume that aa = "11", bb = "22", m = 5 and n = 7. How...
Assume that aa = "11", bb = "22", m = 5 and n = 7. How do the following two statements differ? System.out.println(aa + bb + m + n); System.out.println(m + n + aa + bb); Explain your answers in details. Only clear explanations will be given marks.
Based on the five case studies, answer the questions in your own words for each case...
Based on the five case studies, answer the questions in your own words for each case study, using complete sentences, and providing examples, if applicable. Case Study 1 During an appendectomy, the patient, a 16-year-old male, exhibits decreased oxygen saturation, hypotension, decreased breath sounds, and increased airway pressures during ventilation. What is most likely happening with this patient? What should you do as a surgical tech in the scrubbed role? What will the anesthesia provider most likely do? Case Study...
1) Use Python to answer the below questions. a) What is the output of the following...
1) Use Python to answer the below questions. a) What is the output of the following python code?                         def function(x):                              return x * 5                        print(func(7) * func(5)) b) What is the output of the following code fragment?     required_course = "Programming", "Security", "Cybersecurity"         a,b,c = required_course         print(b)
This is for a research paper for each of the below topics answer the 5 questions  ...
This is for a research paper for each of the below topics answer the 5 questions   For each topic do you anticipate using an experimental or a non-experimental approach in your research? Explain the basis for your choice. for each topic state a research hypothesis, purpose, or question for the research you are planning. for each topic Name the major variables you will be studying. Define each variable for each topic are you  conducting “qualitative” or “quantitative” research? Explain the basis...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT