#include <iostream>
using namespace std;
//In this version we use cout << instead printf, so we need to using namespace std;
void main()
{
const int Boys = 8;
const int Girls = 6;
//We create constants of type int, because the number of boys and girls
//is an integer that does not change during the program
double boys_to_girls, girls_to_boys, boys_to_total, girls_to_total;
//We create variables of type double for each ratio
//We do not use the int type for this, since we want to calculate the rations as a number with a fractional part
boys_to_girls = (double)Boys / Girls; //Calculating rations.
//We write(double)Boys to temporarily change the type of the Boys variable to double.
//When dividing int by int, we always get an integer value, but we need a fractional one.
//Therefore we divide dobule by int, getting double.
girls_to_boys = (double)Girls / Boys;
boys_to_total = (double)Boys / (Boys + Girls);
girls_to_total = (double)Girls / (Boys + Girls);
cout << "Ratio of boys to girls: " << boys_to_girls << "\n"; //\n - escape sequence, newline
cout << "Ratio of girls to boys: " << girls_to_boys << "\n";
cout << "Ratio of boys to overall total: " << boys_to_total << "\n";
cout << "Ratio of girls to overall total: " << girls_to_total << "\n\n";
return;
}
VERSION WITH printf()
#include <iostream>
void main()
{
const int Boys = 8;
const int Girls = 6;
//We create constants of type int, because the number of boys and girls
//is an integer that does not change during the program
double boys_to_girls, girls_to_boys, boys_to_total, girls_to_total;
//We create variables of type double for each ratio
//We do not use the int type for this, since we want to calculate the rations as a number with a fractional part
boys_to_girls = (double)Boys / Girls; //Calculating rations.
//We write(double)Boys to temporarily change the type of the Boys variable to double.
//When dividing int by int, we always get an integer value, but we need a fractional one.
//Therefore we divide dobule by int, getting double.
girls_to_boys = (double)Girls / Boys;
boys_to_total = (double)Boys / (Boys + Girls);
girls_to_total = (double)Girls / (Boys + Girls);
printf("%s %f\n", "Ratio of boys to girls :", boys_to_girls);
printf("%s %f\n", "Ratio of girls to boys: ", girls_to_boys);
printf("%s %f\n", "Ratio of boys to overall total: ", boys_to_total);
printf("%s %f\n\n", "Ratio of girls to overall total: ", girls_to_total);
return;
}
Let me know if this helped you.
Comments
Leave a comment