Using the scenario below, answer the following questions: You are preparing for
ID: 3661391 • 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.
Explanation / Answer
I am writing the program in C++:
#include <iostream>
using namespace std;
int main(){
double miles[10]; //we will have to initialise here if there is no constant increase in miles each week
double n=2.6;
for(int i=0;i<10;i++) //Initialisation of array
{
miles[i]=n;
n=n+2.6; //assume a constant rise of 2.6 miles in each week
}
for(int i=0;i<10;i++){
cout<<"No of miles covered during "<<i+1<<" week : "<<miles[i]<<" miles"<<endl; // print the miles being covered in eack week
}
cout << "Length of list = " << (sizeof(miles)/sizeof(*miles)) << endl; //print the length of list
int Index=2; //value in the array element when the index contains 2
cout<<"The value in the array element when the index contains 2 is: "<<miles[Index]<<endl;
double sum=0;
for(int i=0;i<10;i++){
sum=sum+miles[i]*7; //claculate sum of the total miles(i.e sum of miles covered each day) for 10 weeks
}
cout<<"The sum of the total miles spent running over the 10 weeks"<<sum<<endl; //print the total sum
cout<<"Average miles covered per day: "<< (sum/(10*7)); //calculate and print the average of miles
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.