11 Replies - 332 Views - Last Post: 04 August 2013 - 08:26 PM Rate Topic: -----

#1 PersinArd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 28-April 13

for loop and increment

Posted 03 August 2013 - 08:53 PM

a club charges 2500 per year for a memebrship.increase membership by %4 for six years. display the projected rates for next six years. i have the program written but i cant tell what im doing wrong, my results don't match what i have calculated.



#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
    double membership,increase,total;
    total=0;
    membership=2500;
    increase=0.04;
    for (membership=2500; (increase=0.04); membership++) {
        membership=membership*increase;
        cout<<"youre cost of memebership for the fist year is $\n"<<membership++<<endl;
         
        cout<<"youre cost of memebership for the second year is $\n"<<membership++<<endl;
        
        cout<<"youre cost of memebership for the third year is $\n"<<membership++<<endl;
         
        cout<<"youre cost of memebership for the fourth year is $\n"<<membership++<<endl;
        
        cout<<"youre cost of memebership for the fifth year is $\n"<<membership++<<endl;
      
        cout<<"youre cost of memebership for the sixth year is $\n"<<membership++<<endl;
        return 0;
    }
    

}




Is This A Good Question/Topic? 0
  • +

Replies To: for loop and increment

#2 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: for loop and increment

Posted 03 August 2013 - 09:17 PM

It doesn't seem like you understand the basic syntax or concept of loops very well. I would suggest you learn the basics of loops first and come back when you have problems with them. But for starters, if you do it correctly would eliminate the need for doing what your dong in the body.

This is what you would do without using loops, try doing this with correctly with loops.
    double membership, increase, total;
    total=0;
    membership=2500;
    increase=0.04;
    cout<<"youre cost of memebership for the fist year is $\n"<<membership<<endl;
    membership = membership + membership * increase;
    cout<<"youre cost of memebership for the second year is $\n"<<membership<<endl;
    membership = membership + membership * increase;
    cout<<"youre cost of memebership for the third year is $\n"<<membership<<endl;
    membership = membership + membership * increase;
    cout<<"youre cost of memebership for the fourth year is $\n"<<membership<<endl;
    membership = membership + membership * increase;
    cout<<"youre cost of memebership for the fifth year is $\n"<<membership<<endl;
    membership = membership + membership * increase;
    cout<<"youre cost of memebership for the sixth year is $\n"<<membership++<<endl;


This post has been edited by salazar: 03 August 2013 - 09:29 PM

Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,864
  • Joined: 25-December 09

Re: for loop and increment

Posted 03 August 2013 - 09:24 PM

Quote

I doesn't seem like you understand the basic syntax or concept of loops very well.

Would you care to elaborate? I'm not saying you're wrong, just asking you to explain why.

Jim
Was This Post Helpful? 1
  • +
  • -

#4 nandureddy  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 129
  • Joined: 31-January 11

Re: for loop and increment

Posted 03 August 2013 - 09:30 PM

Use a loop, for example:
for (i=0;i<=6; i++) {
        membership=membership+membership*increase;
        cout<<"youre cost of memebership for the fist year is $\n"<<membership<<endl;
}

Refer to loops.

This post has been edited by nandureddy: 03 August 2013 - 09:32 PM

Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,864
  • Joined: 25-December 09

Re: for loop and increment

Posted 03 August 2013 - 09:46 PM

Let's look at your loop construce.
    for (membership=2500; (increase=0.04); membership++) {

Now remember a for loop has three distinct elements separated by semicolons. The first is the initialization section, the second is the condition section, and the third is the increment section.

In your loop when will increase ever equal .04?? Usually for loop condition statements checks the value used in the increment section.

So for the loop you have something like the following would be more the norm.
    for (membership=2500; membership < 2506; membership++) {

Or something similar.

But your loop really should be working with the years not the membership. So something like this would probably be better:

double membership = 2500.0;
double increase = 0.04;
for(int year = 1; year < 6; year++)
{
   membership += (membership * increase);
...




Note the above is purposely not entirely correct, so be sure to throughly check the results.

Edit: @salazar please don't modify your post to add the additional information as this can make the conversation seem disjointed. Just add a post.


Jim

This post has been edited by jimblumberg: 03 August 2013 - 09:48 PM

Was This Post Helpful? 1
  • +
  • -

#6 PersinArd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 28-April 13

Re: for loop and increment

Posted 04 August 2013 - 06:08 PM

thanks jim for the help. a friend helped me earlier and told me the same thing. i feel its not calculating it right but i might be wrong. here is my output.

//this is my output.
youre cost of memebership for the fist year is $2600
youre cost of memebership for the second year is $2601
youre cost of memebership for the third year is $2602
youre cost of memebership for the fourth year is $2603
youre cost of memebership for the fifth year is $2604
youre cost of memebership for the sixth year is $2605

here is the new code.

#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
    double membership,increase,total;
    total=0;
    membership=2500;
    increase=0.04;
    for(int years = 0; years < 4; years++)
    {
        membership += membership *.04;
    
    
        cout<<"youre cost of memebership for the fist year is $"<<membership++<<endl;
         
        cout<<"youre cost of memebership for the second year is $"<<membership++<<endl;
        
        cout<<"youre cost of memebership for the third year is $"<<membership++<<endl;
         
        cout<<"youre cost of memebership for the fourth year is $"<<membership++<<endl;
        
        cout<<"youre cost of memebership for the fifth year is $"<<membership++<<endl;
      
        cout<<"youre cost of memebership for the sixth year is $"<<membership++<<endl;
        return 0;
    }
}


Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,864
  • Joined: 25-December 09

Re: for loop and increment

Posted 04 August 2013 - 06:32 PM

Look closely at the end of your for loop. Why is that return in the loop?

How many years are you required to be doing this calculation for?

There are still errors but you fixing the return issue should get you on your way.


Jim
Was This Post Helpful? 0
  • +
  • -

#8 PersinArd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 28-April 13

Re: for loop and increment

Posted 04 August 2013 - 06:49 PM

i added salezars method to batch it seem to do the job.

//here is the new output.
youre cost of memebership for the fist year is $2600
youre cost of memebership for the second year is $2705.04
youre cost of memebership for the third year is $2814.28
youre cost of memebership for the fourth year is $2927.89
youre cost of memebership for the fifth year is $3046.05
youre cost of memebership for the sixth year is $3168.93

#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
    double membership,increase,total;
    total=0;
    membership=2500;
    increase=0.04;
    for(int years = 0; years < 4; years++)
    {
        membership += membership *.04;
    
    
        cout<<"youre cost of memebership for the fist year is $"<<membership++<<endl;
          membership += membership *.04;
        cout<<"youre cost of memebership for the second year is $"<<membership++<<endl;
         membership += membership *.04;
        cout<<"youre cost of memebership for the third year is $"<<membership++<<endl;
          membership += membership *.04;
        cout<<"youre cost of memebership for the fourth year is $"<<membership++<<endl;
         membership += membership *.04;
        cout<<"youre cost of memebership for the fifth year is $"<<membership++<<endl;
       membership += membership *.04;
        cout<<"youre cost of memebership for the sixth year is $"<<membership++<<endl;
        return 0;
    }
}



i added the return 0 because the first time i ran the program it got stuck in an infinite loop.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,864
  • Joined: 25-December 09

Re: for loop and increment

Posted 04 August 2013 - 07:34 PM

Quote

i added the return 0 because the first time i ran the program it got stuck in an infinite loop.


You need to remove that return 0 from inside the loop. You should not have an infinite loop.

Jim
Was This Post Helpful? 1
  • +
  • -

#10 PersinArd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 28-April 13

Re: for loop and increment

Posted 04 August 2013 - 07:53 PM

i did but i had to put it back because it repeated the statements two more times with different number.
Was This Post Helpful? 0
  • +
  • -

#11 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: for loop and increment

Posted 04 August 2013 - 08:01 PM

A for loop is made up of three sections.

The first is the variables used by the loop. The second is what makes the loop stop. And the third is the variables that need to be changed for the next iteration.

As I explained before, this is code for doing it without the for loop. This doesn't go in a for loop.
    cout<<"youre cost of memebership for the fist year is $"<<membership++<<endl;
    membership += membership *.04;
    cout<<"youre cost of memebership for the second year is $"<<membership++<<endl;
    membership += membership *.04;
    cout<<"youre cost of memebership for the third year is $"<<membership++<<endl;
    membership += membership *.04;
    cout<<"youre cost of memebership for the fourth year is $"<<membership++<<endl;
    membership += membership *.04;
    cout<<"youre cost of memebership for the fifth year is $"<<membership++<<endl;
    membership += membership *.04;
    cout<<"youre cost of memebership for the sixth year is $"<<membership++<<endl;




This is what you body should look like. Do you notice the repeat lines of code? In a for loop you would only
have to write one, and not six for case.

So you body would look like this.

{
cout<<"youre cost of memebership for the fist year is $"<<membership++<<endl;

}

A for loop is made up of three sections.

The first is the variables used by the loop. The second is what makes the loop stop. And the third is the variables that need to be changed for the next iteration.

As I explained before, this is code for doing it without the for loop. This doesn't go in a for loop.
    cout<<"youre cost of memebership for the fist year is $"<<membership++<<endl;
    membership += membership *.04;
    cout<<"youre cost of memebership for the second year is $"<<membership++<<endl;
    membership += membership *.04;
    cout<<"youre cost of memebership for the third year is $"<<membership++<<endl;
    membership += membership *.04;
    cout<<"youre cost of memebership for the fourth year is $"<<membership++<<endl;
    membership += membership *.04;
    cout<<"youre cost of memebership for the fifth year is $"<<membership++<<endl;
    membership += membership *.04;
    cout<<"youre cost of memebership for the sixth year is $"<<membership++<<endl;




This is what you body should look like. Do you notice the repeat lines of code? In a for loop you would only
have to write one, and not six for case.

So your for loop body would look like this. This may not but correct. This is only to show you the concept that with a loop you dramatically reduce your work.

{
cout<<"youre cost of memebership for the fist year is $"<<membership++<<endl;

}

Note: Sorry for the edit. I accidently posted prematurely.

This post has been edited by salazar: 04 August 2013 - 08:04 PM

Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,864
  • Joined: 25-December 09

Re: for loop and increment

Posted 04 August 2013 - 08:26 PM

Quote

i did but i had to put it back because it repeated the statements two more times with different number.


You need to remove that return from inside the loop. The problem with the multiple printouts is that you are printing things multiple times in that loop.

Replace that return 0 with the following:

cout << endl;

And maybe you'll see a difference.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1