Created
February 21, 2014 15:33
-
-
Save dillmo/9136330 to your computer and use it in GitHub Desktop.
C++ Homework Problem Set 2 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 2 Question 10 by Dillon Morse | |
* ----------------------------------------- | |
* Write a program that converts Celsius temperatures to Fahrenheit | |
* temperatures. | |
*/ | |
#include<iostream> | |
using namespace std; | |
double get_celsius() { | |
double temp; | |
cout << "What is your temperature in Celsius?" << '\n'; | |
cin >> temp; | |
return temp; | |
} | |
double convert_to_fahrenheit(double celsius_temp) { | |
double fahrenheit_temp = 9.0 / 5.0 * celsius_temp + 32; | |
return fahrenheit_temp; | |
} | |
int main() { | |
double celsius_temp = get_celsius(); | |
double fahrenheit_temp = convert_to_fahrenheit(celsius_temp); | |
cout << fahrenheit_temp << " degrees Fahrenheit" << '\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 2 Question 21 by Dillon Morse | |
* ----------------------------------------- | |
* For this assignment you will write two programs: | |
* | |
* Program 1: | |
* Write a program that asks the user to enter five numbers. Use a | |
* floating-point data type to hold the numbers. The program should create a | |
* file and save all five numbers to the file. | |
* | |
* Program 2: | |
* Write a program that opens the file created by Program 1, reads the five | |
* numbers, and displays them. The program should also calculate the sum of | |
* the five numbers. | |
* | |
* For convenience, I will be writing two functions instead of two programs, | |
* and calling them both from the main() function. | |
*/ | |
#include<iostream> | |
#include<fstream> | |
#include<cstdlib> | |
using namespace std; | |
int program_1() { | |
double nums[6]; | |
ofstream file; | |
cout << "Please enter 5 numbers, seperated by carriage returns" << '\n'; | |
cin >> nums[0] >> nums[1] >> nums[2] >> nums[3] >> nums[4]; | |
file.open("file.txt"); | |
file << nums[0] << '\n' << | |
nums[1] << '\n' << | |
nums[2] << '\n' << | |
nums[3] << '\n' << | |
nums[4] << '\n'; | |
file.close(); | |
return 0; | |
} | |
int program_2() { | |
ifstream file; | |
string line; | |
double nums[6]; | |
// Initializing i and sum as 0 allows me to add to them in loops without any | |
// extra code. | |
int i = 0; | |
double sum = 0; | |
file.open("file.txt"); | |
while(file >> nums[i]) { | |
i = i + 1; | |
} | |
file.close(); | |
cout << "Numbers: " << | |
nums[0] << ", " << | |
nums[1] << ", " << | |
nums[2] << ", " << | |
nums[3] << ", " << | |
nums[4] << '\n'; | |
cout << "Sum: "; | |
// sizeof returns the size of its argument in bytes. This allows me to find | |
// the number of variables in the array by dividing the size of the array by | |
// the size of a variable stored in the array. | |
for(i = 0; i < sizeof(nums) / sizeof(nums[0]); i = i + 1) { | |
sum = sum + nums[i]; | |
} | |
cout << sum << '\n'; | |
return 0; | |
} | |
int main() { | |
cout << "Program 1:" << '\n'; | |
program_1(); | |
cout << '\n' << "Program 2:" << '\n'; | |
program_2(); | |
// 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 2 Question 7 by Dillon Morse | |
* ---------------------------------------- | |
* A bag of cookies holds 40 cookies. The calorie information on the bag claims | |
* that there are 10 "servings" in the bag and that a servimg equals 300 | |
* calories. Write a program that asks the user to input how many cookies he or | |
* she actually ate and then reports how many total calories were consumed. | |
*/ | |
#include<iostream> | |
using namespace std; | |
int how_many() { | |
int cookies; | |
cout << "How many cookies did you consume?" << '\n'; | |
cin >> cookies; | |
return cookies; | |
} | |
double total_calories(int cookies) { | |
double cookies_per_serving = 40 / 10; | |
double calories_per_serving = 300; | |
double total = cookies / cookies_per_serving * calories_per_serving; | |
return total; | |
} | |
int main() { | |
int cookies = how_many(); | |
double total = total_calories(cookies); | |
cout << "You consumed " << total << " calories." << '\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