Thread: MxN grid

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    13

    MxN grid

    Hello,

    I am trying to create a grid in C. I want the user to give the M and N, and the program should produce a MxN Grid using the characters + - | . As an example a 3x5 grid should look like:

    MxN grid-grid-jpg

    I managed to solve it. My code works perfect!!:


    Code:
    #include<stdio.h>
    int main()
    {
        int i,j,k,m,n;
        
        printf("Enter number of rows: ");
        scanf("%d",&m);
        
            printf("Enter number of rows: ");
        scanf("%d",&n);
        
        
        for(k=n; k>=1; k--)
        {
        
        for(i=m; i>=1; i--)
        {
            
            printf("+--");
            }
            printf("+");//outer border
            printf("\n");
            for(i=m; i>=1; i--)
        {
            printf("|  ");
        }
        printf("|");//outer border
            printf("\n");
    }
    for(i=m; i>=1; i--)
        {
            
            printf("+--");
            }
            printf("+");//outer border
    }

    a) Any ideas of how I could reduce the above code for the repeated parts?

    b) Is there another way to solve that by only using the characters +-- for the horizontal lines
    and the + | characters for the verticals?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    You can shorten it by using a function.
    Your main problem is bad indentation!
    And since there's no reason for your loops to count backwards it's more usual to count forwards.
    Code:
    #include <stdio.h>
     
    void print_row(int cols, char corner, char line)
    {
        putchar(corner);
        for (int c = 0; c < cols; ++c)
        {
            putchar(line);
            putchar(line);
            putchar(corner);
        }
        putchar('\n');
    }
     
    int main()
    {
        int rows, cols;
        printf("Enter number of rows: ");
        scanf("%d", &rows);
        printf("Enter number of columns: ");
        scanf("%d", &cols);
     
        for (int r = 0; r < rows; ++r)
        {
            print_row(cols, '+', '-');
            print_row(cols, '|', ' ');
        }
     
        print_row(cols, '+', '-');
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suduko Grid For Solving From Already Generated Grid Function
    By Nick Krause in forum C Programming
    Replies: 21
    Last Post: 11-17-2014, 03:16 PM
  2. Help with a grid
    By karimoftos in forum C Programming
    Replies: 6
    Last Post: 04-19-2011, 11:32 AM
  3. 2d grid
    By lord in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2009, 08:06 PM
  4. 4x4 grid in C
    By amorvisa in forum C Programming
    Replies: 7
    Last Post: 10-17-2007, 11:13 PM
  5. grid
    By xlnk in forum Windows Programming
    Replies: 3
    Last Post: 12-14-2002, 08:40 PM

Tags for this Thread