LO: (Analyze) Students will fix a loop that runs forever. main.cpp O New #include 2 using namespace std; This program runs the countdown sequence for a rocket launch. However, it seems 3. to loop infinitely. Fix the program so it counts down and terminates properly. 4- /* * Counts down to blastoff, starting from a given number. 6. */ 7- int main() { int countdown 8 -10; cin >> countdown; while (countdown != 0) { cout <« countdown << 9. 10 - 11 <« endl; 12 countdown--; 13 cout « "Blast off!" << endl; turn 0; 14 15 16 }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
**Instructional Objective:** 

Students will learn to analyze and fix a loop that runs indefinitely. 

**Task Description:**

This exercise involves a C++ program designed to simulate the countdown sequence for a rocket launch. However, the current implementation causes the program to loop indefinitely. Your task is to identify and correct the issue so that the program correctly counts down and terminates.

**Code Explanation:**

```cpp
#include <iostream>
using namespace std;

/*
 * Counts down to blastoff, starting from a given number.
 */
int main() {
    int countdown = -10;
    cin >> countdown;
    while (countdown != 0) {
        cout << countdown << "..." << endl;
        countdown--;
    }
    cout << "Blast off!" << endl;
    return 0;
}
```

**Analysis:**

1. **Initialization and Input:**
   - The variable `countdown` is initialized to `-10`.
   - User input is expected to provide the actual countdown starting number.

2. **Loop Structure:**
   - The `while` loop continues as long as `countdown` is not equal to `0`.
   - Inside the loop, the current value of `countdown` is printed, followed by decrementing `countdown`.

3. **Problem:**
   - If `countdown` is not properly set by user input or there is no input, the loop will execute indefinitely since the starting value of `-10` will never reach `0`.

**Solution Steps:**

- Ensure that valid input is captured in the `countdown` variable.
- Verify that the logic inside the loop accurately decreases the countdown to zero.
- Consider handling edge cases, such as non-numeric input or negative countdown starting values.

**Expected Outcome:**

Once corrected, the program should accept a starting number, count down to zero, and then print "Blast off!" indicating successful termination.
Transcribed Image Text:**Instructional Objective:** Students will learn to analyze and fix a loop that runs indefinitely. **Task Description:** This exercise involves a C++ program designed to simulate the countdown sequence for a rocket launch. However, the current implementation causes the program to loop indefinitely. Your task is to identify and correct the issue so that the program correctly counts down and terminates. **Code Explanation:** ```cpp #include <iostream> using namespace std; /* * Counts down to blastoff, starting from a given number. */ int main() { int countdown = -10; cin >> countdown; while (countdown != 0) { cout << countdown << "..." << endl; countdown--; } cout << "Blast off!" << endl; return 0; } ``` **Analysis:** 1. **Initialization and Input:** - The variable `countdown` is initialized to `-10`. - User input is expected to provide the actual countdown starting number. 2. **Loop Structure:** - The `while` loop continues as long as `countdown` is not equal to `0`. - Inside the loop, the current value of `countdown` is printed, followed by decrementing `countdown`. 3. **Problem:** - If `countdown` is not properly set by user input or there is no input, the loop will execute indefinitely since the starting value of `-10` will never reach `0`. **Solution Steps:** - Ensure that valid input is captured in the `countdown` variable. - Verify that the logic inside the loop accurately decreases the countdown to zero. - Consider handling edge cases, such as non-numeric input or negative countdown starting values. **Expected Outcome:** Once corrected, the program should accept a starting number, count down to zero, and then print "Blast off!" indicating successful termination.
### Understanding Crop Yield Analysis Using Multi-Dimensional Data

#### Learning Objectives
- **LO (1)**: Students will compute a value from multi-dimensional data.
- **LO (2)**: Students will find the optimal value in a collection.

#### Context
A group of farmers are utilizing drone technology to gather data on corn yields across various plots. These plots are structured into rows, with each row containing an equal number of plots. The rows and plots are numbered starting at zero, with row 0 being nearest to the farm's house. 

The goal is to identify which row has the highest corn yield. If multiple rows have the same yield, the row closest to the farm house should be selected.

#### Methods for Accessing Crop Data
```cpp
void loadCropData()
int getRowCount()
int getRowLength()
int getCropCount(int row, int plot)
```

- **`loadCropData()`**: Call this method once at the beginning to load your data.
- **`getRowCount()`**: Returns the total number of rows.
- **`getRowLength()`**: Returns the number of plots within each row.
- **`getCropCount(r, p)`**: Returns the yield of corn (in bushels) for a specific plot `p` in row `r`.

*Note:* Ensure to include `crops.cpp` to utilize these functions.

#### Usage Notes
Refer to `corn.data` for example data:
- The **first value** is the number of rows.
- The **second value** is the number of plots per row.
- The remaining values represent the yield of each plot, organized first by row then by plot within that row.

By carefully analyzing this structured data, students can compute and determine the row with the highest corn yield efficiently.
Transcribed Image Text:### Understanding Crop Yield Analysis Using Multi-Dimensional Data #### Learning Objectives - **LO (1)**: Students will compute a value from multi-dimensional data. - **LO (2)**: Students will find the optimal value in a collection. #### Context A group of farmers are utilizing drone technology to gather data on corn yields across various plots. These plots are structured into rows, with each row containing an equal number of plots. The rows and plots are numbered starting at zero, with row 0 being nearest to the farm's house. The goal is to identify which row has the highest corn yield. If multiple rows have the same yield, the row closest to the farm house should be selected. #### Methods for Accessing Crop Data ```cpp void loadCropData() int getRowCount() int getRowLength() int getCropCount(int row, int plot) ``` - **`loadCropData()`**: Call this method once at the beginning to load your data. - **`getRowCount()`**: Returns the total number of rows. - **`getRowLength()`**: Returns the number of plots within each row. - **`getCropCount(r, p)`**: Returns the yield of corn (in bushels) for a specific plot `p` in row `r`. *Note:* Ensure to include `crops.cpp` to utilize these functions. #### Usage Notes Refer to `corn.data` for example data: - The **first value** is the number of rows. - The **second value** is the number of plots per row. - The remaining values represent the yield of each plot, organized first by row then by plot within that row. By carefully analyzing this structured data, students can compute and determine the row with the highest corn yield efficiently.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education