Nested While

This is a discussion on Nested While within the C Programming forums, part of the General Programming Boards category; Hello there I am stuck on how to use nest while!! This is the question: Write a program that reads ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    20

    Question Nested While

    Hello there

    I am stuck on how to use nest while!! This is the question:

    Write a program that reads in two integers m and n, and prints out an m-by-n rectangle of
    asterisks.
    (a) Use two nested while loops.
    (b) Use two nested for loops.

    I did the for loops okay but cannot figure out how the while works.

    Code:
    #include <stdio.h>
    int main()
    {
        int m, n;
        int i=0;
        int j=0;
    
    
        printf("Input value for m and n: \n");  //prompts
        scanf("%d %d", &m, &n);                 //inputs
    
    
        while(i <= m)                           // Size of m
        {
            i++;
            printf("*");
        }
    
    
        while(j < n)
        {
           j++;
           printf("*");
        }
    
    
        return 0;
    }

    What am i doing wrong?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It's probably easier to start with just giving height. Then from that, start with one star. For each iteration space--; and stars += 2; since for each level 2 stars is added and one space char at each side disappears. Make a print_stars(int s); and print_space(int s); function.

    Then loop that until height. In the loop body:

    print_space
    print_stars
    print_space

    update, stars and space vars
    Last edited by Subsonics; 10-01-2011 at 01:04 PM. Reason: spelling

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Ok. I will try to figure out the things you said as actually am new to C programming.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The idea is to break down the problem, into smaller problems. You could potentially implement these loops inside your loop instead of using functions but you would end up writing similar code three times instead of once then calling that code.

    Edit: in this case with my example twice actually. You could however create a generic char printing function that prints a char, x amount of times, example: print_char(char c, int times);
    Last edited by Subsonics; 10-02-2011 at 01:09 PM.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    I got that working


    Code:
    #include <stdio.h>
    int main()
    {
        int m, n;
        int i=0;
        int j=0;
    
    
        printf("Input value for m(rows) and n(columns): \n");  //prompts
        scanf("%d %d", &m, &n);                                      //inputs
    
    
    
    
        j=1;                                        // Initialize to 1
        while(j <= m)                           // Size of m
        {
            i=1;
            while(i<=n)
            {
                printf("*");
                i++;
            }
    
    
            printf("\n");
            j++;
        }
    
    
    
    
    
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can switches be nested?
    By JM1082 in forum C Programming
    Replies: 6
    Last Post: 03-14-2011, 04:13 PM
  2. Nested structs
    By Sir Andus in forum C Programming
    Replies: 5
    Last Post: 11-28-2006, 09:31 AM
  3. nested #includes
    By tasha302 in forum C++ Programming
    Replies: 10
    Last Post: 11-12-2006, 08:03 PM
  4. Help About Nested Loop
    By Antigloss in forum C Programming
    Replies: 16
    Last Post: 10-17-2006, 10:18 AM
  5. Help with using nested For
    By webbizdesign in forum C Programming
    Replies: 5
    Last Post: 09-29-2003, 10:50 PM

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