Complete Programming Exercise 12 “Average Steps Taken” at the end of Chapter 6 (
ID: 3715422 • Letter: C
Question
Complete Programming Exercise 12 “Average Steps Taken” at the end of Chapter 6 (page 342 in the 4th Edition). Can you think of a way to write a “helper” function that will reduce the amount of repetitive code in your solution?
12. Average Steps Taken Fitness Tracker is a wearable device that tracks your physical activi burned, heart rate, sleeping patterns, and so on. One common physical activity that mis Personal of these devices track is the number of steps you take each day. If you have downloaded this book's source code from the Computer Science Portal, vou will find a file named steps.txt in the Chapter 06 folder. (The Computer Science Portal can be found at www.pearsonhighered.com/gaddis.) The steps. txt file contains the number of steps a person has taken each day for a year. There are 365 lines in the file, and each line contains the number of steps taken during a day. (The first line is the number of steps taken on January 1st, the second line is the number of steps taken on January 2nd, and so forth.) Write a program that reads the file, then displays the average number of steps taken for each month. (The data is from a year that was not a leap year, so February has 28 days.)Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
int main(){
ifstream fin("steps.txt");
if (!fin){
cout << "Error opening file ";
return 0;
}
for (int i = 1; i<=12; i++){
if (i == 2){
double sum = 0;
for (int j = 0; j<28; j++){
int a;
fin >> a;
sum = sum + a;
}
double avg = sum/28;
cout << "Average number of steps in February:" << avg << endl;
}
if (i == 1 || i == 3 ||i == 5 || i == 7 || i == 8 || i == 10 || i == 12){
double sum = 0;
for (int j = 0; j<31; j++){
int a;
fin >> a;
sum = sum + a;
}
double avg = sum/31;
switch(i){
case 1 : cout << "Average number of steps in January:" << avg << endl;
break;
case 3 : cout << "Average number of steps in March:" << avg << endl;
break;
case 5 : cout << "Average number of steps in May:" << avg << endl;
break;
case 7 : cout << "Average number of steps in July:" << avg << endl;
break;
case 8 : cout << "Average number of steps in August:" << avg << endl;
break;
case 10 : cout << "Average number of steps in October:" << avg << endl;
break;
case 12 : cout << "Average number of steps in December:" << avg << endl;
break;
}
}
if (i == 4 || i == 6 ||i == 9 || i == 11){
double sum = 0;
for (int j = 0; j<31; j++){
int a;
fin >> a;
sum = sum + a;
}
double avg = sum/30;
switch(i){
case 4 : cout << "Average number of steps in April:" << avg << endl;
break;
case 6 : cout << "Average number of steps in June:" << avg << endl;
break;
case 9 : cout << "Average number of steps in September:" << avg << endl;
break;
case 11 : cout << "Average number of steps in November:" << avg << endl;
break;
}
}
}
fin.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.