Question

The C++ code below will compile but has a variety of runtime issues. Identify a runtime...

The C++ code below will compile but has a variety of runtime issues. Identify a runtime issue with the program and describe how you might fix the problem.

#include <iostream>
#include <vector>

using namespace std;

//------------------------------------------------------------------------------

int main()
{
        vector<double> temps; // temperatures

        double temp = 0;
        double sum = 0;
        double high_temp = 0;
        double low_temp = 0;

        while (cin >> temp)         // read and put into temps
                temps.push_back(temp);

        for (int i = 0; i < temps.size(); ++i)
        {
                if (temps[i] > high_temp)
                        high_temp = temps[i];  // find high

                if (temps[i] < low_temp)
                        low_temp = temps[i];  // find low

                sum += temps[i];      // compute sum
        }

        cout << "High temperature: " << high_temp << endl;
        cout << "Low temperature: " << low_temp << endl;
        cout << "Average temperature: " << sum / temps.size() << endl;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Below are the issues.

  • need to break the while loop to take input from the user.
  • then need to assign the first element as high_temp and low_temp

Here a new cpp class with name "main.cpp" is created, which contains following code.

main.cpp :

#include <iostream> //header file
#include <vector>
using namespace std;
//main method
int main()
{
vector<double> temps; // temperatures
//declaring variables
double temp = 0;
double sum = 0;
//using while loop reading temperatures
while (temp>=0){ // read and put into temps
cin >> temp;
temps.push_back(temp);//push element in the array
}
temps.pop_back();//removing last element from the array
//assigning first element as high_temp and low_temp
double high_temp = temps[0];
double low_temp = temps[0];
//using for loop checking high_temp and low_temp
for (int i = 0; i < temps.size(); ++i)
{
if (temps[i] > high_temp)
high_temp = temps[i]; // find high

if (temps[i] < low_temp)
low_temp = temps[i]; // find low

sum += temps[i]; // compute sum
}

cout << "High temperature: " << high_temp << endl;//print high temperature
cout << "Low temperature: " << low_temp << endl;//print low temperature
cout << "Average temperature: " << sum / temps.size() << endl;//Average temperature
}

======================================================

Output : Compile and Run main.cpp to get the screen as shown below

Screen 1 :main.cpp

10 20 25 -10 High temperature: 25 Low temperature: 2 Average temperature: 14.25 ...Program finished with exit code 0 Press EN

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
The C++ code below will compile but has a variety of runtime issues. Identify a runtime...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • Find and fix the errors in this C++ code: * This program illustrates a variety of...

    Find and fix the errors in this C++ code: * This program illustrates a variety of common loop errors. * Fix the errors in each section. */ #include <iostream> using namespace std; int main() { cout << "Welcome to Loop World" << endl; // SECTION I: update comment below on how you fixed this section's code, and tests run // FIX = // TESTS: cout << endl; cout << "******************" << endl; cout << "Section I" << endl; cout <<...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • C++ Write a function so that the main() code below can be replaced by the simpler...

    C++ Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). Original main(): int main() { double milesPerHour; double minutesTraveled; double hoursTraveled; double milesTraveled; cin >> milesPerHour; cin >> minutesTraveled; hoursTraveled = minutesTraveled / 60.0; milesTraveled = hoursTraveled * milesPerHour; cout << "Miles: " << milesTraveled << endl; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using...

  • Make the function take in the variables by reference #include <iostream> using namespace std; //Make the...

    Make the function take in the variables by reference #include <iostream> using namespace std; //Make the function take in the variables by reference void add_to_first_and_reset(int total, int to_add){    total += to_add; to_add = 0; } int main(){ int sum = 0, temp = 0;    for(int i = 0; i < 10; i++){ cin >> temp; add_to_first_and_reset(sum,temp); cout << sum << " " << temp << endl; }    }

  • Question 1 Let's consider the following code * * * * Compile and fix all errors No test cases are...

    Please answer in C++ ONLY, and please fill all comments elaboratively. Question 1 Let's consider the following code * * * * Compile and fix all errors No test cases are required for this question. Run and provide a single screenshot showing the output. Complete the missing comments to explain the action performed by each statement highlighted in red. The first comment is given as an example. Copy and paste the source code with the comments filled out in your...

  • Write a c++ code into the given code  to find composite numbers from the given random number...

    Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() {    srand(time(NULL)); int size_of_list = 0; // the number of random...

  • How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...

    How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...

  • ****I'm trying to compile this code, however, the answer still shows "0". This the formula I'm...

    ****I'm trying to compile this code, however, the answer still shows "0". This the formula I'm trying to use: (num1!) / ((num2!)(num1 - num2)! ) ***** (C++) FOR INSTANCE; Big Number = 20 and Small Number = 3 #include <iostream> using namespace std; int factorial(int num1, int num2){ unsigned long long answer = 0; unsigned long long top = 1; unsigned long long bottom = 1; unsigned long long bottom2 = 1; unsigned long long bottom_3 = 1; int num3...

  • Re-type the code and fix any errors. The code should convert non-positive numbers to 1. if...

    Re-type the code and fix any errors. The code should convert non-positive numbers to 1. if (userNum > 0) cout << "Positive." << endl; else cout << "Not positive, converting to 1." << endl; userNum = 1; cout << "Final: " << userNum << endl; answer: #include <iostream> using namespace std; int main() { int userNum; cin >> userNum; /* Your solution goes here */ return 0; }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT