Skip to content

Instantly share code, notes, and snippets.

@liddiard
Last active August 29, 2015 14:13
Show Gist options
  • Save liddiard/721b30927ecc3e6b2d33 to your computer and use it in GitHub Desktop.
Save liddiard/721b30927ecc3e6b2d33 to your computer and use it in GitHub Desktop.
PIC 10A Homework 2
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main(){
const int FIRST_COL_WIDTH = 20;
const int SECOND_COL_WIDTH = 10;
string first_category;
string second_category;
int number_in_first_category;
int number_in_second_category; // declare two strings for the two categories and two integers for the numerical value for each
cout << "How many in the first category? ";
cin >> number_in_first_category >> first_category;
cout << "How many in the second category? ";
cin >> number_in_second_category >> second_category; // input number in each category and type of name
double ratio_first_category_to_second_category = (double)number_in_first_category / number_in_second_category;
double ratio_second_category_to_first_category = (double)number_in_second_category / number_in_first_category; // equations for calculating ratios between the two categories
cout << fixed;
cout << setprecision(2); // set decimal precision to two places
cout << setw(FIRST_COL_WIDTH) << first_category << setw(SECOND_COL_WIDTH) << number_in_first_category << endl;
cout << setw(FIRST_COL_WIDTH) << second_category << setw(SECOND_COL_WIDTH) << number_in_second_category << endl;
string ratio_first_category;
string ratio_second_category;
int n = first_category.length() - 1;
ratio_first_category = first_category.erase(n, 2);
int m = second_category.length() - 1;
ratio_second_category = second_category.erase(m, 1); // erase "s" from category names to make them singular nouns
cout << setw(FIRST_COL_WIDTH) << ratio_first_category + "-to-" + ratio_second_category + " ratio" << setw(SECOND_COL_WIDTH) << ratio_first_category_to_second_category << endl;
cout << setw(FIRST_COL_WIDTH) << ratio_second_category + "-to-" + ratio_first_category + " ratio" << setw(SECOND_COL_WIDTH) << ratio_second_category_to_first_category << endl;
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment