Вы находитесь на странице: 1из 5

C allows us to define such tables of items by using two-dimensional arrays.

Definition: A list of items can be given one variable name using two subscripts and such a variable is called a two subscripted variable or a two dimensional array. Declaring 2-D Arrays Syntax: type array_name [row_size][column_size]; Examples: int marks[4][5]; float sales[12][25]; double matrix[100][100]; The previous example of student can represent as:

Initializing Two- Dimensional Arrays: Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces. int table[2] [3] = {0,0,0,1,1,1}; This initializes the elements of first row to zero and the second row to one. This initialization is done row by row. The above statement can be equivalently written as int table[2][3] = {{0,0,0},{1,1,1}}; we can also initialize a two dimensional array in the form of a matrix as shown. int table[2][3] = { {0,0,0}, {1,1,1} }; Commas are required after each brace that closes of a row, except in case of last row. If the values are missing in an initializer, they are automatically set to zero. Accessing Elements of a 2-D Array Similar to that for 1-D array, but use two indices. First indicates row, second indicates column. Both the indices should be expressions which evaluate to integer values. How is a 2-D array is stored in memory? Starting from a given memory location, the elements are stored row-wise in consecutive memory locations.

x: starting address of the array in memory c: number of columns k: number of bytes allocated per array element a[i][j] - is allocated memory location at address x + (i * c + j) * k

How to read the elements of a 2-D array? By reading them one element at a time for (i=0; i<row; i++) for (j=0; j<col; j++) scanf (%f, &a[i][j]); The ampersand (&) is necessary. The elements can be entered all in one line or in different lines. How to print the elements of a 2-D array? By printing them one element at a time. for (i=0; i<row; i++) for (j=0; j<col; j++) printf (\n %f, a[i][j]); The elements are printed one per line. for (i=0; i<row; i++) for (j=0; j<col; j++) printf (%f, a[i][j]); The elements are all printed on the same line. for (i=0; i<row; i++) { printf (\n); for (j=0; j<col; j++) printf (%f , a[i][j]); } The elements are printed nicely in matrix form. Input elements into an 2-D array and display it
#include<stdio.h> main() { int arr[3][3],i,j; printf("Enter elements into array::"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&arr[i][j]); }

for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d\t",arr[i][j]); } printf("\n"); }

Example: Matrix Addition


#include <stdio.h> main() { int a[100][100], b[100][100], c[100][100], p, q, m, n; scanf (%d %d, &m, &n); for (p=0; p<m; p++) for (q=0; q<n; q++) c[p]q] = a[p][q] + b[p][q]; for (p=0; p<m; p++) { printf (\n);

sum of the elements of a matrix


#include<stdio.h> main() { int mat1[3][4],i,j,sum=0; printf("Enter the elements::\n"); for(i=0;i<3;i++) { for(j=0;j<4;j++) { scanf("%d",&mat1[i][j]); } } printf("Matrix is::\n"); for(i=0;i<3;i++) {

for(j=0;j<4;j++) { printf("%d\t",mat1[i][j]); //sum=sum+mat1[i][j]; } printf("\n"); } for(i=0;i<3;i++) { for(j=0;j<4;j++) { sum=sum+mat1[i][j]; } printf("\n"); } printf("sum is %d",sum);

Multiplication of two matrix No of columns in first matrix should be equal to number of number of rows in second matrix //Program for multiplication of two matrix
#include<stdio.h> main() { int mat1[2][2],mat2[2][2],mat3[2][2]; int i,j,k; printf("Enter element of matrix1::\n"); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&mat1[i][j]); printf("Enter element of matrix2::\n"); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&mat2[i][j]); /*multiplication*/ for(i=0;i<2;i++) for(j=0;j<2;j++) {

mat3[i][j]=0; for(k=0;k<2;k++) mat3[i][j]=mat3[i][j]+(mat1[i][k]*mat2[k][j]); } printf("Result is::"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("%5d",mat3[i][j]); } printf("\n"); }}

Write a program to display 2-Dimensional array elements together with their addresses. # include<stdio.h> printf(Row 0\t) # include<conio.h> for( i=0; i<3; i++) void main( ) { { int i,j; for( j=0; j<3; j++) int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; printf(%d [%u], a[ i ][ j ], &a[ i ][ j clrscr( ); ]); printf(Array Elements and address \n \n); printf( \n Row %d , i+1); printf(col-0 col -1 col-2 \n); } prinf(-------- -------- ------- \ n); getch( ); } program to find transpose of matrix #include<stdio.h> #include<conio.h> void main() { int m[100][100],r,c,i,j; clrscr(); printf("How many ROW and COLUM\n"); scanf("%d%d",&r,&c); printf("\nEnter the Matrix\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&m[i][j]); } }
Wap a program to print the pattern * ** *** **** ***** #include<stdio.h> #include<conio.h> Void main() { int n, row, col; WAP to print the pyramid of stars #include<stdio.h> #include<conio.h> main() { int row, col, n, temp; printf("Enter the rows in pyramid of stars to print "); } printf("\nThe transpose is\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf("%d ",m[j][i]); } printf("\n"); } getch(); }

printf("Enter number of rows\n"); scanf("%d",&n); for ( row = 1 ; row <= n ; c++ ) { for( col = 1 ;col <= c ; k++ ) printf("*"); printf("\n");
} getch(); }

{ for ( col = 1 ; col < temp ; c++ ) printf(" "); temp--; for ( c = 1 ; c <= 2*row - 1 ; c++ ) printf("*"); printf("\n"); } getch();

scanf("%d",&n); temp = n; for ( row = 1 ; row <= n ; row++ ) WAP to print the
* *** ***** *** *

space--; for (c = 1; c <= 2*k-1; c++) printf("*"); printf("\n"); } space = 1; for (k = 1; k <= n - 1; k++) { for (c = 1; c <= space; c++) printf(" "); space++; for (c = 1 ; c <= 2*(n-k)-1; c++) printf("*"); printf("\n"); } return 0; }

#include <stdio.h> #include<conio.h> int main() { int n, c, k, space = 1; printf("Enter number of rows\n"); scanf("%d", &n); space = n - 1; for (k = 1; k <= n; k++) { for (c = 1; c <= space; c++) printf(" ");

Вам также может понравиться