You are on page 1of 44

MULTI-DIMENSIONAL

ARRAY
note

#include<stdio.h>
#include<conio.h>
void main()
{
int x=13,y=6,z;
z=x^y;
printf("value %d",z);
getch();
}
13 in binary =1011
6 in binary =0110
Xor for =1101
Multi-Dimensional Arrays
 Multidimensional arrays are derived from the
basic or built-in data types of the C language.

 Two-dimensional arrays are understood as rows


and columns with applications including two-
dimensional tables, parallel vectors, and two-
dimensional matrices.

 Mostly Two-dimensional array are used in


Multi-dimensional array.
Arrays of Greater
D iOm e n si o n
ne-dimensional arrays are linear containers.
[0] [1] [2]

Multi-dimensional Arrays
[2]
[0] [1] [2] [3] [1]
[0]
[0]
[0]
[1]
[1]
[2]
[2]

Two-Dimensional [3]

[0] [1] [2] [3] [4]


Three-dimensional
TWO DIMENSIONAL
ARRAY
CONTENT
 Introduction to two dimensional array

 Declaration

 Initialization

 Input and output of a 2d array

 Storage allocation
Two - Dimensional Arrays
 What is a Two-dimensional array?
Array type Array name
Array dimension = 2
51, 52, 53 Row 1
B= Int b[2][3] = {(51, 52, 53),(54, 55, 56)};
54, 55, 56 Row 2

Two rows First row second row

Col 1 Col 2 Col 3


Three columns

Algebraic notation C notation


Indexes in 2D arrays
 Assume that the two dimensional array called val is
declared and looks like the following:
val Col 0 Col 1 Col 2 Col 3

Row 0 8 16 9 52

Row 1 3 15 27 6

Row 2 14 25 2 10

 To access the cell containing 6, we reference val[1]


[3], that is, row 1, column 3.
7
DECLARATION
 How to declare a multidimensional array?
int b[2][3];

the name of the array to be b


the type of the array elements to be int
the dimension to be 2 (two pairs of brackets [])
the number of elements or size to be 2*3 = 6
Declaration Statement
INITIALIZATION
 How to initialize a Two-Dimensionalarray?
 Initialized directly in the declaration statement
 int b[2][3] = {51, 52, 53, 54, 55, 56};

 b[0][0] = 51 b[0][1] = 52 b[0][2] = 53

 Use braces to separate rows in 2-D arrays.


 int c[4][3] = {{1, 2, 3},

{4, 5, 6},
{7, 8, 9},
{10, 11, 12}};
 int c[ ][3] = {{1, 2, 3},

{4, 5, 6},
{7, 8, 9},
{10, 11, 12}};
Implicitly declares the number of rows to be 4.
Input of Two-Dimensional Arrays

 Data may be input into two-dimensional


arrays using nested for loops interactively
or with data files.
 A nested for loop is used to input elemts in
a two dimensional array.
 In this way by increasing the index value of
the array the elements can be entered in a
2d array.
Output of Two-Dimensional Arrays

 The output of two-dimensional arrays should


be in the form of rows and columns for
readability. Nested for loops are used to print
the rows and columns in row and column
order.

 By increasing the index value of the array the


elements stored at that index value are printed
on the output screen.
Storage Allocation
In storage allocation of array contagious memory is
allocated to all the array elements.
A program to input elements in a
two dimensional array and print it.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3];
int i,j;
printf("enter the elements in the array
\n:");
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
printf("%d",a[i][j]);
} printf("\n");
}
getch();
}
OUTPUT :-
Enter elements in array:
1
2
3
4
5
6
7
8
9
123
456
789
EXAMPLES BASED ON
TWO-DIMENSIONAL
ARRAY
A program to add two matrix
entered by the user and print it.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3]
,c[3 ][3];
int i,j;
printf("enter the elements in both the array1:\n");
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the elements in both the array2: \n");
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d",c[i][j]);
}
printf("\n");
}
OUTPUT:-
Enter elements in array:-
1 3
2 4
3 5
4 6
5 7
6 8
7 9
8 246
9 81012
1 141618
2
Tasks

1- write A program to input a matrix and print its


transpose.
2- write A program to input a matrix and print its
multiplication
3- !x=1+x/1+ .(x^2)\!2+(x^3)/!3+(x^4)/!4+00000
4- write c program that compute temperature in 7
Days for 4 times in one Day
‘*’ , ‘&’  Pointers In C
What is a Pointer ?
Who does a memory look like ?
Address
Locations
X1000

X1004
X1000
X1001
X1008
X1002
X100c X1003

x1010
Operators used in Pointers
Dereferencing Address

(
laV
)foeu
&
(Address of)
Int i=3;

Address of ‘i’ Value of ‘i’

‘&i’ variable
i
‘*i’
3
(Value of i)

X1000 x1004 x1008 x100c x1010 x1014

Address of i
The value ‘3’ is saved in the memory location ‘x100c’
Syntax for pointers
(pointer type declaration)

type *identifier ;
Example

Char *cp ;
Int *ip ;
Double *dp ;
Pointer Assignment

Int i = 1 , *ip ; //pointer declaration


ip = &i ; //pointer assignment
*ip = 3 ; //pointer assignment
ex
#include<stdio.h>
#include<conio.h>
main()
{int x=5;
int *ptr=&x;
printf("location Ptr=%d",ptr);
printf("\nlocation var=%d ",&x);
printf("\nvalue Ptr=%p ",*ptr);
printf("\n value var=%d",x);
getch();
}
ex
#include<stdio.h>
#include<conio.h>
main()
{
int g=5;
int *ptr=&g;
*ptr=32;
printf("\nx=%d ",g);
getch();
}
Pointer Arithmetic
Lets take this example program
#include<stdio.h> b=1
Void main() b=1+4
{ b= 5
Int a [5]={1,2,3,4,5} , b , *pt ;
pt = &a[0];
a[0] a[1] a[2] a[3] a[4]
pt = pt + 4 ;
X1000 x1004 x1008 x100c x1010

b=a[0] ; pt

b+=4 ; a[0] a[1] a[2] a[3] a[4]


b
} X1000 x1004 x1008 x100c x1010
Lets Take an Example and See how pointers work
#include<stdio.h>

#include<conio.h>

void main()

int i=3;

int *j;

j=&i;

printf("i=%d \n",i);

printf("*j=%p\n",*j);

getch();

}
Int i=3; Create an integer variable ‘i’ and initialize it to 3

Int *j; Create a pointer variable ‘j’- create value of‘j’

j = &i; Initialize the pointer value of ‘j’ to the address of ‘i’

variabl
es Int *j Int i

M
e 3
m x100c
o
r
y
X1000 x1004 x1008 x100c x1010 x1014
Printf(“i=%d” i);
Printf(“*j=%p” *j);
Output
screen
i=3
*j=3
We know j=&i
So  *j=*(&i) value of (address of
i)
(i.e.) value in address (x100c)

Int *j Int i

M
e 3
m x100c
o
r
y
X1000 x1004 x1008 x100c x1010 x1014
Predict the output of this code

#include<stdio.h>

#include<conio.h>

void main()

int num=10;

int* pnum=NULL;

pnum = &num;

*pnum += 20;

printf("\nNumber = %d", num);

printf("\nPointer Number = %p", *pnum);


Number
= 10
Pointer
Number
= 30
Task 1
int a[10] = {1,2,3,4,5,6,7,8,9,12} ,*p, *q , i;
p = &a[2];
q = &a[5];
i = *q - *p;
Printf(“The value of i is %d” i );
i = *p - *q;
Printf(“The value of i is %d” i );
a[2] = a[5] = 0;
Printf(“The value of i is %d” i );
Task2

int a[10] = { 2,3,4,5,6,7,8,9,1,0 }, *p, *q;


p = &a[2];
q = p + 3;
p = q – 1;
p+ + ;
Printf(“The value of p and q are : %p , %p” *p,*q);

You might also like