Thread: Nested Loop help

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    4

    Nested Loop help

    hello everyone,

    I am trying to achieve a nested loop to post the following on the prompt:

    1
    2 4
    1 3 5
    2 4 6 8
    1 3 5 7 9
    2 4 6 8 10 12

    I have been told I must use a modulus to achieve this but I am struggling.

    I must use the following format, for e.g I completed this code:

    Code:
    
    
    Code:
      for(i = 9; i >= 1; i-=2)  
      {
        for(j = 1; j <= i; j+=2) 
        {
          printf("%hd " , j);
        }
      printf("\n");
    }
    to achieve this:

    1 3 5 7 9
    1 3 5 7
    1 3 5
    1 3
    1

    so I am unsure where to add the modulus to to the other nested loop, any help would be appreciated.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    500
    You've got six lines. Then on each line you've got 1, 2, 3, 4, 5, 6 values.

    So that bit is easy. Your skeleton is

    Code:
    for (i=0; i < 6; i++)
    {
       for (j = 0; j < 6; j++)
       {
          x = // fill here, x is based on i and j
       }
       printf("\n");
    }
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    4
    How do I achieve the different values on every line?

    1
    2 4
    1 3 5
    2 4 6 8
    1 3 5 7 9
    2 4 6 8 10 12

    I know how to do it if it was just 1, 1 3 5, 1 3 5 7 9. But how do I work out two different sets of value in the same code. Thanks.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,011
    Code:
    1
    2 4
    1 3 5
    2 4 6 8
    1 3 5 7 9
    2 4 6 8 10 12
    If you can not figure out how to do the above try doing

    Code:
    1
    1 3
    1 3 5
    1 3 5 7
    1 3 5 7 9
    1 3 5 7 9 11
    Then look up what !0 equals.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    4
    I have worked out something similar such as

    Code:
    printf("\n\n");
    
    
      for(i = 2; i <= 10; i+=2)  
      {
        for(j = 2; j <= i; j+=2) 
        {
          printf("%hd " , j);
        }
      printf("\n");
    }
    for

    2
    2 4
    2 4 6
    2 4 6 8
    2 4 6 8 10

    but i am still confused on how to add different lines of numbers in between each other but still in a similar pattern using similar to code that i have used above.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,629
    Code:
      for(i = 2; i <= 10; i+=2)  
      {
        printf("Row start = %d\n", i % 2 );
        for(j = 2; j <= i; j+=2) 
        {
          printf("%hd " , j);
        }
      printf("\n");
    }
    From this, can you figure out an expression that gives you 1,2,1,2,1,2,....
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-07-2019, 09:32 AM
  2. nested while loop
    By Gaily Guzon in forum C Programming
    Replies: 2
    Last Post: 07-09-2013, 11:42 AM
  3. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  4. Nested Loop
    By DuckCowMooQuack in forum C Programming
    Replies: 8
    Last Post: 02-15-2011, 10:37 AM
  5. Nested For Loop
    By yacek in forum C Programming
    Replies: 3
    Last Post: 12-07-2010, 10:58 PM

Tags for this Thread