207k views
4 votes
-------- C++ ------

Assume you need to test a function named inOrder. The function inOrder receives three intarguments and returns true if and only if the arguments are in non-decreasing order: that is, the second argument is not less than the first and the third is not less than the second. Write the definition of driver function testInOrder whose job it is to determine whether inOrder is correct. So testInOrder returns true if inOrder is correct and returns false otherwise.

For the purposes of this exercise, assume inOrder is an expensive function call, so call it as few times as possible!

-------------------------------------------

Assume you need to test a function named max. The function max receives two int arguments and returns the larger. Write the definition of driver function testmax whose job it is to determine whether max is correct. So testmax returns true if max is correct and returns false otherwise.

------------------------------------------

Write a program that will predict the size of a population of organisms. The program should ask the user for the starting number of organisms, their average daily population increase (as a percentage, expressed as a fraction in decimal form: for example 0.052 would mean a 5.2% increase each day), and the number of days they will multiply. A loop should display the size of the population for each day.

Input Validation.Do not accept a number less than 2 for the starting size of the population. If the user fails to satisfy this print a line with this message "The starting number of organisms must be at least 2.", display the prompt again and try to read the value. Similarly, do not accept a negative number for average daily population increase, using the message "The average daily population increase must be a positive value." and retrying. Finally, do not accept a number less than 1 for the number of days they will multiply and use the message "The number of days must be at least 1."

1 Answer

4 votes

Answer:

The function testInOrder is as follows:

bool testInOrder(){

bool chk= false;

int n1, n2, n3;

cin>>n1;cin>> n2; cin>> n3;

if(n2>=n1 && n3 >= n2){chk = true;}

bool result = inOrder(n1,n2,n3);

if(result == chk){ return true;}

else{return false;}

}

The function testmax is as follows:

bool testmax(){

bool chk = false;

int n1, n2;

cin>>n1; cin>>n2;

int mx = n1;

if(n2>=n1){ mx = n2; }

int returnMax = max(n1,n2);

if(returnMax == mx){ chk = true; }

return chk;

}

The prediction program is as follows:

#include <iostream>

using namespace std;

int main(){

int startSize,numDays;

float aveInc;

cout<<"Start Size: ";

cin>>startSize;

while(startSize<2){

cout<<"The starting number of organisms must be at least 2.";

cin>>startSize; }

cout<<"Average Daily Increase: ";

cin>>aveInc;

while(aveInc<1){

cout<<"The average daily population increase must be a positive value.";

cin>>aveInc; }

cout<<"Number of days: ";

cin>>numDays;

while(numDays<1){

cout<<"The number of days must be at least 1.";

cin>>numDays; }

for(int i = 0;i<numDays;i++){ startSize*=(1 + aveInc); }

cout<<"Predicted Size: "<<startSize;

}

Step-by-step explanation:

testInOrder

This defines the function

bool testInOrder(){

bool chk= false;

This declares all three variales

int n1, n2, n3;

This gets input for the three variables

cin>>n1; cin>>n2; cin>>n3;

This correctly check if the numbers are in order, the result is saved in chk

if(n2>=n1 && n3 >= n2){chk = true;}

This gets the result from inOrder(), the result is saved in result

bool result = inOrder(n1,n2,n3);

If result and chk are the same, then inOrder() is correct

if(result == chk){ return true;}

If otherwise, then inOrder() is false

else{return false;}

testmax

This defines the function

bool testmax(){

This initializes the returned value to false

bool chk = false;

This declares the two inputs as integer

int n1, n2;

This gets input for the two numbers

cin>>n1; cin>>n2;

This initializes the max of both to n1

int mx = n1;

This correctly calculates the max of n1 and n2

if(n2>=n1){ mx = n2; }

This gets the returned value from the max() function

int returnMax = max(n1,n2);

If returnMax and max are the same, then max() is correct

if(returnMax == mx){ chk = true; }

return chk;

}

Prediction program

This declares all necessary variables

int startSize,numDays; float aveInc;

This gets input for start size

cout<<"Start Size: "; cin>>startSize;

The loop is repeated until the user enters valid input (>=2) for startSize

while(startSize<2){

cout<<"The starting number of organisms must be at least 2.";

cin>>startSize; }

This gets input for average increase

cout<<"Average Daily Increase: "; cin>>aveInc;

The loop is repeated until the user enters valid input (>=1) for aveInc

while(aveInc<1){

cout<<"The average daily population increase must be a positive value.";

cin>>aveInc; }

This gets input for the number of days

cout<<"Number of days: "; cin>>numDays;

The loop is repeated until the user enters valid input (>=1) for numDays

while(numDays<1){

cout<<"The number of days must be at least 1.";

cin>>numDays; }

The following loop calculates the predicted size at the end of numDays days

for(int i = 0;i<numDays;i++){ startSize*=(1 + aveInc); }

This prints the predicted size

cout<<"Predicted Size: "<<startSize;

User ArwynFr
by
6.1k points