C problem

This is a discussion on C problem within the C Programming forums, part of the General Programming Boards category; I am new to learning C and was wondering how I can print out the following results using nested if's ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    2

    C problem

    I am new to learning C and was wondering how I can print out the following results using nested if's and only using (1) printf:
    1--------- 1
    1----------2
    1--------- 3
    2--------- 1
    2--------- 2
    2--------- 3
    3--------- 1
    3 -------- 2
    3 -------- 3

    (I added ------- to represent spacing)

    As you can tell I am VERY new to learning this so I think somebody should know the answer in 2 seconds!

    You can post results here or to SHIBZ@att.net

    Thanks a ton!!!!!!

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    my homework detector is ringing again
    it would be possible using nested if statements and multiple printf's and i can see doing it with only 1 printf and no nested ifs, but nested if's and one printf ill have to think about.....
    Last edited by iain; 09-16-2001 at 01:31 PM.
    Monday - what a way to spend a seventh of your life

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    2

    Re

    It is for homework but not for a grade.....I have tried and tried but cannot figure it out. Struggling with this one......

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Here are some "hints". Looking at the data, you have a pattern. On the left side, there is a set of numbers (1...3), and on the right, there is a set of numbers (1...3) for each number in the first set. So, you could use to integers to store the data (one for the current number in the first set, and one for the current number in the second). You could then loop over these values, and using 'if' statements within the loop, to print the current number from the first set, a space, and then the current number from the second set, and then a newline ('\n') character.

    I hope this helps.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User Mangesh's Avatar
    Join Date
    Sep 2001
    Posts
    18

    code

    main()
    {
    int i=1,j=0;

    while(1)
    {
    if (j==3)
    {
    i++;
    if (i==4)
    break;
    j=0;
    }
    j=j+1;
    printf("%d %d\n",i,j);
    }
    }

  6. #6
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    No offense to Mangesh, but his code is probably hard for a n00b to understand. I re-wrote it the more logical way (even though it might not be exactly what shibz wants. At least he'll get the idea on what is going on.

    Code:
    #include <stdio.h>
    
    int main(void) 
    {
    
        int i, j;
        
        for (i = 1; i <= 3; i++) 
            for (j = 1; j <= 3; j++) 
                printf("%d ------ %d\n", i, j);
                
        return 0;
    }
    REMINDER: Everybody use code tags

  7. #7
    Registered User Mangesh's Avatar
    Join Date
    Sep 2001
    Posts
    18

    use of nested if's

    Dear biosx,

    Shibz' first sentence :

    >I am new to learning C and was wondering how I can print out the following results using nested if's and only using (1) printf:

    I wrote this code to implement it using nested if's.

    Regards,
    Mangesh.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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