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...

  • 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(){...

  • 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 <<...

  • 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...

  • 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;...

  • The following code will not compile. Fix it by inserting one line of code. Do not...

    The following code will not compile. Fix it by inserting one line of code. Do not change anything else. Hint: copy and paste the code below into your answer box, then make the required addition. Also include a comment indicating the line of code that you inserted, and why that line of code is required include <iostream> using namespace std int mainO cout <<"The word of the day is: sayword 0 return 0 void sayWordO f cout くく "WORD!";

  • C++ Fix the errors in the following code. (Not all errors are syntax related) #include <iostream>...

    C++ Fix the errors in the following code. (Not all errors are syntax related) #include <iostream> using namespace std; int main() { //Part A int numA numA = 10; cout << numA << end; /* Part B */*/ int numB = numA; cin >> usrInput; int usrInput = 0; numB = numB + usrInput; cout << numB <"/n";    /* Part C */ int numC = 10000000000; cout << numC << endl; return 0; }

  • Modify this code to display the message "That's hot!" if the temperature entered is greater than...

    Modify this code to display the message "That's hot!" if the temperature entered is greater than 90 degrees Farenheit or equivalent Celsius, or the message "That's cold!" if the temperature entered is less than -10 degrees Farenheit or equivalent Celsius. And to show one in between where it doesn't show any message. #include <iostream> #include <iomanip> using namespace std; // a temperature conversion program int main() { char tempType; double temp, fahren, celsius; cout << "Enter the temperature to be...

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
ADVERTISEMENT