Question

In: Computer Science

Below is my code, Replace the 2 static_cast codes to a simpler C++ code. I would...

Below is my code, Replace the 2 static_cast codes to a simpler C++ code. I would like to find another way to compile the program without using static_cast in my code. Thank you

#include <iostream>
#include <iomanip>
using namespace std;

class Population
{
private:
int population,
birth,
death;

public:
Population()
{
population = 2;
birth = 0;
death = 0;
}
Population(int x, int y, int z)
{
if (x < 2)
population = 0;
else
population = x;


if (y < 0)
birth = 0;
else
birth = y;


if (z < 0)
death = 0;
else
death = z;
}
void setPopulation(int x)
{
if (x < 2)
population = 0;
else
population = x;
}


void setBirth(int y)
{
if (y < 0)
birth = 0;
else
birth = y;
}


void setDeath(int z)
{
if (z < 0)
death = 0;
else
death = z;
}


double getBirth()
{
return static_cast<double>(birth) / population;
}
double getDeath()
{
return static_cast<double>(death) / population;
}
};


int main()
{
int population, birth, death;

cout << "What is the starting population: ";
cin >> population;

cout << "What is the amount of births: ";
cin >> birth;
cout << "What is the amount of deaths: ";
cin >> death;
Population place1(population, birth, death);


cout << fixed << setprecision(2);



cout << endl << "Birth rate is " << place1.getBirth() << endl;
cout << "Death rate is " << place1.getDeath() << endl;


return 0;
}

Solutions

Expert Solution

#include <iostream>
#include <iomanip>
using namespace std;

class Population
{
private:
int population,
birth,
death;

public:
Population()
{
population = 2;
birth = 0;
death = 0;
}
Population(int x, int y, int z)
{
if (x < 2)
population = 0;
else
population = x;


if (y < 0)
birth = 0;
else
birth = y;


if (z < 0)
death = 0;
else
death = z;
}
void setPopulation(int x)
{
if (x < 2)
population = 0;
else
population = x;
}


void setBirth(int y)
{
if (y < 0)
birth = 0;
else
birth = y;
}


void setDeath(int z)
{
if (z < 0)
death = 0;
else
death = z;
}


double getBirth()
{
// mulitply with double number so that it will be converted into double
return (birth *1.0) / population;
}
double getDeath()
{
// mulitply with double number so that it will be converted into double
return (death*1.0) / population;
}
};


int main()
{
int population, birth, death;

cout << "What is the starting population: ";
cin >> population;

cout << "What is the amount of births: ";
cin >> birth;
cout << "What is the amount of deaths: ";
cin >> death;
Population place1(population, birth, death);


cout << fixed << setprecision(2);

cout << endl << "Birth rate is " << place1.getBirth() << endl;
cout << "Death rate is " << place1.getDeath() << endl;


return 0;
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
Need this in C#. Below is my code for Problem 3 of Assignment 2. Just have...
Need this in C#. Below is my code for Problem 3 of Assignment 2. Just have to add the below requirement of calculating the expected winning probability of VCU. Revisit the program you developed for Problem 3 of Assignment 2. Now your program must calculate the expected winning probability of VCU through simulation. Run the simulation 10,000 times (i.e., play the games 10,000 times) and count the number of wins by VCU. And then, calculate the winning probability by using...
Below is my source code for file merging. when i run the code my merged file...
Below is my source code for file merging. when i run the code my merged file is blank and it never shows merging complete prompt. i dont see any errors or why my code would be causing this. i saved both files with male names and female names in the same location my source code is in as a rtf #include #include #include using namespace std; int main() { ifstream inFile1; ifstream inFile2; ofstream outFile1; int mClientNumber, fClientNumber; string mClientName;...
Below is my code in C#, When I run it, the output shows System.32[], Can you...
Below is my code in C#, When I run it, the output shows System.32[], Can you please check and let me know what is the problem in the code. class Program { static void Main(string[] args) { int number=12; Console.WriteLine(FizzArray(number)); } public static int[] FizzArray(int number) { int[] array = new int[number]; for (int i = 1; i < number; i++) array[i] = i; return array; }
In which folder of XAMPP would I put in my PHP code
In which folder of XAMPP would I put in my PHP code
The major advantage of using structs is to make the C++ code simpler, easier to read,...
The major advantage of using structs is to make the C++ code simpler, easier to read, and easier to work with. After completing this assignment, students will be able to: • use structs to create and/or introduce new custom data types • implement structs to group fixed numbers of pieces of data of different types • implement structs within structs • chaining using the dot operators to access nested fields • copy an entire structure • create an array of...
The world would be simpler if the US Tax Code and US GAAP were the same,...
The world would be simpler if the US Tax Code and US GAAP were the same, and thus two sets of books would not be needed. Discuss the reasons they are not and whether or not you think it necessary for the present system to remain as it is
I need assistance translating a custom C++ program to MIPS. My C++ code is the following:...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following: I have made numerous attempts on my own to no avail, any assistance is appreciated. Also, template code for this solution is provided below: #include int moveRobots(int *, int *, int, int ); int getNew(int, int); int main() { int x[4], y[4], i, j, myX = 25, myY = 25, move, status = 1; // initialize positions of four robots x[0] = 0; y[0]...
This is my C language code. I have some problems with the linked list. I can't...
This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL. #include #include #include #include struct node{int data; struct node *next;}; int main() {     struct node *head, *current, *temp, *trash;     srand(time(0));     int randNumber = rand()%51;     if(randNumber != 49)     {         temp = (struct node*)malloc(sizeof(struct node));         current = (struct node*)malloc(sizeof(struct node));...
How would I make it so that when I run my code it does not ask...
How would I make it so that when I run my code it does not ask for input (not having to enter after the statement and enter 0 for example) after ROXY (Forever ROXY Enterprises) appears? Like it would tell me the second statement right away along with the Roxy phrase. This is in C++. My code: #include / #include using std::endl; int main() {    void readAndConvert();    unsigned int stockSymbol;    unsigned int confNum;    std::cout << "ROXY...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT