This is a C++ program that runs properly. I need to know the details of every co
ID: 3716674 • Letter: T
Question
This is a C++ program that runs properly.
I need to know the details of every code line such as what each line does. For example what they are (declarations, assignments, expressions, selection, repetition etc. , objects that are being used, and the types that are being used such as int, double, string) I want to be able to understand the program. I can run it but I dont know exactly the point of the code functions etc. Please be as detailed as possible. Thank you
1 #include<iostream>
2 #include<vector>
3 using namespace std;
4
5 int main(){
6
7 vector<double>v;
8 double average, min, max, input, sum, i, c_temp;
9 cout << "Enter Fahrenheit temperatures (press Ctrl-Z to finish): ";
10
11 while (cin >> input)
12 v.push_back(input);
13 sum = 0.0;
14 for (size_t i=0; i<v.size(); i++)
15 sum += v[i];
16 average = sum/v.size();
17 for (i=0 ; i<v.size(); i++){
18 if (v[i] > max)
19 max = v[i];
20 else if (v[i] < min)
21 min = v[i];
22 }
23 cout << "The average in degrees Fahrenheit is: " << average << endl;
24 cout << "The minimum temperature in degrees Fahrenheit is: " << min << endl;
25 cout << "The maximum temperature in degrees Fahrenheit is: " << max << endl;
26 cout << "The average in degrees Celsius is: " << (average -32) * (5.0/9.0) << endl;
27 cout << "The minimum temperature in degrees Celsius is: " << (min-32) * (5.0/9.0) << endl;
28 cout << "The maximum temperature in degrees Celsius is: " << (max-32) * (5.0/9.0) << endl;
29
30 return 0;
31 }
Explanation / Answer
here i have written the comments..
================================================================
//these are the headers files which has the defintions of each and every function used in our program.
1 #include<iostream>
2 #include<vector>
//statndard namespace
3 using namespace std;
4
//start of main function
5 int main(){
6
//a vector v is declared of double type
7 vector<double>v;
//variabls are declared whcih will be used in the program
8 double average, min, max, input, sum, i, c_temp;
//ask the user to input temperatures
9 cout << "Enter Fahrenheit temperatures (press Ctrl-Z to finish): ";
10
//while input is received by user
11 while (cin >> input)
//store the number to vector
12 v.push_back(input);
//initialize sum to 0.0
13 sum = 0.0;
//this for loop will iterate each element and calculate the sum
14 for (size_t i=0; i<v.size(); i++)
15 sum += v[i];
//here calculation of average has been done
16 average = sum/v.size();
//this for loop is used to calculate the max and min element in the vector
17 for (i=0 ; i<v.size(); i++)
{
//if v[i] is greater than max then update the value of max
18 if (v[i] > max)
19 max = v[i];
//if v[i] is less than max then update the value of min
20 else if (v[i] < min)
21 min = v[i];
22 }
//here we have displayed the average, min, max etc
23 cout << "The average in degrees Fahrenheit is: " << average << endl;
24 cout << "The minimum temperature in degrees Fahrenheit is: " << min << endl;
25 cout << "The maximum temperature in degrees Fahrenheit is: " << max << endl;
26 cout << "The average in degrees Celsius is: " << (average -32) * (5.0/9.0) << endl;
27 cout << "The minimum temperature in degrees Celsius is: " << (min-32) * (5.0/9.0) << endl;
28 cout << "The maximum temperature in degrees Celsius is: " << (max-32) * (5.0/9.0) << endl;
29
30 return 0;
31 }
==============================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.