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

  Write a c program to print permutation

Given integer n and permutation of numbers 1,2,..n-1, c program to print permutation that lexicograghically precedes given input permutation if permutation is least print input permutation itself

  An expression whose value depends upon the evaluation

Give an example in C++ of an expression whose value depends upon the evaluation order. Show the orders that produce different values and the values they produce. Explain what side effect is the expression contains.

  Write the code

Write a program that allows an instructor to keep a grade book. Each students has scores for exams, homework assignments, and quizzes.

  Program that reads in the average monthly

Write a program that reads in the average monthly rainfall for a city for each month of the year and then reads in the actual monthly rainfall for each of the previous 12 months. The program then prints out a nicely formatted table showing the rainfa..

  Add the correct code to fill the matrix array with the value

Use the file below code and add the correct code to fill the matr2 array with the values that make it the matrix transpose of matr1. There are many examples on the Internet that show how to do this in C++ and what it should look like when it is corre..

  If a class is derived protected

If a class is derived protected from a base class, explain how this affects the inheritance of all public, protected, and private members of the base class by the derived class.

  Reads in a list of integers

Write a program in C++ that reads in a list of integers into and array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should reque..

  Functions to compute stress and strain in steel rod

Write a main function and the following functions to compute the stress and strain in a steel rod of diameter D (inches) and length L (inches) subject to the compression loads P of 10,000 to 1,000,000 pounds.

  1 recursive sorts are easier to write in functional style

1 recursive sorts are easier to write in functional style than others. two recursive sorts are quicksort and mergesort.

  Program that calculates how much a person earns in a month

Write a program that calculates how much a person earns in a month if the salary is one penny the first day, two pennies the second day, four pennies the third day,

  Compares the number of possible sequences

A c++ program that compares the number of possible sequences of the length L that can be generated uinder the following assumptions. Given an alphabet of size N=9. The program must output a formatted list comparing the number of possible sequences..

  The initial data structure with information about a service

Implement a program using function for a defined task. Initial data is read from a file and added from keyboard. Program has to output initial and resulting data.The initial data structure with information about a service:

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