Skip to content

Instantly share code, notes, and snippets.

@dillmo
Created February 21, 2014 01:23
C++ Problem Set 1
/* Homework Set 1 Problem 13 by Dillon Morse
* -----------------------------------------
* An electronics company sells circuit boards at a 40 percent profit. Write a
* program that will calculate the selling price of a circuit board that costs
* $12.67. Display the result on the screen.
*/
#include<iostream>
#include<cmath>
using namespace std;
string price(double cost) {
double selling_price = cost * 1.4;
string str_price;
str_price = "$" + to_string(round(selling_price * 100) / 100);
// This is necessary to remove trailing zeros from the string resulting from
// the above statement.
str_price.erase(str_price.find_last_not_of('0') + 1, string::npos);
return str_price;
}
int main() {
double cost = 12.67;
cout << price(cost) << '\n';
// Necessary to prevent warnings from the g++ compiler.
// 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;
}
/* Homework Set 1, Problem 5 by Dillon Morse
* -----------------------------------------
* Write a program that stores the following values in five different
* variables: 28, 32, 37, 24, and 33. The program should first calculate the
* sum of these five variables and store the result in a seperate variable
* named sum. Then, the program should divide the sum variable by 5 to get the
* average. Display the average on the screen.
*/
#include<iostream>
using namespace std;
const int a = 28;
const int b = 32;
const int c = 37;
const int d = 24;
const int e = 33;
// It is simpler to take the size of the array as an argument than to try and
// find the number of variables in an array pointer.
// The name int_sum prevents conflicts with the variable, sum.
int int_sum(int nums[], int size) {
// sum is declared as 0 so I can add to it in the for loop without any extra
// code.
int sum = 0;
for(int i = 0; i < size; i++) {
sum = sum + nums[i];
}
return sum;
}
int main() {
int constants[6] = {a, b, c, d, e};
// sizeof returns the size of the argument in bytes. So, I am dividing the
// size of the array constants by the size of a constant stored in the array.
int size = sizeof(constants) / sizeof(constants[0]);
int sum = int_sum(constants, size);
double average = sum / 5;
cout << average << '\n';
// Necessary to prevent warnings from the g++ compiler.
// 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;
}
/* Homework Set 1 Problem 9 by Dillon Morse
* ----------------------------------------
* You have been given a job as a programmer on a Cyborg supercomputer. In
* order to accomplish some calculations, you need to know how many bytes the
* following data types use: char, int, float, and double. You do not have any
* manuals, so you can't look this information up. Write a C++ program that
* will determine the amount of memory used by these types and display the
* information on the screen.
*/
#include<iostream>
using namespace std;
int main() {
char character;
int integer;
float single_precision_floating_point;
double double_precision_floating_point;
string out;
// sizeof returns the size of a variable in bytes.
// to_string was added in C++ 11 and is used to convert variables to strings.
out = to_string(sizeof(character)) + ", " + to_string(sizeof(integer)) +
", " + to_string(sizeof(single_precision_floating_point)) + ", " +
to_string(sizeof(double_precision_floating_point));
cout << out << '\n';
// Necessary to prevent warnings from the g++ compiler.
// 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