Like Tree2Likes
  • 2 Post By Salem

change var inside loop and run loop again

This is a discussion on change var inside loop and run loop again within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { double a=1; double b=0.01; double c; c = a * b; while ( a ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    4

    change var inside loop and run loop again

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    
    double a=1;
    double b=0.01;
    double c;
    
    c = a * b;
    
    while ( a <= 10000)
    {
         printf("%f\t%f\n", a, c);
    a = a++;
    c = a*b;
    }
    
    }
    return 0;
    }
    my question is how do I replace the value of b and run the loop over again untill b = 100
    thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Put into words what it is you actually are trying to do. You can't code for what you want if you don't know what it is you want to do. How are a and b related? Why are you using a to control the loop if you really care about what b is doing?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    By having a second, outer loop.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    b is 1% and a is 1 dollar
    i used this loop to get a to go up to 10,000 dollars and return the % amount for each dollar

    i don't know how to change the value of b and run the loop again

    if I change the value of b to .02 the loop will return the % of the given dollar value


    sorry I should of put comments in code

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Here's a hint... If the loop were in a function, you could feed it seed values and run it as often as you like.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > a = a++;
    This is wrong -> see Question 3.3

    > b is 1% and a is 1 dollar
    Then you'll want to use integers, not floats. floats are approximate numbers with all sorts of rounding errors. If you want to keep things nice and accurate, do something like

    int dollars = 100;
    int cents = 1;

    ...

    dollars += 100; // a new dollar
    cents++; // a new cent

    Notice how I've uses meaningful variable names, not meaningless a,b,c
    quzah and AndrewHunter like this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-23-2009, 03:51 AM
  2. For Loop inside While Loop
    By mesmer in forum C Programming
    Replies: 1
    Last Post: 11-29-2008, 11:29 AM
  3. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  4. loop and compiling inside a code
    By MtJ in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 11:50 AM
  5. for loop inside of a for loop
    By pippen in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2003, 03:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21