What change do i make to fix my code so it will not output negative numbers for the future years. Here is the code. and output. HOW CAN I FIX THE ARITHMETIC OVERFLOW? and here is the question.  If P is the population of the first day of the year, B is the birth rate and D is the death rate, the estimated population at the end of the next year is given by formula: P + ( B * P ) / 100 – ( D * P ) / 100 The population growth rate is given by the formula: B - D Write a program that prompts the user to enter the starting population, birth and death rates and the number of years to project. The program should then calculate and print the growth rate and the estimated population after each year. Your program must contain the following functions: GrowthRate: This function takes as its parameters the birth and death rates and returns the population growth rate. Estimated Population: This function takes as its parameters the current population, the birth rate and the death rate. It returns the estimated population for the next year. Your program should not accept negative birth rate, negative death rate or population of less than 2.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 19PE
icon
Related questions
Question

What change do i make to fix my code so it will not output negative numbers for the future years. Here is the code. and output. HOW CAN I FIX THE ARITHMETIC OVERFLOW?

and here is the question. 

If P is the population of the first day of the year, B is the birth rate and D is the death rate, the estimated population at the end of the next year is given by formula:

P + ( B * P ) / 100 – ( D * P ) / 100

The population growth rate is given by the formula: B - D


Write a program that prompts the user to enter the starting population, birth and death rates and the number of years to project. The program should then calculate and print the growth rate and the estimated population after each year. Your program must contain the following functions:

GrowthRate: This function takes as its parameters the birth and death rates and returns the population growth rate.

Estimated Population: This function takes as its parameters the current population, the birth rate and the death rate. It returns the estimated population for the next year. Your program should not accept negative birth rate, negative death rate or population of less than 2.

X
✓ Filter
Ex
U
A Homework 1
2:3
Homework 1)
2 // main.cpp
3 // Homework 1
4
5
6
7
8 #include <iostream>
F8D2N2≈±±✿~✿IG FINNHAN
9
10 using namespace std;
11
17
19
12 float growthRate(int birth_Rate, int death_Rate) {
13
return birth_Rate - death_Rate;
14 }
15
16
18 }
23
20 int main()
21 {
24
25
26
27
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
C* main
//
// Created by Nicholas Florio on 9/11/23.
//
56
57 }
Homework 1 C* main
float est Population (int birth_Rate, int population, int death_Rate) {
return population + (birth_Rate * population) / 100 - (death_Rate * population) / 100;
Homework 1 My Mac
float birth_Rate;
float population;
float death_Rate;
int num_Years;
f main()
cout << "Enter the Birth Rate << endl;
cin >> birth_Rate;
cout << "Enter the Population " << endl;
cin >> population;
}
cout << "Enter the Death Rate << endl;
cin >> death_Rate;
cout << "Enter the number of years to project: ";
cin >> num_Years;
}
// Check for invalid inputs
if (population < 2 || birth_Rate < 0 || death_Rate < 0) {
cout << "Invalid input. Population must be >= 2, and birth and death rates must be non-negative." << endl;
return 1;
// Calculate and display the population growth rate
double growth = growthRate (birth_Rate, death_Rate);
cout << "Population growth rate: " << growth << "% per year" << endl;
// Calculate and display estimated population for each year
for (int year = 1; year <= num_Years; year++) {
return 0;
Finished running Homework 1
population = est Population (population, birth_Rate, death_Rate);
cout << "Estimated population after year. << year << ": " << (int) (population) << endl;
||
I
P
WH
20
Line: 24 Col: 22
+
6
2.
No Selection
H
Transcribed Image Text:X ✓ Filter Ex U A Homework 1 2:3 Homework 1) 2 // main.cpp 3 // Homework 1 4 5 6 7 8 #include <iostream> F8D2N2≈±±✿~✿IG FINNHAN 9 10 using namespace std; 11 17 19 12 float growthRate(int birth_Rate, int death_Rate) { 13 return birth_Rate - death_Rate; 14 } 15 16 18 } 23 20 int main() 21 { 24 25 26 27 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 C* main // // Created by Nicholas Florio on 9/11/23. // 56 57 } Homework 1 C* main float est Population (int birth_Rate, int population, int death_Rate) { return population + (birth_Rate * population) / 100 - (death_Rate * population) / 100; Homework 1 My Mac float birth_Rate; float population; float death_Rate; int num_Years; f main() cout << "Enter the Birth Rate << endl; cin >> birth_Rate; cout << "Enter the Population " << endl; cin >> population; } cout << "Enter the Death Rate << endl; cin >> death_Rate; cout << "Enter the number of years to project: "; cin >> num_Years; } // Check for invalid inputs if (population < 2 || birth_Rate < 0 || death_Rate < 0) { cout << "Invalid input. Population must be >= 2, and birth and death rates must be non-negative." << endl; return 1; // Calculate and display the population growth rate double growth = growthRate (birth_Rate, death_Rate); cout << "Population growth rate: " << growth << "% per year" << endl; // Calculate and display estimated population for each year for (int year = 1; year <= num_Years; year++) { return 0; Finished running Homework 1 population = est Population (population, birth_Rate, death_Rate); cout << "Estimated population after year. << year << ": " << (int) (population) << endl; || I P WH 20 Line: 24 Col: 22 + 6 2. No Selection H
X
✓ Filter
Ex
U
A Homework 1
2:3
Homework 1)
C* main
20
1
2
3
4 //
5
// Created by Nicholas Florio on 9/11/23.
6
//
7
8
9
10
11
Auto
//
main.cpp
// Homework 1
Homework 1 c* main
#include <iostream>
17
18 }
19
20 int main()
21
{
22
23
24
25
26
27
28
using namespace std;
float growthRate (int
return birth_Rate
O
Homework 1 My Mac
12
13
14 }
15
16 float est Population (int birth_Rate, int population, int death_Rate) {
return population + (birth_Rate * population) / 100 (death_Rate * population) / 100;
float birth_Rate;
float population;
float death_Rate;
int num_Years;
f main()
birth_Rate, int death_Rate) {
death_Rate;
cout << "Enter the Birth Rate " << endl;
cin >> birth_Rate;
Filter
Finished running Homework 1
-
All Output
Enter the Birth Rate
75645
Enter the Population
574664
Enter the Death Rate
5764
Enter the number of years to project: 8
Population growth rate: 69881% per year
Estimated population after year 1: 923321
Estimated population after year 2: 6966871
Estimated population after year 3: -17004738
Estimated population after year 4: -25566376
Estimated population after year 5: -16616825
Estimated population after year 6: 10172374
Estimated population after year 7: 2616320
Estimated population after year 8: -854225
Program ended with exit code: 0
→ Filter
P
G
▬▬▬▬▬▬▬▬▬▬▬▬▬ – 1
E0
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 1.
Line: 24 Col: 22
EP
+
+
U
Identity and Type
Name main.cpp
Type Default - C++ Source
Relative to Group
main.cpp
Location
Full Path /Users/nick/Desktop/
Homework 1/Homework 1/
main.cpp
On Demand Resource Tags
Only resources are taggable
Target Membership
Homework 1
Text Settings
Text Encoding No Explicit Encoding
Line Endings No Explicit Line Endings
Indent Using Spaces
Widths
4
Tab
Wrap lines
Indent
A
4
C
↑
↑
Transcribed Image Text:X ✓ Filter Ex U A Homework 1 2:3 Homework 1) C* main 20 1 2 3 4 // 5 // Created by Nicholas Florio on 9/11/23. 6 // 7 8 9 10 11 Auto // main.cpp // Homework 1 Homework 1 c* main #include <iostream> 17 18 } 19 20 int main() 21 { 22 23 24 25 26 27 28 using namespace std; float growthRate (int return birth_Rate O Homework 1 My Mac 12 13 14 } 15 16 float est Population (int birth_Rate, int population, int death_Rate) { return population + (birth_Rate * population) / 100 (death_Rate * population) / 100; float birth_Rate; float population; float death_Rate; int num_Years; f main() birth_Rate, int death_Rate) { death_Rate; cout << "Enter the Birth Rate " << endl; cin >> birth_Rate; Filter Finished running Homework 1 - All Output Enter the Birth Rate 75645 Enter the Population 574664 Enter the Death Rate 5764 Enter the number of years to project: 8 Population growth rate: 69881% per year Estimated population after year 1: 923321 Estimated population after year 2: 6966871 Estimated population after year 3: -17004738 Estimated population after year 4: -25566376 Estimated population after year 5: -16616825 Estimated population after year 6: 10172374 Estimated population after year 7: 2616320 Estimated population after year 8: -854225 Program ended with exit code: 0 → Filter P G ▬▬▬▬▬▬▬▬▬▬▬▬▬ – 1 E0 ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 1. Line: 24 Col: 22 EP + + U Identity and Type Name main.cpp Type Default - C++ Source Relative to Group main.cpp Location Full Path /Users/nick/Desktop/ Homework 1/Homework 1/ main.cpp On Demand Resource Tags Only resources are taggable Target Membership Homework 1 Text Settings Text Encoding No Explicit Encoding Line Endings No Explicit Line Endings Indent Using Spaces Widths 4 Tab Wrap lines Indent A 4 C ↑ ↑
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Binary numbers
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning