1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
padilas [110]
3 years ago
15

Given positive integer numInsects, write a while loop that prints that number doubled without reaching 100. Follow each number w

ith a space. After the loop, print a newline. Ex: If numInsects = 8, print:
8 16 32 64

#include

using namespace std;

int main() {

int numInsects = 0;

numInsects = 8; // Must be >= 1

while (numInsects < 100) {

numInsects = numInsects * 2;



cout << numInsects << " ";

}

cout << endl;

return 0;

}

So my question is what am I doing wrong?

Computers and Technology
1 answer:
mestny [16]3 years ago
3 0

Answer:

The cout<<numInsects<<""; statement should be placed before numInsects = numInsects * 2;  in the while loop.

Explanation:

Your program gives the following output:

16  32  64  128

However it should give the following output:

8  16  32  64

Lets see while loop to check what you did wrong in your program:

The while loop checks if numInsects is less than 100. It is true as numInsects =8 which is less than 100.

So the body of while loop executes. numInsects = numInsects * 2;  statement in the body multiplies the value of numInsects i.e 8 with 2 and then cout << numInsects << " ";  prints the value of numInsects  AFTER the multiplication with 2 is performed. So 8 is not printed in output but instead 16 (the result of 8*2=16) is printed as the result of first iteration.

So lets change the while loop as follows:

while (numInsects < 100) {

cout << numInsects << " ";

numInsects = numInsects * 2;

Now lets see how it works.

The while loop checks if numInsects is less than 100. It is true as numInsects =8 which is less than 100

So the body of while loop executes. cout << numInsects << " "; first prints the value of numInsects i.e. 8. Next numInsects = numInsects * 2;  multiplies the value of numInsects i.e 8 with 2. So first 8 is printed on the output screen. Then the multiplication i.e. 8*2=16 is performed as the result of first iteration. So now value of numInsects becomes 16.

Next the while loop condition numInsects < 100 is again checked. It is true again as 16<100. Now cout << numInsects << " "; is executed which prints 16. After this, the multiplication is again performed and new value of numInsects becomes 32 at second iteration. This is how while loops continues to execute.

So this while loop stops when the value of numInsects exceeds 100.

You might be interested in
The ability to use various design software such as Adobe Photoshop, InDesign, and Illustrator would be MOST beneficial to which
NikAS [45]
Answer: b
Explanation:
5 0
3 years ago
How can you tell if a motherboard can be watercooled?
ivann1987 [24]
Well your cpu should have a header on it for a pump you can look for close to the cpu it should be little metal rods sticking up where you could connect a fan or a cooling device to!
3 0
4 years ago
Why might it be important to be careful when placing multiple microphones around the same sound source
Softa [21]

Answer:

I think you can get mic feedback from all of the microphones

Explanation:

Let me know if this is the answer you are looking for

8 0
3 years ago
Should spreadsheets be used to maintain a checkbook?<br> YES<br><br> NO
algol [13]

Yes, because it keeps all your data organized and allows you to see the trend and growth of your data by automatically creating different types of charts and graphs to help you visualize your data.

Hope that helps!

7 0
2 years ago
A business that subscribes to a specific computing model has its entire system fully functional within a short time. What benefi
defon

Its quick development

Quick deployment: When a business opts for cloud computing and subscribes to a specific model, its entire system can be fully functional within a short time—even within a few minutes. The amount of time depends on the kind of technology and the business’s computing needs.


7 0
3 years ago
Other questions:
  • When formatting documents, ____ settings determine where text begins on a line in relation to the left margin?
    8·1 answer
  • A set of instructions to increase a programmer’s pay rate by 10 percent is hidden inside an authorized program. It changes and u
    6·1 answer
  • Which of the following is NOT acceptable to use as proof of
    12·2 answers
  • Computer-aided manufacturing (CAM) offers which possible benefits?
    15·2 answers
  • What are some reasons a person might choose to remain anonymous on the Internet?
    12·2 answers
  • We will use linear interpolation in a C program to estimate the population of NJ between the years of the census, which takes pl
    10·1 answer
  • 19. Fair use applies to which of the following situations?
    14·1 answer
  • The space that helps you organize your PowerPoint or Web Page is called ______.
    13·1 answer
  • An intruder with malicious intent breaks into an office and steals a hard drive containing sensitive information about the compa
    5·1 answer
  • A flowchart meeting is a process where members of the team analyze the design piece-by-piece to make sure it meets requirements
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!