Solved

C++ Problems - Easy for you, tough for me.

Posted on 2006-10-26
10
274 Views
Last Modified: 2010-04-01
I am trying to understand iteration and loops and for some reason just can't grasp it.  The problem I have to code is this.

Suppose you deposit $1 into an account this month, $2 into the account the second month, $4 in the third month and so on, doubling your deposit every month.  Write a program that displays the first month your deposit exceeds $1,000,000.  

I've started with a while loop but it's just not working.  I don't even know how I'm going to count up the number of months it takes.  Can someone show me the code for this please?  I actually need to have this and another I will be posting by tomorrow so I will assign an "urgent number of points (500).
0
Comment
Question by:MrParkes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17810902
Hi MrParkes,
> I've started with a while loop but it's just not working.  I don't even
> know how I'm going to count up the number of months it takes.

Show us the code and we will try to get it working!!

We can assist you with your homework, but EE policy prevents us from doing your homework for you. Show us the effort and we will help you with it.

Cheers!
sunnycoder
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 17811049
In general here are the things that you need to know about loops:

- Termination condition
   What is required for the loop to terminate?
   Generally, there is an expression that is evaluated, and something about this expression should change within the loop
   so that the loop termination can occur.

- Frequently, there is a loop control variable that is initialized output of the loop
  You said that you have tried using a "while", this is just one of the possible loop constructs that exists.
  The while needs to have an associated expression that evaluates to true, or false.  As long as the result of the
  expression is true, the loop will continue to be executed.

* Something in the loop must modify the loop expression, otherwise you will have an infiinte loop
0
 

Author Comment

by:MrParkes
ID: 17815528
I got started with this but as you can see I'm not very far and probably not at all close.

#include <iostream>

using namespace std;

int main ()
{

       int   balance,                  // declare the variables
             deposit,
             total_balance;

              // State the purpose to the user
            
            cout << "How many months will it take to establish a $1,000,000" << endl;
            cout << "balance by doubling you deposit every month?" << endl,
            cout << endl;

            balance = 0;            // Initialize the account balance to zero
            
            cout << "Make your first deposit:";
            cin  >> deposit;      // The first number to be multiplied
                  
            // perform this iteration as long as the balance is less than $1,000,000
            
            while (total_balance < 1000000);  

            {
                                    
                 cout << total_balance << endl;
                 cout << 2 * balance << endl;
                              
            }
            
            cout << "Current balance is: ";
            cout << endl;

return 0;

}

This is so humbling to a beginner like me.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 17816815
Your loop is correct ... One of the best codes for a beginner that I have seen ... Just a few modifications would make it work like a charm

Initialize all variables ...not just balance. Moreover balance should be initialized to 1 and not 0 since this is the variable you are using to calculate monthly deposit. The name balance is misleading - monthly_installment would have caused less confusion!!

What is the role of reading in deposit? You can omit it all together.

1. note that you are not changing the value of balance or total_balance in the loop ... You do multiply the value of balance by 2 but you never assign it back to balance
balance  = 2 * balance should modify the value of balance ... Next add this value back to total_balance
total_balance = total_balance + balance

2. You need to keep track of how many times you doubled the amount. This would represent the number of months. To do this, declare a variable "int months" and initialize it  to 0. Within the loop, simply increment this variable by one. When you exit the loop, this variable will contain the number of times you iterated in the loop and would be same as the number of months that you needed to double the amount to reach your target

3. Print this value months after you have exited the loop

4. while (total_balance < 1000000);  
This is an infinite loop .. the ; denotes end of loop and since total_balance is not modified in the condition, the loop will execute for ever. Remove the ;

Good work and good luck!

Cheers!
sunnycoder
0
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 17816831
I guess in ur program, u r doubling the balance instead of the deposit.

Replace ur while loop with the for loop below : -

<<Code Edited by sunnycoder, Page Editor>>
0
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 17816841
Oops, sorry for directly posting the previous code.

@ admin : Plz delete it before MrParkes sees it so that he can try & develop it himself.

Sorry again !!!
0
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 17816858
Thanx :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question