Help with a grid

This is a discussion on Help with a grid within the C Programming forums, part of the General Programming Boards category; Hello, I am completely new to C programming so this may be a very trivial question but i cannot do ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    17

    Help with a grid

    Hello,

    I am completely new to C programming so this may be a very trivial question but i cannot do it. I have to make a simple grid wich displays dots. The tricky part is that the user enters a width and a height and then the grid is generated and printed with that width and height. This grid needs to be stored in an array as well.

    I believe im overcomplicating the problem but this is what i have so far:

    Code:
    # include <stdio.h>
    
    int lx;
    int ly;
    char grid [10] [10];
    int i;
    int j;
    
    int main()
    
    {
    printf ("Insert the width (Lx) and height (Ly) of the grid \n");
    scanf ("%d %d", &lx, &ly);
    
    for (i = 0; i < lx; i++)
    
      { 
       for (j=0; j < ly; j++)
    
        {
          grid [i] [j] = ".";
    
        }
       }
    
    for (i = 0; i < lx; i++)
    
      {
       for (j=0; j < ly; j++)
    
        {
         printf ("%c", grid [i] [j]);
    
        }
       }
    
    }
    The output that im getting is a line of i's rather than a grid. i need these to be dots in a 2d grid.... how do i go about this?

    Thanks in advance.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First question... what happens if your user types 20 x 30 for the grid size?
    "." asks your grid elements to be strings... you should use the single quote (apostropic) version... '.' for assigning your values.
    In your final loop while printing, you need to put a newline inside the outer loop but not inside the inner loop... printf("\n\"); should work.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    You need to print a new line after every row.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    hmmm ok. Ive put a new line in the loop but now its outputing the same thing but in a long column... how do i input the lines to be just after every row?

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Code:
    for (i = 0; i < lx; i++)
    
      {
       for (j=0; j < ly; j++)
    
        {
         printf ("%c", grid [i] [j]);
    
        }
       printf("\n"); /* <-- goes here, the completion of the row loop */
       }

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    As Tater said, inside your outer loop [the for (i = 0...)], but outside your inner loop [the for (j = 0...)].

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    o great thanks you all so much it works fine now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Grid
    By mortalc in forum C Programming
    Replies: 7
    Last Post: 03-12-2011, 05:23 AM
  2. help with the grid
    By wannabec++ in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2010, 01:43 PM
  3. 2d grid
    By lord in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2009, 07:06 PM
  4. 4x4 grid in C
    By amorvisa in forum C Programming
    Replies: 7
    Last Post: 10-17-2007, 11:13 PM
  5. hex grid
    By waterst in forum C Programming
    Replies: 2
    Last Post: 10-30-2002, 02:36 PM

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