// This program averages a set of numbers. #include <iostream> using namespace s
ID: 3638278 • Letter: #
Question
// This program averages a set of numbers.#include <iostream>
using namespace std;
int main()
{
int numCount;
double total = 0.0, average;
cout << "How many numbers do you want to average? ";
cin >> numCount;
for (int count = 0; count < numCount; count++)
{
double num;
cout << "Enter a number: ";
cin >> num;
total += num;
}
average = total / numCount;
cout << "The average is " << average << endl;
return 0;
}
I'm confused as to how this C++ code could potentially crash the program... I'm thinking it has something to do with the division between the double and the int... PLEASE HELP ME AND I WILL REWARD YOU! Thank you very much!!!
Explanation / Answer
division between double and integer is fine. only problem is when user enters numCount = 0. Then program will crash. you can include following code segement to avoid it : . . . cout > numCount; if(numCount == 0) { coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.