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...
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...
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...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files 1. WriteaclassGradeBookcontainingthefollowing: Private attributes: - courseName: a string representing the name of the course. - nbOfStudents: an integer representing the number of students enrolled in the course. The number of students is greater than or equal to 5. - grades: a double dimensional array of integers representing the grades of Test1, Test2 and Final of every student. It should be a dynamic array. Public...
C++ code Write a program to illustrate how to use the temporary class. Your program must...
C++ code Write a program to illustrate how to use the temporary class. Your program must contain statements that would ask the user to enter data of an object and use the setters to initialize the object. Use three header files named main.cpp, temporary.h, and temporaryImp.cpp An example of the program is shown below: Enter object name (rectangle, circle, sphere, or cylinder: circle Enter object's dimensions: rectangle (length and width) circle (radius and 0) sphere (radius and 0) rectangle (base...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based on a problem in the C++ text by Friedman & Koffman: The results of a survey of the households in your township are available for public scrutiny. Each record (struct-type entity) contains input data for one household, including a four-digit integer identification number the annual income for the household the number of household members. Assuming that no more than 25 households were surveyed, write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT