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?