Answer to Question #100701 in C++ for x123

Question #100701
There are 8 boys and 6 girls in a classroom. Calculate the following:
1. Ratio of boys to girls
2. Ratio of girls to boys
3. Ratio of boys to overall total
4. Ratio of girls to overall total
1
Expert's answer
2019-12-23T03:43:46-0500

#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.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS