In: Computer Science
You MUST use VECTORS in this lab. Do NOT use ARRAYS.
Write code in C++ with //comments . Please include a screen shot of the output
Part 4: Calorie Counting
Specifications:
Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over one week, the average calories consumed per day, the day that had the greatest number of calories consumed and the day that had the least number of calories consumed.
Create 4 functions in addition to your main function
Here is the code:
# include <iostream>
# include <vector>
# include <utility>
using namespace std;
vector<int> getInput() {
vector<int> calories;
for (int i=0; i<7; i++) {
//read input
cout << "Please enter calories consumed on day " << i+1
<< ": ";
int n;
cin >> n;
//add to vector
calories.push_back(n);
}
return calories;
}
//use pair to return two values
pair<int, double> getSumAvg(vector<int> calories)
{
int length = calories.size();
int sum = 0;
//add all values to get the sum
for (int i=0; i<length; i++) {
sum += calories[i];
}
return make_pair(sum, (double) sum/length);
}
int getMax(vector<int> calories) {
int max = calories[0];
int index = 0;
int length = calories.size();
for (int i=1; i<length; i++) {
//if current element more than max
if (calories[i] > max) {
//update max
max = calories[i];
index = i;
}
}
return index;
}
int getMin(vector<int> calories) {
int min = calories[0];
int index = 0;
int length = calories.size();
for (int i=1; i<length; i++) {
//if current element less than min
if (calories[i] < min) {
//update min
min = calories[i];
index = i;
}
}
return index;
}
int main() {
vector<int> calories = getInput();
pair<int, double> sumAvg = getSumAvg(calories);
int maxIndex = getMax(calories);
int minIndex = getMin(calories);
cout << "Total calories consumed " << sumAvg.first
<< " with average " << sumAvg.second <<
endl;
cout << "Maximum consumed was " << calories[maxIndex]
<< " on day " << maxIndex + 1 << endl;
cout << "Minimum consumed was " << calories[minIndex]
<< " on day " << minIndex + 1 << endl;
return 0;
}
Here are some outputs:
Please enter calories consumed on day 1: 2020
Please enter calories consumed on day 2: 1800
Please enter calories consumed on day 3: 1700
Please enter calories consumed on day 4: 2100
Please enter calories consumed on day 5: 1900
Please enter calories consumed on day 6: 1850
Please enter calories consumed on day 7: 2000
Total calories consumed 13370 with average 1910
Maximum consumed was 2100 on day 4
Minimum consumed was 1700 on day 3
Please enter calories consumed on day 1: 2000
Please enter calories consumed on day 2: 2100
Please enter calories consumed on day 3: 2200
Please enter calories consumed on day 4: 1900
Please enter calories consumed on day 5: 1800
Please enter calories consumed on day 6: 1700
Please enter calories consumed on day 7: 1800
Total calories consumed 13500 with average 1928.57
Maximum consumed was 2200 on day 3
Minimum consumed was 1700 on day 6
Comment in case of any doubts.