Output data in columns

I am currently trying to write a code in which I will have information displayed in two different columns. The first column will be the population in thousands, ( going from 10 to 25), while the second column will be the flowrate associated with the corresponding number of people. I'm having trouble incorporating a function of the form Q = 3.86 sqrtP (1-0.01sqrtP) where P is the population in thousands. Any help or advice would be useful. Thank you.

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int main()
{
int lowerBound = 10;
int upperBound = 25;
int population;

cout << "Enter the lower bound of population in thousands (1 = 1000, 10 = 10000, etc.):" << lowerBound << endl;
cout << "Enter the upper bound of population in thousands (1 = 1000, 10 = 10000, etc.):" << upperBound << endl;

for (int i = 10 ; i <= 25 ; i++)
{
cout << "Population in thousands " << i << ":" << endl;
cin >> population;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(4);


}

This is the code I have thus far. I'm assuming that I will need to declare the function prior to the loop?
1
2
3
4
5
6
7
8
#include <cmath>

// function of the form Q = 3.86 sqrtP (1-0.01sqrtP) where P is the population in thousands.
double Q( double population_in_thousands ) 
{
    const double sqrt_p = std::sqrt(population_in_thousands) ;
    return 3.86 * sqrt_p * ( 1.0 - 0.01*sqrt_p ) ;
}
Thank you. I have implemented that piece of code into my existing one. I am now having trouble figuring out how to place the return value of the formula into the flowrate column. Here is an updated look at the code I have so far.

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

//function of the form Q = 3.86 sqrtP (1 - 0.01sqrtP) where P is the population in thousands.

double Q (double population_in_thousands)
{
const double sqrt_p = std::sqrt (population_in_thousands);
return 3.86 * sqrt_p * (1.0 - 0.01 * sqrt_p);
}

int main()
{
int lowerBound = 10;
int upperBound = 25;

cout << "Enter the lower bound of population in thousands (1 = 1000, 10 = 10000, etc.):" << lowerBound << endl;
cout << "Enter the upper bound of population in thousands (1 = 1000, 10 = 10000, etc.):" << upperBound << endl;
cout << setw(10) << "Population in thousands " <<
setw(10) << "Flowrate" << endl;

for (int i = 10 ; i <= 25 ; i++)
{
cout << i << endl;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(4);





}

return 0;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cmath>
#include <iomanip>

// function of the form Q = 3.86 sqrtP (1-0.01sqrtP) where P is the population in thousands.
double flow_rate( double population_in_thousands )
{
    const double sqrt_p = std::sqrt(population_in_thousands) ;
    return 3.86 * sqrt_p * ( 1.0 - 0.01*sqrt_p ) ;
}

int main()
{
    int lower_bound ;
    std::cout << "Enter the lower bound of population in thousands (1 = 1000, 10 = 10000, etc.): " ;
    std::cin >> lower_bound ;

    int upper_bound ;
    std::cout << "Enter the upper bound of population in thousands (1 = 1000, 10 = 10000, etc.): " ;
    std::cin >> upper_bound ;

    std::cout << '\n' << std::fixed << std::setprecision(4)
              << std::setw(15) << "Population" << std::setw(10) << "Flowrate" << '\n'
              << std::setw(15) << "----------" << std::setw(10) << "--------" << '\n' ;

    for( int i = lower_bound ; i <= upper_bound ; ++i )
        std::cout << std::setw(8) << i << ",000"  << std::setw(12) << flow_rate(i) << '\n' ;
}
Last edited on
Thank you.
Topic archived. No new replies allowed.