Using the scenario below, answer the following questions: You are preparing for
ID: 3568874 • Letter: U
Question
Using the scenario below, answer the following questions:
You are preparing for the Boston Marathon. In order to prepare, you need to train for 10 weeks, running an increasing number of miles per week, starting at running at least 2 miles your first week up to 26 miles by week 10.
1.) Initialize the array with the appropriate number of values.
2.) What is the value in the array element when the index contains 2?
3.) What is your list length?
4.) Calculate the sum of the total miles you spent running over the 10 weeks. Calculate the average number of miles you ran.
5.) Write a search to determine whether 4 is on your list.
For reference, here is a tutorial on arrays.
For additional details, refer to the Homework Rubric in the Assignment Guidelines and Rubrics folder.
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
string search(int* l, int j){
for (int i = 0; i < 10; i++){
if (l[i] == j){
return "YES";
}
}
return "NO";
}
int main(){
int* l = new int[10];
l[0] = 3;
int sum = 3;
for (int i = 1; i < 10; i++){
if (i % 2 == 1)
l[i] = l[i-1] + 3;
else
l[i] = l[i-1]+2;
sum += l[i];
}
cout << "The value in the array element when the index contains 2 is " << l[2] << endl;
cout << "Length list is " << 10 << endl;
cout << "The sum of the total miles you spent running over the 10 weeks " << sum << endl;
cout << "Average number of miles you ran " << sum/10.0 << endl;
cout << "Result for searching 4 in the list "<< search(l,4) << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.