Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Multidimensional Arrays in C programming

August 27, 2013 Posted by: GPA Views: 1063 0 comments

Problem

Multidimensional arrays in C.

Solution

Multidimensional arrays are tables of values consisting of information arranged in rows and columns. To create table elements, we must specify two subscripts: the first identifies the element's row and the second identifies the element's column. Arrays that have two subscripts are called double-subscripted arrays. Multidimensional arrays can have more than two subscripts. On the figure below you can see a double-subscripted array a. The array contains three rows and four columns. A multidimensional array can be initialized like as singlesubscripted array. A double-subscripted array int b[2][2] could be initialized:

first way: int b[ 2 ][ 2 ] = { { 5, 4 }, { 6, 9 } };

second way:

int b[2][2];

b[0][0]=5;

b[0][1]=4;

b[1][0]=6;

b[1][1]=9; 

 

 

multidimensional arrays in c programming1

In this example we use 2-dimensional array and pass it to function "print".

Example:

   

#include <stdio.h>
#include <stdlib.h>
void print(int a[][ 3 ]); //function prototype
int main(int argc, char *argv[])
{

   int inputarray[ 2 ][ 3 ] = { { 3, 2, 5 }, { 6, 7, 5 } };//initialize inputarra
printf("Values in inputarray are:\n" );
   print(inputarray);//call function print
   system("PAUSE");
   return 0;
}
void print(int a[][ 3 ])
{
    int i; //row counter
    int j; //column counter
    for ( i = 0; i <= 1; i++ ) {
        //output column values
         for ( j = 0; j <= 2; j++ ) {
             printf( "%d ", a[i][j] );
         }
         printf( "\n" ); //start new line
    }
} 

Output

multidimensional arrays in c

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe