Printing a 2D array in C

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.

--------------Solutions-------------

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;
}

...
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)

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.

Category:multidimensional array Time:2010-04-30 Views:1

Related post

  • How do you print an associative array in dtrace? 2010-02-23

    The question pretty much sums it up. "dtrace 'print an associative array'" has exactly one google hit and the similar searches are equally useless. EDIT: If I were to use an aggregation, I'm not aware that I'd still be able to remove entries. My appl

  • print two dimensional array of strings as String 2010-03-07

    I know how to do toString method for one dimensional arrays of strings, but how to print two dimensional array ? With 1D I do it this way : public String toString() { StringBuffer result = new StringBuffer(); res = this.magnitude; String separator =

  • Pretty-printing of numpy.array 2010-05-23

    I'm curious, whether there is any way to print formated numpy.arrays, e.g., in the way similar to this: x = 1.23456 print '%.3f' % x If I want to print the numpy.array of floats, it prints several decimals, often in 'scientific' format, which is rath

  • How do you print C dynamic array contents from dbx? 2010-07-15

    How do you print C dynamic array contents from dbx? i tried print aj[2..10:1] but it didnt work --------------Solutions------------- Just say "print aj" Dbx will use dynamic information to print the array. Are you asking about array slicing syntax in

  • Is there a simpler way of printing a PHP array in a string? 2010-09-26

    As you know, a good programmer is a lazy programmer, but I'm just lazy. My question is this: Is there a simpler way to print out an element of an array (from a MySQL query) in a PHP echo statement? I usually do this: echo "string start " . $array['el

  • How do I print a Java array in gdb? 2011-01-31

    I compiled a very simple java program with gcj-4.4 and -o option. I loaded it in gdb-7.2 and tried to do some debugging. I noticed at I could print the variables in int type but I could not print an array of int. I received this error message: (gdb)

  • print a 2D array in C 2011-03-03

    I was wondering if it was possible to print a 2D array in C like it is in python. For example, if I have int array1[10][10]; then fill in the array then printf("%li", array1) does not seem to work. In C, is there something like printf that can print

  • Is there a way to have printf() properly print out an array (of floats, say)? 2011-12-09

    I believe I have carefully read the entire printf() documentation but could not find any way to have it print out, say, the elements of a 10-element array of float(s). E.g., if I have float[] foo = {1., 2., 3., ..., 10.}; Then I'd like to have a sing

  • perl printing hash of arrays with out Data::Dumper 2012-01-05

    Here is the code, I know it is not perfect perl. If you have insight on how I an do better let me know. My main question is how would I print out the arrays without using Data::Dumper? #!/usr/bin/perl use Data::Dumper qw(Dumper); use strict; use warn

  • How to print a full array in MIPS, and sum certain integers of an array 2012-02-08

    I am having an issue with the following code I am using for an assignment.. Note, I do not want anybody just giving me code, I am really trying to understand MIPS. I am using the QTSpim simulator to run my MIPS code. The following code is supposed to

  • How to print a nested array inside the value of a cloned input object? 2012-03-13

    I have a fiddle code: //create array with values thisArray= ['',[]]; thisArray[0] = ['numbers',['one','two','three','four','five']]; // have a clean clone of input var newElem = $('#input').clone(); //search through aray and print the nested array in

  • How do I print out an array? 2014-11-20

    I'm almost done with my practice except that I can't seem to figure out on how to display the array. The practice is to take a group of strings from the user with the length of 10 and check if there's a number 0-9 in the string that corresponds to th

  • How can I print a multidimensional array in Perl? 2015-03-19

    I am trying to print a multidimensional array (matrix) in Perl passing reference to array to subroutine. Here is my code: sub print_matrix(\@) { my $array = shift; for my $i ( 0 .. $#{ $array } ) { my $row = $array[$i]; for my $j ( 0 .. $#($row) ) {

  • Ruby: How to make IRB print structure for Arrays and Hashes 2009-03-31

    When I make a new array/hash in irb, it prints out a nice format to show the structure, ex. ["value1", "value2", "value3"] {"key1" => "value1"} ... but when I try to print out my variables using puts, I get them collapsed: value1 value2 value3 key

  • Print two-dimensional array in spiral order 2009-04-07

    How do I print a 5×5 two-dimensional array in spiral order? Is there any formula so that I can print an array of any size in spiral order? --------------Solutions------------- The idea is to treat the matrix as a series of layers, top-right layers an

  • Efficient Way to Print MIPS Int Array 2009-11-10

    I'm working on a homework assignment translating a C program we wrote to MIPS. My question is about general MIPS coding and not project specific issues though. I've run into an issue with printing my output. I have an array and output string declared

  • How to print a single array element in Objective-C? 2010-02-05

    How to print a array element at particular index in Objective-C?My code looks like this, NSString *String=[NSString StringWithContentsOFFile:@"/User/Home/myFile.doc"]; NSString *separator = @"\n"; NSArray *array = [String componetntsSeparatedByString

  • printing from multidimentional arrays? 2010-05-04

    I have a multi array that looks like this: $_SESSION['cartItems']['quantity'] How do I print out its values? print_r won't work, unless $_SESSION doesn't support multi-dimensional arrays? --------------Solutions------------- print_r($_SESSION['cartIt

  • printing multi dimentional array 2010-05-12

    i have this multi dimentional array that i want to print into a table having each record/item go into its own row but it goes column wise. this is the output that im getting: http://mypetshopping.com/product.php ps: the value of $product will by dyna

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.461 (s). 13 q(s)