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

# Amity School of Engineering & Technology

Functions
Amity School of Engineering & Technology

## • Functions are of two types:

1 predefined like printf main getch etc
2 user defined which we declare according
to our need.
While using functions we follow 3 steps:
1 function declaration.
2 function calling.
3 function definition.
Amity School of Engineering & Technology

## void square(); \\ declaration

void main()
{
square(); \\calling
getch();
}
void square() \\ definition
{ int a,b;
Amity School of Engineering & Technology

Arrays
Amity School of Engineering & Technology

• Declaration of array:
1-Data type
2-name of array.
3-size of array.

## Syntax- datatype arrayname[size];

e.g. Int a[10];
char c[10];
Amity School of Engineering & Technology

## • Memory locations are created equal to

size specified.
• All memory locations occupy continues
memory location.
• Memory locations are numbered
sequentially from zero to n-1.
• All locations share the same name as that
we declared earlier but index value is diff.
Amity School of Engineering & Technology

• Int a[5]={10,2,6,7,4};
• a[0]=10 ,a[1]=2, a[2]=6.......
• char c[6]={‘h’,’e’,’l’,’l’,’o’,’\o’};
=“hello”;
Amity School of Engineering & Technology
Wap to take input of 10 nos and
display them
int a[10],i,m;
for(i=0;i<=9;i++)
{
printf(“Enter nos”);
scanf(“%d”,&a[i]);
}
for(i=0;i<=9;i++)
{ printf(“%d \n”,a[i]);
}
Amity School of Engineering & Technology

## Enter 5 nos. Calculate their

square and cube.
for(i=0;i<=4;i++)
{
printf(“no is %d sq is %d cube is %d” ,a[i],
a[i]*a[i], a[i]*a[i]*a[i]);
}
Amity School of Engineering & Technology
Check largest no in 10 numbers
int a[10],i,m;
for(i=0;i<=9;i++)
{printf(“Enter nos”);
scanf(“%d”,&a[i]);
}
M=a[0];
for(i=0;i<=9;i++)
{ if(a[i]>m)
M=a[i];
}
Amity School of Engineering & Technology

Matrices (2D-array)

## • A matrix is a set of numbers arranged in a grid with rows and columns.

• A matrix is defined using a type declaration statement.
– datatype array_name[row_size][column_size]; 4

– int matrix[3][4]; 1
0

Row 0 4 1 0 2 2
-1

Row 1 -1 2 4 3 2
4

Row 2 0 -1 3 1 3
0
-1
3

## Column 0 Column 1 Column 2 Column 1 11

3
in memory
Amity School of Engineering & Technology

## Accessing Array Elements

int matrix[3][4];
• matrix has 12 integer elements
• matrix[0][0] element in first row, first column
• matrix[2][3] element in last row, last column
• matrix is the address of the first element
• matrix[1] is the address of the Row 1
• matrix[1] is a one dimensional array (Row 1)

12
Amity School of Engineering & Technology

Initialization
int x[4][4] = { {2, 3, 7, 2},
{7, 4, 5, 9},
{5, 1, 6, -3},
{2, 5, -1, 3}};
int x[][4] = { {2, 3, 7, 2},
{7, 4, 5, 9},
{5, 1, 6, -3},
{2, 5, -1, 3}};

13
Amity School of Engineering & Technology

Initialization

int i, j, matrix[3][4];
for (i=0; i<3; i++)
{ for (j=0; j<4; j++)
{ matrix[i][j] = i; matrix[i][j] = j;
j j
0 1 2 3
0 1 2 3
0 0 0 0
0
0 0 1 2 3
1 1 1 1
i 1
i 1 0 1 2 3
2 2 2 2 2 0 1 2 3
14
2
Amity School of Engineering & Technology

Exercise
• Write the nested loop to initialize a 2D
array as follow

0 1 2 int i, j, x[4][3];
1 2 3 for(i=0; i<4; i++)
2 3 4 for(j=0; j<3; j++)
3 4 5 x[i][j] = i+j;

15
Amity School of Engineering & Technology

Max in 2D
• Find the maximum of int matrix[3][4]

0 1 2 3
int max = matrix[0][0];
0 0 1 0 2
for (i=0; i<3; i++)
1 -1 2 4 3
for (j=0; j<4; j++)
2 0 -1 3 1
if (matrix[i][j] > max)
max = matrix[i][j];

16
Amity School of Engineering & Technology

Matrix sum

## • Compute the addition of two matrices

0 1 2 3 0 1 2 3 0 1 2 3
0 0 1 0 2 0 3 -1 3 1 0 3 0 3 3

1 -1 2 4 3 + 1 1 4 2 0 = 1 0 6 6 3

2 0 -1 3 1 2 2 1 1 3 2 2 0 4 4

17
Amity School of Engineering & Technology

solution

int matrix1[3][4],
matrix2[3][4],
sum[3][4];
// initialize matrix1 and matrix2

## for (i=0; i<3; i++)

for (j=0; j<4; j++)
sum[i][j]= matrix1[i][j]+matrix2[i][j];

18
Amity School of Engineering & Technology

• Transpose of a matrix...
Amity School of Engineering & Technology

Matrix multiplication

## double a[3][2], b[2][4], c[3][4];

• Find c = a * b;
3 4 22 29 45 35
2 3 7 1
5 2 x = 18 40 47 21
4 5 6 8
1 6 26 33 43 49
3*2 + 4*4=22 3*3 + 4*5=29 3*7 + 4*6=45 3*1 + 4*8=35

## 5*2 + 2*4=18 5*3 + 2*5=40 5*7 + 2*6=47 5*1 + 2*8=21

1*2 + 6*4=26 1*3 + 6*5=33 1*7 + 6*6=43 1*1 + 6*8=49 20
Amity School of Engineering & Technology

for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{ c[i][j]=0;
for(k=0;k<2;k++)
{
C[i][j]=c[i][j]+a[i][k]*b[k][j];
} } }
Amity School of Engineering & Technology

• i=0,j=o,k=o;
• c[0,0]=c[0,0]+a[0.0]*b[0,0]
• i=0,j=0,k=1
• C[0,0]=c[0,0]+a[0,1]*b[1,0]
• 2nd element
• i=0,j=1,k=0;
• c[0,1]=c[0,1]+a[0.0]*b[0,1]
• i=0,j=1,k=1
• C[0,1]=c[0,1]+a[0,1]*b[1,1]
Amity School of Engineering & Technology

• 3rd element
• i=0,j=2,k=o;
• c[0,2]=c[0,2]+a[0.0]*b[0,2]
• i=0,j=2,k=1
• C[0,2]=c[0,2]+a[0,1]*b[1,2]
• 4th element
• i=0,j=3,k=0;
• c[0,3]=c[0,3]+a[0.0]*b[0,3]
• i=0,j=1,k=1
• C[0,1]=c[0,1]+a[0,1]*b[1,3]
Amity School of Engineering & Technology

• 5th element
• i=1,j=0,k=o;
• c[1,0]=c[1,0]+a[1,0]*b[0,0]
• i=1,j=0,k=1
• c[1,0]=c[1,0]+a[1,1]*b[1,0]
• 6th element
• i=1,j=1,k=o;
• c[1,1]=c[1,1]+a[1,0]*b[0,1]
• i=1,j=0,k=1
• c[1,1]=c[1,1]+a[1,1]*b[1,1]
Amity School of Engineering & Technology

• 7th element
• i=1,j=2,k=o;
• c[1,2]=c[1,2]+a[1,0]*b[0,2]
• i=1,j=2,k=1
• c[1,2]=c[1,2]+a[1,1]*b[1,2]
• 8th element
• i=1,j=3,k=o;
• c[1,3]=c[1,3]+a[1,0]*b[0,3]
• i=1,j=3,k=1
• c[1,3]=c[1,3]+a[1,1]*b[1,3]