Created
March 25, 2014 18:23
-
-
Save dillmo/9767993 to your computer and use it in GitHub Desktop.
Intro to C++ Homework Problem Set 4 Solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Problem Set 4 Question 10 solution by Dillon Morse | |
* -------------------------------------------------- | |
* Write a proram that uses nested loops to collect data and calculate the | |
* average rainfall over a period of years. The program should first ask for | |
* the number of years. The outer loop will iterate once for each year. The | |
* inner loops will iterate twelve times, once for each month. Each iteration | |
* of the inner loop will ask the user for the inches of rainfall for that | |
* month. | |
* | |
* After all iterations, the program should display the number of months, the | |
* total inches of rainfall, and the average rainfall per month for the entire | |
* period. | |
* | |
* Input Validation: Do not accept a number less than 1 for the number of | |
* years. Do not accept negative numbers for the monthly rainfall. | |
*/ | |
#include<iostream> | |
#include<vector> | |
using namespace std; | |
int get_years() { | |
int years; | |
cout << "How many years did you take data for?" << '\n'; | |
cin >> years; | |
return years; | |
} | |
vector<double> get_data(int years) { | |
// vector types make it easy to perform CRUD operations on data | |
vector<double> rainfall; | |
int i; | |
int i2; | |
double input; | |
for(i = 1; i <= years; i = i + 1) { | |
for(i2 = 1; i2 <= 12; i2 = i2 + 1) { | |
cout << "Enter your rainfall for the " << i2 << "th month of the " << i | |
<< "th year." << '\n'; | |
cin >> input; | |
// Adds the user's input to as the next item in the vector | |
rainfall.push_back(input); | |
} | |
} | |
return rainfall; | |
} | |
int number_of_months(vector<double> rainfall) { | |
int months = rainfall.size(); | |
return months; | |
} | |
double total_inches(vector<double> rainfall) { | |
double sum; | |
// Finds the sum of the variables in the vector by using an iterator | |
for(vector<double>::iterator i = rainfall.begin(); i != rainfall.end(); ++i) { | |
sum = sum + *i; | |
} | |
return sum; | |
} | |
double get_average(vector<double> rainfall, double inches) { | |
double average = inches / rainfall.size(); | |
return average; | |
} | |
bool validate_input(int years, vector<double> rainfall) { | |
// Return false if the input fails to validate | |
if(years < 1) { | |
cout << "Error: You did not enter at least one year." << '\n'; | |
return false; | |
} | |
else { | |
for(vector<double>::iterator i = rainfall.begin(); i != rainfall.end(); ++i) { | |
if(*i < 0) { | |
cout << "Error: You entered a negative number for rainfall." << '\n'; | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
int main() { | |
int years = get_years(); | |
// vector types make it easy to perform CRUD operations on data | |
vector<double> rainfall = get_data(years); | |
int months = number_of_months(rainfall); | |
double inches = total_inches(rainfall); | |
double average = get_average(rainfall, inches); | |
// Quit if input validation fails | |
if(!validate_input(years, rainfall)) return 0; | |
cout << "Number of months: " << months << " months" <<'\n' | |
<< "Number of inches: " << inches << " inches" << '\n' | |
<< "Average rainfall: " << average << " inches" << '\n'; | |
// Necessary to prevent warnings from some compilers. | |
// I'm not keeping the program running in a paused state because I am running | |
// it from a CLI, so letting it close is most convenient. | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Problem Set 4 Question 10 solution by Dillon Morse | |
* -------------------------------------------------- | |
* Write a program that generates a random number and asks the user to guess | |
* what the number is. If the user's guess is higher than the random number, | |
* the program should display "Too high, try again." If the user's guess is | |
* lower than the random number, the program should display "Too low, try | |
* again." The program should use a loop that repeats until the user correctly | |
* guesses the random number. | |
*/ | |
#include<iostream> | |
#include<ctime> | |
#include<cstdlib> | |
using namespace std; | |
int main() { | |
int guessed_num; | |
int rand_num; | |
bool escape = true; // Declared with a value so it can be used with a loop | |
// immediately. | |
srand(time(0)); | |
rand_num = rand() % 10 + 1; | |
cout << "I just came up with a number from 1 to 10. "; | |
while(escape) { | |
cout << "What do you think it is?" << '\n'; | |
cin >> guessed_num; | |
if(guessed_num < rand_num) cout << "Too low, try again." << '\n'; | |
else if(guessed_num > rand_num) cout << "Too high, try again." << '\n'; | |
else { | |
cout << "Good guess!" << '\n'; | |
escape = false; | |
} | |
} | |
return 0; | |
// The process is never paused by the kernel because I am running it using a | |
// CLI, so allowing it to terminate after execution is most convenient. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Problem Set 4 Question 4 Solution by Dillon Morse | |
* ------------------------------------------------- | |
* Running on a particular treadmill you burn 3.9 calories per minute. Write a | |
* program that uses a loop to display the number of calories burned after 10, | |
* 15, 20, 25, and 30 minutes. | |
*/ | |
#include<iostream> | |
using namespace std; | |
const double calories_per_minute = 3.9; | |
int main() { | |
int i; | |
for(i = 10; i <= 30; i = i + 5) { | |
cout << "After " << i << " minutes, " << i * calories_per_minute | |
<< " calories are consumed." << '\n'; | |
} | |
// Necessary to prevent warnings from some compilers. | |
// I'm not keeping the program running in a paused state because I am running | |
// it from a CLI, so letting it close is most convenient. | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment