Need help with a code problem

Im trying to get this code working in C++. I originally wrote it in Python as:
folds = raw_input('Enter the number of folds of paper')
paperThickness = 0.005;
#0.005 = 1/200
for i in range (0, int(folds)):
paperThickness = paperThickness * 2.0
print 'The thickness after '+folds + ' folds will be', paperThickness, 'cm'

Below are two codes I've tried:

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
29
30
31
32
33
34
# include <iostream>

using namespace std;
int main()
    {

    int folds;
    const double paperThickness = 0.005;
    cout<<"Enter the number of folds of paper"<<endl;
    cin>>folds;
    cout<<endl;
    double thick = paperThickness;
    while(folds >= 0){
        thick *=2;
    }
    cout<<"The thickness after "<<folds<<" folds will be "<<thick<<"cm<<endl;

    /* Or 

    int folds;
    double paperThickness;
    cout<<"Enter the number of folds of paper"<<endl;
    cin>>folds;
    cout<<endl;
    for(folds =0; folds;){
        paperThickness = 0.005;
        paperThickness = paperThickness*2.0;
    }
    cout<<"The thickness after "<<folds<<" folds will be "<<paperThickness<<"       cm"<<endl;
    */

    return 0;
    }
 
Last edited on
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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    setprecision(5);  // ( I think)
    int folds;
// Im trying to get this code working in C++. I originally wrote it in Python as:
// folds = raw_input('Enter the number of folds of paper')

cout << "Enter the number of folds of paper  ";
cin >> folds;
float paperThickness = 0.005;

// #0.005 = 1/200    No idea what this is


// for (i in range (0, int(folds)):  would be
 for (int i=0;i<folds;i++)
{
    int paperThickness = paperThickness * 2.0;
cout << "The thickness after  "<< folds << " folds will be " << paperThickness << ", cm" << endl;
}

return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    const double paper_thickness = 0.005;

    std::cout << "Enter the number of folds of paper: " ;
    int folds ;
    std::cin >> folds ;

    double thickness = paper_thickness ;
    for( int i = 0 ; i < folds ; ++i ) thickness *= 2 ;

    std::cout << "The thickness after " << folds << " folds will be " << thickness << " cm\n" ;
}
> (...;i < folds) shouldn't this be > folds? why is it less than?

i starts with the value zero: for( int i = 0 ; i < folds ; ++i )

At the end of each iteration through the loop, i is incremented: for( int i = 0 ; i < folds ; ++i )

i < folds : The loop executes as long as i is less than folds.
The number of times the loop executes (thickness is doubled) is equal to folds

The loop could also be writen this way: for( int i = folds ; i > 0 ; --i ) thickness *= 2 ;
However, the earlier version is the canonical form.


> how do I get the "thickness" variable to print out to six decimal places?

#include <iomanip>
and then
1
2
std::cout << std::fixed << std::setprecision(6) // fixed point format, six digits after the decimal point
          << thickness << '\n' ; 

Thanks for the help! I figured out how to print to a specific decimal place too.

cout<<"The thickness after "<<folds<<" folds will be "<<fixed<<setprecision(6)<<thickness<< " cm"<<endl;
Topic archived. No new replies allowed.