views:

186

answers:

3

how would I print a 2d array in c using scanf for user input, array called grid[ ][ ] and a for loop?

say if the user types in 3 5, the output will be:

.....
.....
.....

Here is the code that I have written so far (newbie here):

#include <stdio.h>

#define MAX 10

int main()
{
    int grid[MAX][MAX];
    int row, col;
    int i,j;

    printf("Please enter your grid size: ");
    scanf("%d %d", &row, &col);

    for (i=0; i<MAX; i++)
        for //i gave up here


}

This is only a little part of the whole stage of my task:

Enter number of rows and columns followed by list of words (hit enter twice to end list): 10 15
quick
brown
fox
jumped
over
lazy
dog

00  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
01  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
02  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
03  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
04  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
05  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
06  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
07  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
08  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
09  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
    0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 
  0. quick
  1. brown
  2. fox
  3. jumped
  4. over
  5. lazy
  6. dog

functions allowed and should be included in the code: string functions - strlen(),strcpy(), strcat(), strchr(), strcmp(),strstr()

must use 2d array

must use fgets for words. Out put must match the exact format.

A: 
...
for(int i=0;i<3;i++){ //Rows
for(int j=0;j<5;j++){ //Cols
 printf("%<...>\t",var);
}
printf("\n");
}
...

considering that <...> would be d,e,f,s,c... etc datatype... X)

pojomx
A: 

First you need to input the two numbers say num_rows and num_columns perhaps using argc and argv then do a for loop to print the dots.

int j=0;
int k=0;
for (k=0;k<num_columns;k++){
   for (j=0;j<num_rows;j++){
       printf(".");
   }
 printf("\n");
 }

you'd have to replace the dot with something else later.

shuttle87
The two answers given are great, but they don't use the 2d array grid...?
wello horld
OP mentioned that input is done with scanf
NomeN
And yet, they do show how to setup nested loops that you would need to go through the grid. We can't, in good concience, give you the answer directly.
Michael Dorgan
Seriously? :( i have many more questions to come... this is only a little chunk of my assignment.
wello horld
We want to guide you in the right direction without just answering your assignment directly :)Perhaps include the question in the original post.
shuttle87
okay,i will do that
wello horld
+1  A: 

Is this any help?

#include <stdio.h>

#define MAX 10

int main()
{
    char grid[MAX][MAX];
    int i,j,row,col;

    printf("Please enter your grid size: ");
    scanf("%d %d", &row, &col);


    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            grid[i][j] = '.';
            printf("%c ", grid[i][j]);
        }
        printf("\n");
    }

    return 0;
}
NomeN
Yeah, you've pretty much hit the nail on the head. Thanks! Time for that dreaded list of words...
wello horld
@wello horld, try the small exercises from your text book or find an absolute beginners tutorial online and do those exercises. That is the best way too learn. Additional tip, google is your friend! For example a good page for fgets with a code sample: http://www.cplusplus.com/reference/clibrary/cstdio/fgets/
NomeN