I have this pracitce problem and I cant figure out how to do the 2-4 sections. IN C++ please... I've marked in the code where I need help which I'll include at the bottom of the question. Thanks The scaffold code below tracks monthly precipitation in Denver, Colorado. It accepts four inputs: (1) an integer index representing the month for which a value is to be updated, (2) the new value for that month's precipitation amount, (3) an integer index representing the first month to be considered when calculating total precipitation over multiple months, and (4) an integer index representing the last month to be considered when calculating total precipitation over multiple months. Complete the following tasks: (1) Modify the function VectorSum so that it returns the sum of all elements in theVector with indices between startIndex and endIndex inclusive. (In this context, "inclusive" means that the elements at startIndex and at endIndex are both included in the sum calculation.) (2) Create a function UpdateVector that accepts three parameters: (a) a vector object that is passed by reference, (b) the index of the vector element to be updated, (c) the new value to be placed in the updated element. The function should replace the current value in the index to be updated with the new value. (3) Modify the scaffold code so that the user-provided values for correctionMonth and newPrecipitationAmount are used to update the vector monthlyPrecipitationInDenver. (i.e. the value currently stored in element correctionMonth should be changed to equal the value of newPrecipitationAmount.) You must call your UpdateVector function to do this. (4) Use a cout statement to print the sum of all vector elements with indices between startIndex and endIndex inclusive. You must call the VectorSum function to do this. Sample Input 3 6.0 0 11 Sample Output 37.8 CODE #include <iostream>#include <vector>using namespace std; double VectorSum(vector<double> theVector, unsigned int startIndex, unsigned int endIndex) {double sum = 0;unsigned int i = 0;//TODO: Calculate the sum of all elements in theVector with indices between startIndex and endIndex inclusivefor (i=0; i < theVector.size() ; i++){sum = sum + theVector.at(i);}return sum;} int main() {// create the vector//void UpdateVector (vector<double> monthlyPrecipitationInDenver;monthlyPrecipitationInDenver.push_back(1.0);monthlyPrecipitationInDenver.push_back(1.0);monthlyPrecipitationInDenver.push_back(3.0);monthlyPrecipitationInDenver.push_back(4.3);monthlyPrecipitationInDenver.push_back(6.2);monthlyPrecipitationInDenver.push_back(3.8);monthlyPrecipitationInDenver.push_back(4.5);monthlyPrecipitationInDenver.push_back(4.2);monthlyPrecipitationInDenver.push_back(2.9);monthlyPrecipitationInDenver.push_back(1.9);monthlyPrecipitationInDenver.push_back(2.1);monthlyPrecipitationInDenver.push_back(1.2);int correctionMonth;double newPrecipitationAmount;cin >> correctionMonth >> newPrecipitationAmount;//TODO: replace the vector value at index correctionMonth with the value for newPrecipitationAmountint startIndex;int endIndex;cin >> startIndex >> endIndex;//TODO: output the sum of all elements in monthlyPrecipitationInDenver with indices between startIndex and endIndex inclusive.return 0;}
I have this pracitce problem and I cant figure out how to do the 2-4 sections. IN C++ please... I've marked in the code where I need help which I'll include at the bottom of the question. Thanks
The scaffold code below tracks monthly precipitation in Denver, Colorado. It accepts four inputs: (1) an integer index representing the month for which a value is to be updated, (2) the new value for that month's precipitation amount, (3) an integer index representing the first month to be considered when calculating total precipitation over multiple months, and (4) an integer index representing the last month to be considered when calculating total precipitation over multiple months. Complete the following tasks:
(1) Modify the function VectorSum so that it returns the sum of all elements in theVector with indices between startIndex and endIndex inclusive. (In this context, "inclusive" means that the elements at startIndex and at endIndex are both included in the sum calculation.) (2) Create a function UpdateVector that accepts three parameters: (a) a
Sample Input
3 6.0 0 11
Sample Output
37.8
CODE
#include <iostream>
#include <vector>
using namespace std;
double VectorSum(vector<double> theVector, unsigned int startIndex, unsigned int endIndex) {
double sum = 0;
unsigned int i = 0;
//TODO: Calculate the sum of all elements in theVector with indices between startIndex and endIndex inclusive
for (i=0; i < theVector.size() ; i++){
sum = sum + theVector.at(i);
}
return sum;
}
int main() {
// create the vector
//void UpdateVector (
vector<double> monthlyPrecipitationInDenver;
monthlyPrecipitationInDenver.push_back(1.0);
monthlyPrecipitationInDenver.push_back(1.0);
monthlyPrecipitationInDenver.push_back(3.0);
monthlyPrecipitationInDenver.push_back(4.3);
monthlyPrecipitationInDenver.push_back(6.2);
monthlyPrecipitationInDenver.push_back(3.8);
monthlyPrecipitationInDenver.push_back(4.5);
monthlyPrecipitationInDenver.push_back(4.2);
monthlyPrecipitationInDenver.push_back(2.9);
monthlyPrecipitationInDenver.push_back(1.9);
monthlyPrecipitationInDenver.push_back(2.1);
monthlyPrecipitationInDenver.push_back(1.2);
int correctionMonth;
double newPrecipitationAmount;
cin >> correctionMonth >> newPrecipitationAmount;
//TODO: replace the vector value at index correctionMonth with the value for newPrecipitationAmount
int startIndex;
int endIndex;
cin >> startIndex >> endIndex;
//TODO: output the sum of all elements in monthlyPrecipitationInDenver with indices between startIndex and endIndex inclusive.
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images