What is the output of the corrected loop

Assignment Help C/C++ Programming
Reference no: EM13685412

Question 1. What is the exact output of the program below? Indicate a blank space in the output by writing the symbol. Indicate a blank line in the output by writing blank line.

#include <iostream.h>
main()
{
int n = 4, k = 2;
cout << ++n << endl;
cout << n << endl;
cout << n++ << endl;
cout << n << endl;
cout << -n << endl;
cout << n << endl;
cout << --n << endl;
cout << n << endl;
cout << n-- << endl;
cout << n << endl;
cout << n + k << endl;
cout << n << endl;
cout << k << endl;
cout << n << k << endl;
cout << n << endl;
cout << " " << n << endl;
cout << " n" << endl;
cout << "\n" << endl;
cout << n * n << endl;
cout << 'n' << endl;
return 0;
}

Questin 2. What is the output when the following code fragment is executed?

int found = 0, count = 5;
if (!found || --count == 0)
cout << "danger" << endl;
cout << "count = " << count <<
endl;

Question 3. Suppose that the following code fragment is executed.

const int LENGTH = 21;

char message[LENGTH];

cout << "Enter a sentence on the line below." << endl;

cin >> message;

cout << message << endl;

Suppose that in response to the prompt, the interactive user types the following line and presses Enter:

Please go away.

What will the output of the code fragment look like?

Question 4. What is the output of the program below?

#include <iostream.h>
main()
{
int n = 3;
while (n >= 0)
{
cout << n * n << endl;
--n;
}
cout << n << endl;
while (n < 4)
cout << ++n << endl;
cout << n << endl;
while (n >= 0)
cout << (n /= 2) << endl;
return 0;
}

Question 5. What is the output when the following code fragment is executed?

int i =5, j =6, k =7, n =3;
cout << i+j*k-k%n << endl;
cout << i / n << endl;

Question 6. What is the output of the program below?

#include <iostream.h>
main()
{
int n;
cout << (n = 4) << endl;
cout << (n == 4) << endl;
cout << (n > 3) << endl;
cout << (n < 4) << endl;
cout << (n = 0) << endl;
cout << (n == 0) << endl;
cout << (n > 0) << endl;
cout << (n && 4) << endl;
cout << (n || 4) << endl;
cout << (!n) << endl;
return 0;
}

Question 8. What is the output when the following code fragment is executed?

char ch;
char title[] = "Titanic";
ch = title[1];
title[3] = ch;
cout << title << endl;
cout << ch << endl; 

Question 9. Suppose that the following code fragment is executed.

const int LENGTH = 21;
char message[LENGTH];
cout << "Enter a sentence on the
line below." << endl;
cin.getline(message,LENGTH,'\n');
cout << message << endl;

a. Suppose that in response to the prompt, the interactive user types the following line and presses Enter:

Please go away.

What will the output of the code fragment look like?

b. Suppose that in response to the prompt, the interactive user types the following line and presses Enter:

Please stop bothering me.

What will the output of the code fragment look like?

Question 10. Suppose that the following code fragment is executed.

const int LENGTH = 21;
char message[LENGTH];
cout << "Enter a sentence on the line below."
<< endl;
int i = 0;
do
{
cin >> message[i];
++i;
}
while (i<LENGTH-1 && message[i]!='\n');
message[i] = '\0'; // NUL char.
cout << message << endl;

a. Suppose that in response to the prompt, the interactive user types the following line and presses Enter:

What will the output of the code fragment look like?

b. Suppose that the statement

cin >> message[i];
is replaced by the statement
cin.get(message[i]);
Now what will the output of the code fragment look like if, in response to the prompt, the interactive user types the following line and
presses Enter?

Please go away.

Question 11. The nested conditional statement shown below has been written by an inexperienced C/C++ programmer.

The behavior of the statement is not correctly represented by the formatting.

if (n < 10)
if (n > 0)
cout << "The number is positive.
<< endl;
else
cout << "The number is _________."
<< endl;
a. What is the output of the statement if the variable n has the value 7 ? If n has the value 15 ?
If n has the value -3 ?

b. Correct the syntax of the statement so that the logic of the corrected statement corresponds to the formatting of the original statement. Also, replace the blank with an appropriate word or phrase.

c. Correct the formatting of the (original) statement so that the new format reflects the logical behavior of the original statement. Also, replace the blank with an appropriate word or phrase.

12. The loop shown below has been written by an inexperienced C/C++ programmer. The behavior of the loop is not correctly represented by the formatting.

int n = 10;
while (n > 0)
n /= 2;
cout << n * n << endl;

a. What is the output of the loop as it is written?

b. Correct the syntax of the loop so that the logic of the corrected loop corresponds to the formatting of the original loop. What is the output of the corrected loop?

c. Correct the formatting of the (original) loop so that the new format reflects the logical behavior of the original loop.

13. Rewrite the following code fragment so that it uses a "do...while..." loop to accomplish the same task.

int n;
cout <<"Enter a non-negative integer: ";
cin >> n;
while (n < 0) {
cout <<"The integer you entered is
negative." << endl;
cout<<"Enter a non-negative integer: ";
cin >> n; }

Reference no: EM13685412

Questions Cloud

Specializes in online security software development : A company that specializes in online security software development wants to have $85 million available in three years to pay stock dividends. How much money must the company set aside now in an account that earns interest at a rate of 8% per year, co..
Systems is a manufacturer of vertical compactors : V-Tek Systems is a manufacturer of vertical compactors, and it is examining its cash flow requirements for the next five years. The company expects to replace office machines and computer equipment at various times over the 5-year planning period.
When we construct the cubic total variable cost : When we construct the cubic total variable cost, TVC = aQ + bQ2 + cQ3, in order to confirm the theoretical properties, the parameters should satisfy
Presume working hard raises the probability of making sale : Presume you have hired a new worker, unfortunately you do not know if the worker is a shirker or a hard worker. Presume working hard raises the probability of making a sale from 40% to 80% (thus raises the probability of making a commission C by the ..
What is the output of the corrected loop : What is the output of the loop as it is written and correct the syntax of the loop so that the logic of the corrected loop corresponds to the formatting of the original loop. What is the output of the corrected loop?
Find the length of the coastlines of the south : Find the length of the coastlines of the South and North Carolina
Thompson mechanical products are planning : Thompson Mechanical products are planning to set aside $150,000 now for possibly replacing its large synchronous refiner motors whenever it becomes necessary. If the replacement isn`t required for 7 years, how much will the company have in its invest..
Maximize the profit by employing the amount of input : A firm can choose the optimal usage of input to maximize the profit by employing the amount of input where
What was the rate of return on the investment : A design-build engineering firm completed a pipeline project wherein the company realized a profit of $2.3 million in one year. If the amount of money the company had invested was $6 million, what was the rate of return on the investment?

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Create program that uses functions and reference parameters

Create program that uses functions and reference parameters, and asks user for the outside temperature.

  Write a program using vectors and iterators

Write a program using vectors and iterators that allows a user to maintain a personal list of DVD titles

  Write the code required to analyse and display the data

Calculate and store the average for each row and column. Determine and store the values for the Average Map.

  Write a webservices application

Write a webservices application that does a simple four function calculator

  Iimplement a client-server of the game

Iimplement a client-server version of the rock-paper-scissors-lizard-Spock game.

  Model-view-controller

Explain Model-View-Controller paradigm

  Design a nested program

How many levels of nesting are there in this design?

  Convert celsius temperatures to fahrenheit temperatures

Write a C++ program that converts Celsius Temperatures to Fahrenheit Temperatures.

  Evaluate and output the value in the given base

Write C program that will input two values from the user that are a Value and a Base with which you will evaluate and output the Value in the given Base.

  Design a base class shape with virtual functions

Design a base class shape with virtual functions

  Implementation of classes

Implementation of classes Chart and BarChart. Class barChart chould display a simple textual representation of the data

  Technical paper: memory management

Technical Paper: Memory Management, The intent of this paper is to provide you with an in depth knowledge of how memory is used in executing, your programs and its critical support for applications.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd