Question

In: Computer Science

You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...

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

  • One function should ask the user to enter their calories consumed. You should start with a vector of size 0 and use the push_back() function to enter calories each day.
  • A second function should find the total calories consumed during the entire week and the average calories consumed per day. Both should be displayed in the main function. Think about what you must do to get two values returned to the main function.
  • A third function should find the day that had the largest number of calories consumed. Both the day and the number of calories consumed should be displayed by the main function.   
  • A fourth function should find the day that had the least number of calories consumed. Both the day and the number of calories consumed should be displayed by the main function.   

Solutions

Expert Solution

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.


Related Solutions

You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
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 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Please do not use vectors or any previously posted code Write a C++ program which reads...
Please do not use vectors or any previously posted code Write a C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read line by line until an end-of-transmission (^d) is encountered. You should read the list and represent it in a linked list data...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and other files, loops, conditionals, data types, assignment.   Calculating fuel economy. This program will use exceptions and stream errors to make a robust application that gets the number of miles and gallons each time the user fuels their car. It will put those values into vectors. Once the user wants to quit enter values, it will calculate the fuel economy. Create GetMiles() function that returns...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q...
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q (4) Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the 1st token...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined up horizontally in a row Each locker is numbered from 1 to 100 in sequential order Every locker can be fully closed (open state = 0.00) Every locker can be fully opened (open = 1.00) Every locker can be partially open with any possible value between 0.00 and 1.00 inclusive on both ends A locker cannot ever be more closed than fully closed (open...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if...
C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if we want! Choice either vectors or arrays either one is fine We will implement a classic Inheritance hierarchy. A simple console based interface is all that is needed. Build your classes first, each in their own .h and .cpp files, then test them with the simple main method provided below. Phase 1 : Here is the following set of classes you will implement and...
Do not use arrays and code a program that reads a sequence of positive integers from...
Do not use arrays and code a program that reads a sequence of positive integers from the keyboard and finds the largest and the smallest of them along with their number of occurrences. The user enters zero to terminate the input. If the user enters a negative number, the program displays an error and continues. Sample 1: Enter numbers: 3 2 6 2 2 6 6 6 5 6 0 Largest Occurrences Smallest Occurrences 6 5 2 3. Do not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT