Doubling equation for C++ program.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gator6688
    New Member
    • Sep 2007
    • 63

    #1

    Doubling equation for C++ program.

    I am writing a program where I need to start with .01 and double it every day thereafter for 64 days. I have all the output showing up like it is supposed to except I can't get the money side to work right. Could someone help, please?
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    We'd love to help. That's what we're here for. You just need to ask a question. (Take a look at what you posted and you'll see that not a single bit of info allows us to diagnose your problem).

    Comment

    • gator6688
      New Member
      • Sep 2007
      • 63

      #3
      I need to make the program look like:

      Day Amount Owed
      1 .01
      2 .02
      3 .04
      4 .08

      all the way to day 64.

      I did a for loop and my program looks like it is supposed to. My problem is that I can't figure out how to start out at .01 and get it to double each day after. Maybe someone could help me with an equation or maybe I need to use a different kind of loop?

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Do you know about the *= operator?

        Comment

        • gator6688
          New Member
          • Sep 2007
          • 63

          #5
          I probably should know it but I am new and regretfully I dont.

          Comment

          • gator6688
            New Member
            • Sep 2007
            • 63

            #6
            Need help with C++ program.

            The program I am working on says a king gives a beggar one cent and double the amount for 64 days. I have to write a program that will display how much the king must pay on each day. My problem is I can't figure out how to get the amount owed to start at .01 and then double every day. Here is what I got so far. I have to turn this in in 27 minutes so any help would be useful.
            [code=cpp]
            #include "stdafx.h"
            #include <iostream>
            #include <iomanip>

            using namespace std;

            int _tmain(int argc, _TCHAR* argv[])
            {
            const int MAXNUMS = 64;

            int num;
            double amount;

            amount = .01;

            cout << "Day Amount Owed\n"
            << "--- -----------\n";




            for (num = 1; num<=MAXNUMS; num++)


            cout << setiosflags(ios ::fixed)
            << setiosflags(ios ::showpoint)
            << setw(2)<< num << " "
            << setw(8)<< setprecision(2) << amount*2 << " " <<endl;


            return 0;
            }[/code]
            Last edited by sicarie; Sep 24 '07, 12:43 PM. Reason: Code tags

            Comment

            • Benny the Guard
              New Member
              • Jun 2007
              • 92

              #7
              Your code simply does the math and prints out the result of amount*2 and does not store the result nor modify the amount. What you need to do is store the result of the math and then print it out. Such as:

              Code:
              amount = amount * 2.0;
              So the result of the math portion (amount * 2.0) is assigned into the variable amount. A shorthand of doing manipulations like this is:

              Code:
              amount *= 2.0;
              Same result as the *= just means multiply the left hand side by the right hand side and store into the left hand side. Similarly for +=, -= and /=. If your math is more complex use the first one as its easier to read.

              Then your variable value changes each iteration of the loop so on the second pass you get the result of (0.1* 2.0 * 2.0) like you want.

              Comment

              • gator6688
                New Member
                • Sep 2007
                • 63

                #8
                I have tried that and it gives me errors.

                Comment

                • Benny the Guard
                  New Member
                  • Jun 2007
                  • 92

                  #9
                  You cannot do it on the same line as your output. Thus you would have 2 lines in your loop, which means you need to use the { and } around it. This signals to the compiler that all the code within the { and the } is part of the loop (same is true of if statements).

                  If you still have trouble post your code using the assignment I showed and the compiler error.

                  Comment

                  • gator6688
                    New Member
                    • Sep 2007
                    • 63

                    #10
                    I am new to C++. Do I need to add another line for amount = amount * 2.0?

                    Comment

                    • Benny the Guard
                      New Member
                      • Jun 2007
                      • 92

                      #11
                      Technically not all of the time, but avoiding this usually results in hard to understand code. So I normally try to break things up into separate lines. So this would be a case for something like:

                      Code:
                      for (num = 1; num<=MAXNUMS; num++)
                        {
                        amount *= 2.0;
                        cout << .... (the  rest of your output here);
                        }
                      You could even put more code in the { and } if you needed it. In C/C++ we surround blocks of code with the brackets as opposed to something like Python where it uses the indention to show the blocks. You have already used this when defining your function, which itself is just a block of code.

                      Comment

                      • gator6688
                        New Member
                        • Sep 2007
                        • 63

                        #12
                        You are very helpful. This is what I got but when I run it it doesn't start at .01 it starts at .02. I need it to start at .01.
                        [code=cpp]
                        #include "stdafx.h"
                        #include <iostream>
                        #include <iomanip>

                        using namespace std;

                        int _tmain(int argc, _TCHAR* argv[])
                        {
                        const int MAXNUMS = 64;

                        int num;
                        double amount;

                        amount = .01;

                        cout << "Day Amount Owed\n"
                        << "--- -----------\n";




                        for (num = 1; num<=MAXNUMS; num++)
                        {
                        amount *=2.0;



                        cout << setiosflags(ios ::fixed)
                        << setiosflags(ios ::showpoint)
                        << setw(2)<< num << " "
                        << setw(8)<< setprecision(2) << amount << " " <<endl;
                        }


                        return 0;
                        }[/code]
                        Last edited by sicarie; Sep 24 '07, 12:44 PM. Reason: Code tags

                        Comment

                        • Benny the Guard
                          New Member
                          • Jun 2007
                          • 92

                          #13
                          Swap the position of the assignment and output operator. You are doubling the value then printing it out. You want to do the opposite.

                          Comment

                          • gator6688
                            New Member
                            • Sep 2007
                            • 63

                            #14
                            That gives me an error saying the variable amount is not defined.

                            Comment

                            • Benny the Guard
                              New Member
                              • Jun 2007
                              • 92

                              #15
                              Dud you swap the order inside the loop where you do amount *= 2.0? Post the code again. Also use the code tags (the # symbol in the button bar) its a bit easier to read.

                              Comment

                              Working...