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 corresponding if else statement

The mathematical operation max(x,y,w,z) can be represented by using the conditional expression operator, as in:  max = (x > y && x > z && x > w) ? x : ((y > z && y > w) ? y : ((z > w) ? z : w))  Write a corresponding if else statement that is equival..

  Stability in an ever-changing technology world

Where do you think C++ and coding games in C++ is headed in the next five years? How about in the next 10 years? Do you think there are any significant improvements that need to be made to the code to ensure its stability in an ever-changing t..

  Write a c function to convert gallons-quarts-pints and cups

Write a C function named liquid() that is to accept an integer number and the addresses of the variables gallons, quarts, pints, and cups.

  We would like to use linear interpolation

Assume that we would like to use linear interpolation to determine the coefficient of lift for additional flight-path angles that are between -4 degrees and 21 degress. write a program that allows the user to enter the data for two points and a.

  Take the code you wrote for the ford-fulkerson algorithm

Take the code you wrote for the Ford-Fulkerson algorithm for the maximum flow on a directed graph with capacities, and implement scaling on top of this code. You write a function

  State a program that reads an integer from the keyboard

Write a program that reads an integer from the keyboard, and prints the integer to the screen. You must write a function to do this task. After the integer is printed, you must call a function (that you write) which asks the user to enter a ‘Y' or..

  Takes a string containing a full name

Write a program that takes a string containing a full name and outputs each part of the name separately with its length. The name should be in the form of first, middle, and last name, separated from each other by a single space. For example, if the ..

  Evaluates statements of an expression language

Completing and extending the C++ program that evaluates statements of an expression language.

  Allows the user to enter the coefficients for a system

Create a graphical (not text-based) Visual C++ program that allows the user to enter the coefficients for a system of four equations with four unknowns. Include a button that when clicked, will solve the system of equations and display the results in..

  Use numbers 0 through 23 to represent hours

Write a C function seconds_since_jan1() that takes the time as six integer arguments (year, month, day, hour,minute, and second) and returns the number of seconds since the beginning of the year. Use numbers 1 through 12 to represent months. Use numb..

  Write a pseudocode statement

Assume that a program has two string variables named str1 and str2. Write a pseudocode statement that assigns an all uppercase version of str1 to the str2 variable. is str.add(str1,str2) part of it?

  Prints the row sums and the column sums

Write a program that reads a 5 x 5 array of integers and then prints the row sums and the column sums

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