Thread: nested loops

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    22

    nested loops

    Hello, I am having problems doing calculations in a loop.
    What Im trying to do is- count from number1 to number2- which i did using a FOR loop. So, say the numbers are 3 4 5 6 7, I need to take each one of those numbers and perform a calculation to it based on whether it is odd or even.
    I can do the calculation for "number1" and "number2", but am going no where on the numbers in between. Please help!!!

    Here's how im counting:
    if(n1>n2)
    for(i=n2;i<=n1;i++)
    else
    for(i=n2;i>=n1;i--)

    and heres my individual calculations:
    while(n1>1)
    if(n1%2==0)
    n1=n1/2;
    else
    n1=n1*3+1;

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    If you want to do calculations on a series of numbers after you enter them you will need some kind of array to hold these numbers for later processing.

    Jim

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Looking at your code that you have shown, I can see something that may be a problem

    Code:
    //You are not using '{' or '}'
    if(n1>n2)
    {
        for(i=n2;i<=n1;i++)
        {
            ...
        }
    }
    else
    {
        for(i=n2;i>=n1;i--)
        {
            ...
        }
    }
    
    Is problem 14 from projecteuler? Problem 14 - Project Euler
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    I agree with Click_here. Not using brackets could be the potential issue. Can you post your complete code?

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    22
    heres what I have so far. one loop is starting at the end of the 1st instead of into it. ???
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        int n1; //number1
        int n2; //number2
        int i;  //cycle length number
        i=1;
        int c;  //count
        c=1;   
    
    
        printf("enter 2 numbers: ");
        scanf("%d%d", &n1, &n2);
    
    
        for(c=n1;c>=n2;c--) 
         printf("count = %d\n",c ); 
        
        while(n1>1)
     
          if(n1%2==0)
            {n1=n1/2;
             printf("number = %d\n", n1);
             i++; }
          else
            {n1=(n1*3)+1;
             printf("number = %d\n", n1);
             i++; }
    
    
        printf("cycle length = %d\n", i);
    
    
    
    
        return 0;
    }

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by brillpsycho View Post
    heres what I have so far. one loop is starting at the end of the 1st instead of into it. ???
    If you want that the second loop is inside the first loop than you have to move it into the body of the first:
    Code:
    for-loop
    {
       // do stuff
       while-loop
       {
            // do more stuff
       }
    }
    Click_here and poornaMoksha told you already to use braces.

    Some more notes:
    Code:
    printf("enter 2 numbers: ");
    scanf("%d%d", &n1, &n2);
    
    for(c=n1;c>=n2;c--)
    The program is probably just for yourself but nevertheless here is a bug waiting to happen because you can't be sure that n1 is always bigger than n2.

    Code:
     while(n1>1)
          if(n1%2==0)
            {n1=n1/2;
             printf("number = %d\n", n1);
             i++; }
          else
            {n1=(n1*3)+1;
             printf("number = %d\n", n1);
             i++; }
    The second and third line in both the if- and the else-body are identical, thus you can move them below:
    Code:
    while (n1 > 1)
    {
        if (n1 % 2 == 0)
            n1 /= 2;
        else
            n1 = n1 * 3 + 1;
        printf("number = %d\n", n1);
        i++;
    }
    Bye, Andreas

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    22
    I added braces, and just for now im assuming the 1st number if the higher of the 2 numbers. With braces it changes it but im still getting an out come of my calculations only being for the 1st number of the sequence, instead of all the numbers between number 1 and number 2

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I'm guessing that this is Problem 14 - Project Euler which is based on Collatz conjecture - Wikipedia, the free encyclopedia (Read for hints on algorithm) - (I'm up to the 23rd problem on Project Euler and enjoying the challenges. Is anyone else doing them?)

    Your problem is that without
    Code:
    '{' and '}'
    the 'for' loop will not include the 'while' loop
    - Without the braces a 'for' or 'while' loop will only execute the next line of code up the the ';' (that is the printf under the 'for' loop and the if/else statement under the 'while' loop). Look at the first code block of Andreas post.

    [edit]
    Sorry - I think that we posted at the same time
    [/edit]
    Last edited by Click_here; 10-02-2012 at 05:00 PM.
    Fact - Beethoven wrote his first symphony in C

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Can you post your new code?
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    22
    heres new code:

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        int num1; //1st number
        int num2; //2nd number
        int i;  //cycle length number
        i=1;
        int count;  //count from 1st to 2nd number
        count=1;   
    
    
        printf("enter 2 numbers: ");
        scanf("%d%d", &num1, &num2);
    
    
        for(count=num1;count>=num2;count--)          //assuming n1 is greater than n2 for now
    {      printf("count = %d\n",count ); 
        
        while(num1>1)
    {
         if(num1%2==0)                //if number is even
          {
           num1=num1/2;
           printf("number = %d\n", num1);
           i++;
           }
         else                       //if number is odd
          {
           num1=(num1*3)+1;
           printf("number = %d\n", num1);
           i++; 
           }
    }}
        printf("cycle length = %d\n", i);
    
    
    
    
        return 0;
    }

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    [edit]

    I didn't see that the second number was supposed to be higher than the first number - Disregard this post

    [/edit]
    Code:
    for(count = num1; count <= num2; count--)
    Last edited by Click_here; 10-02-2012 at 05:50 PM.
    Fact - Beethoven wrote his first symphony in C

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    At the end of your while statement, 'i' is not being reset,

    num is still equal 1 from the last calculation when it goes through your 'for' loop for the second time

    -> When your code goes back to the while statement where num is still 1, so it doesn't execute for the next number
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested For loops
    By Dagda in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2012, 01:45 PM
  2. Nested loops
    By laurenlb in forum C Programming
    Replies: 5
    Last Post: 03-02-2011, 12:49 AM
  3. nested four loops
    By begginer in forum C Programming
    Replies: 12
    Last Post: 02-25-2011, 11:21 PM
  4. Nested loops
    By zeondik in forum C# Programming
    Replies: 2
    Last Post: 10-26-2008, 06:58 AM
  5. nested for loops
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 11:44 AM