0% found this document useful (0 votes)
5 views

Unit 2 Array

Uploaded by

JUSTIN RAJ S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Unit 2 Array

Uploaded by

JUSTIN RAJ S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

CSE101 –

Problem Solving &


Programming in C
Unit II
Arrays
Need for Arrays
# include<stdio.h> • n=5
int main() { • 2, 4, 1, 8, 9
int n, i=0, sum=0, a;
• When i=0, a=2
printf(“enter the value of n”);
scanf(“%d”, &n); • When i=1, a=4
printf(“enter the values to find sum”); • When i=2, a=1
while(i<n) { • When i=3, a=8
scanf(“%d ”, &a); • When i=4, a=9
sum=sum+a;
• When i=5, loop ends
i++;
} • What is the value of a after the
loop terminates?
printf(“sum=%d”,sum);
return 0; • Does the program remember all the
} values entered for a?
Introduction
• An array is an identifier that refers to a collection of data items
• Has same name
• All items of are of same type (homogeneous data type)
• The individual data items are represented by their corresponding
array-elements
• The individual array elements are distinguished from one another by
the value that is assigned to a subscript
Introduction
• In an n-element array
• the array elements are x [0], x [1] , x [2], . . . ,x [n – 1]

• The value of each subscript can be expressed as


• an integer constant or
• an integer variable or
• a more complex integer expression
Introduction
• The number of subscripts determines the dimensionality of the array
• X[i] refers to an element in the one-dimensional array x
• y[i][j] refers to an element in the two-dimensional array y
• Higher-dimensional arrays can be also be formed, by adding additional
subscripts z[i][j][k]
Defining an Array
• Arrays are defined like ordinary variables but will have a size specified
• For a one-dimensional array, the size is specified by a positive integer
constant, enclosed in square brackets
• data- type array[ integer constant];
• E.g, int a[10] ;
• It is sometimes convenient to define an array size in terms of a
symbolic
#define size 20
int a[size];
Defining an Array
• Arrays also can be initialized
• data- type array[ integer constant] = {value 1, value 2, . . . , value n} ;
• int digits[10] = {1, 2, 3, 4, 5 , 6, 7, 8, 9, 10};
• float x[6] = {0, 0.25, 0, -0.50, 0, 0};
• char color[3] = {‘R’, ‘E’, ‘D’} ;
• digits[0]=?
• digits[3]=?
• x[3]=?
Defining an Array
• int a[10]={3,3,2};
• a[1]=?, a[5]=?
• When initial values are included as a part of an array, array size need
not be specified explicitly
• int a[ ] = {2,5,4,9};
• The array size will automatically be set equal to the number of initial
values
program to find average marks obtained by a class of 30
main( )
students in a test.
{
int avg, sum = 0 ;
int i ;
int marks[30] ; /* array declaration */
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ; /* store data in array */
}
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ; /* read data from an array*/
avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg ) ;
}
Array Declaration
• an array needs to be declared
• so that the compiler will know what kind of an array and how large an
array we want.
• Above program: int marks[30] ;
• Here, int specifies the type of the variable
• and the word marks specifies the name of the variable.
• The number 30 tells how many elements of the type int will be in our
array.
• This number is often called the ‘dimension’ of the array. The bracket ( [ ] )
tells the compiler that we are dealing with an array.
Entering data into an array
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "\nEnter marks: " ) ;
scanf ( "%d", &marks[i] ) ;
}
• The for loop causes the process of asking for and receiving a student’s
marks from the user to be repeated 30 times.
• The first time through the loop, i has a value 0, so the scanf( ) function
will cause the value typed to be stored in the array element marks[0],
the first element of the array.
• This process will be repeated until i becomes 29.
Reading data from an array
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ;
avg = sum / 30 ;

printf ( "\nAverage marks = %d", avg ) ;

• The for loop is much the same, but now the body of the loop causes
each student’s marks to be added to a running total stored in a
variable called sum.
• When all the marks have been added up, the result is divided by 30,
the number of students, to get the average.
Processing an array
• Single operations which involves entire arrays are not permitted in c.
• If a and b are similar array (i.e., same data type, same dimensionality and
same size), assignment operations, comparisons operations etc. may be
carried out on an element-by-element basis.
• This process done by loop→ each pass in loop equal to process one array
element.
• Thus number of passes equals to number of array elements to be
processed.
Program to calculate the average of n numbers, then compute the
main( ) deviation of each number about average
{
int n, count;
float avg, d, sum=0;
float list[100];
printf("enter n value:");
scanf("%d", &n);
for(count=0; count<n; count++)
{
printf("i=%d", count++)'
scanf("%f", &list[count]);
sum+= list[count];
}
avg = sum/n;
printf("\nthe average is %.2f, avg");
for(count=0; count<n; count++)
{
d = list[count]-avg;
printf("i=%d x=%.2f d=%.2f", count ++, list[count],d);
}
}
Passing arrays to functions
• An entire array can be passed to a function as an
argument
• To pass an array to a function, the array name must
appear by itself.
• When declaring a one dimensional array as a formal
argument, the array name is written with a pair of empty
square brackets.
• When writing the function prototype that include array
arguments, an empty pair of square backets must follow
the name of each array arguments, thus indicating that
the argument is an array.
Example
float avg(int a, float x[]);
main()
{
float avg (int a, float x[])
int n;
{
float avg;
………
float list[100];
}
.....
avg = avg(n,list);
........
}
Searching
int arr[5] = {3,4,5,6,7};
1010 1014 1018 1022 1026

3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]

Search Key 5
Searching
int arr[5] = {3,4,5,6,7};
1010 1014 1018 1022 1026

3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Linear Search
int arr[5] = {3,4,5,6,7};
arr[0] = 5?

3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]

Search Key 5
Linear Search
int arr[5] = {3,4,5,6,7};
arr[1] = 5?

3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]

Search Key 5
Linear Search
int arr[5] = {3,4,5,6,7};
arr[2] = 5?

3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]

Search Key 5
Linear Search
int arr[5] = {3,4,5,6,7}; Variable i – subscript
i = i+1 i = i+1

i=0 i=1 i=2


arr[0] = 5? arr[1] = 5? arr[2] = 5?

3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Linear Search
int arr[5] = {3,4,5,6,7}; Search key = 8
i = i+1 i = i+1 i = i+1 i = i+1

i=0 i=1 i=2 i=3 i=4


arr[0] = 8? arr[1] = 8? arr[2] = 8? arr[3] = 8? arr[4] = 8?

3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
#include<stdio.h> Linear Search
int main()
{ for(i=0;i<=9;i++)
int a[10],i,flag=0,searchKey; {
printf("Enter 10 integers: if(a[i]==searchKey)
\n"); printf("\n search key (%d) found at %d
for(i=0;i<=9;i++) position",searchKey,i+1); flag=1;
{ break;
scanf("%d", &a[i]); }
} if((i= =10)&&(flag= =0))
printf("The array elements are: \n"); printf("\n search key (%d) is not found",
for(i=0;i<=9;i++) searchKey);
{ }
printf("%d\t", a[i]);
}
printf("\n Enter the element to be
searched: ");
scanf("%d",&searchKey);
Finding Maximum
int arr[5] = {3,4,5,6,7};
1010 1014 1018 1022 1026

3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]

Maximum =
?
Finding Maximum
int arr[5] = {3,4,5,6,7};
1010 1014 1018 1022 1026

3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Finding Maximum
int arr[5] = {3,4,5,6,7}; Assume, maximum= first element in array
maximum=a[0]
arr[1] > maximum?

3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]

Then, maximum= 4 i.e. maximum=a[1]


Finding Maximum
int arr[5] = {3,4,5,6,7}; maximum=4

arr[2] > maximum?

3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]

Then, maximum= 5 i.e. maximum=a[2]


Finding Maximum
int arr[5] = {3,4,5,6,7}; maximum=5

arr[3] > maximum?

3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]

Then, maximum= 6 i.e. maximum=a[3]


Finding Maximum
int arr[5] = {3,4,5,6,7}; maximum=6

arr[4] > maximum?

3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]

Then, maximum= 7 i.e. maximum=a[4]


Finding Maximum
int arr[5] = {3,4,5,6,7}; max = a[0]
Max=a[1] Max=a[2] Max=a[3] Max=a[4]
i = i+1 i = i+1 i = i+1
i=1 i=2 i=3 i=4
arr[1] > max? arr[2] > max? arr[3] > max? arr[4] > max?

3 4 5 6 7
arr[1] arr[3] arr[4]
Finding Maximum
int arr[5] = {3,4,8,6,7};
1010 1014 1018 1022 1026

3 4 8 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Finding Maximum
int arr[5] = {3,4,8,6,7}; Assume, maximum= first element in array
maximum=a[0]
arr[1] > maximum?

3 4 8 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]

Then, maximum= 4 i.e. maximum=a[1]


Finding Maximum
int arr[5] = {3,4,8,6,7}; maximum=4

arr[2] > maximum?

3 4 8 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]

Then, maximum= 8 i.e. maximum=a[2]


Finding Maximum
int arr[5] = {3,4,8,6,7}; maximum=8

arr[3] > maximum?

3 4 8 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]

Then, maximum= 8
Finding Maximum
int arr[5] = {3,4,8,6,7}; maximum=8

arr[4] > maximum?

3 4 8 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]

Then, maximum= 8
Finding Maximum
int arr[5] = {3,4,8,6,7}; max = a[0]
Max=a[1] Max=a[2]
i = i+1 i = i+1 i = i+1
i=1 i=2 i=3 i=4
arr[1] > max? arr[2] > max? arr[3] > max? arr[4] > max?

3 4 8 6 7
arr[1] arr[2] arr[3] arr[4]
Ref: Programming with C, Byron S. Gottfried.
Finding Maximum
#include<stdio.h> max=a[0];
int main() for(i=0;i<=9;i++)
{ {
int a[10],i,max; if(a[i]>max)
printf("Enter 10 integers: \n"); {max=a[i];}
for(i=0;i<=9;i++) }
{ printf(“Maximum value=%d", max);
scanf("%d", &a[i]); }
}
Sorting
int a[5] = {99, 15, 76, 55, 34};

99 15 76 55 34
a[0] a[1] a[2] a[3] a[4]

Sorted array 15 34 55 76 99
a[0] a[1] a[2] a[3] a[4]
Bubble sort 99 > 55
Pass-1

99 > 15
15 76 99 55 34
a[0] a[1] a[2] a[3] a[4]
99 15 76 55 34
99 > 34
a[0] a[1] a[2] a[3] a[4]
99 > 76
15 76 55 99 34
a[0] a[1] a[2] a[3] a[4]
15 99 76 55 34
a[0] a[1] a[2] a[3] a[4]
15 76 55 34 99
a[0] a[1] a[2] a[3] a[4]
Bubble sort
Pass-2 76 > 34

15 < 76 15 55 76 34 99
a[0] a[1] a[2]
15 76 55 34 99 a[3] a[4]

a[0] a[1] a[2] a[3] a[4] 76 < 99

76 > 55 15 55 34 76 99
a[0] a[1] a[2] a[3] a[4]
15 76 55 34 99
a[0] a[1] a[2] a[3] a[4]
15 55 34 76 99
a[0] a[1] a[2] a[3] a[4]
Bubble sort
Pass-3
15 < 55

15 55 34 76 99
a[0] a[1] a[2] a[3] a[4]

55 > 34

15 55 34 76 99
a[0] a[1] a[2] a[3] a[4]

15 34 55 76 99
a[0] a[1] a[2] a[3] a[4]
Bubble sort
Pass-3
i – variable for subscript
15 < 55

15 55 34 76 99 a[0] < a[1]


No swapping
a[j] < a[j+1]
a[0] a[1] a[2] a[3] a[4]
55 > 34

15 55 34 76 99 a[j] > a[j+1]

a[0] a[1] a[2] a[3] a[4] Swap

15 34 55 76 99
a[0] a[1] a[2] a[3] a[4]
Bubble sort program
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
#include<stdio.h> {
main() if(a[j]>a[j+1])
{ {
int a[]={99,15,76,55,34},i,j,temp; temp=a[j];
int n=5; a[j]=a[j+1];
printf("\n unsorted array \t"); a[j+1]=temp;
for(i=0;i<n;i++) }
}
{
}
printf("%d \t",a[i]); printf("\n Sorted array\t\t");
} for(i=0;i<n;i++)
{
printf("%d \t",a[i]);
}
}
Selection Sort
int a[5] = {99, 15, 76, 55, 34};

99 15 76 55 34
a[0] a[1] a[2] a[3] a[4]

Sorted array 15 34 55 76 99
a[0] a[1] a[2] a[3] a[4]
Removal of duplicates from an ordered
array
#include<stdio.h>
int main()
{
int a[20], i, j, k, n;
printf("Enter n value:");
scanf("%d", &n);
printf("Enter the elements:");
for (i=0; i<n;i++)
{
scanf("%d", &a[i]);
}
for (i=0; i<n;i++)
{
for (j=i+1; j<n;j++)
{
if(a[i]==a[j]) //check for duplicates
{
for (k=j; k<n-1;k++) //delete current position
{
a[k]=a[k+1];
}
n--; //decrease size of an array after removing duplicate element
j--;// if the position of element changes, don’t increase the element j
}
}
}
printf("After removal of duplicates:");
for (i=0; i<n;i++)
{
prinf("%d", a[i]);
}
return 0;
}
Partition an array
#include<stdio.h>
main()
{
int a[10], a1[10], a2[10], i, pos, k1=0, k2=0, n;
printf("Enter n value:");
scanf("%d", &n);
printf("Enter the elements:");
for (i=0; i<n;i++)
{
scanf("%d", &a[i]);
}
printf(“enter the position to split the array into two:”);
scanf(“%d,”&pos);
for (i=0; i<n;i++)
{
if(i<pos)
a1[k1++]=a[i];
else
a2[k2++]=a[i];
}
printf(“Elements of first array:”);
for (i=0; i<k1;i++)
printf(“%d/n, a1[i]”);
printf(“Elements of second array:”);
for (i=0; i<k2;i++)
printf(“%d/n, a2[i]”);
To insert an element into an array
#include<stdio.h>
main()
{
int a[20], i, pos, n, value;
printf("Enter number of elements:");
scanf("%d", &n);
for (i=0; i<n;i++)
{
scanf("%d", &a[i]);
}
printf(“enter the position of a new element:”);
scanf(“%d,”&pos);
printf(“enter the value to insert:”);
scanf(“%d,”&value);
for (i=n-1; i>=pos-1;i--) //shift elements forward
{
a[i+1]=a[i];
}
a[pos-1]=value; //insert value at position
printf(“Resultant array is:”);
for (i=0; i<=n;i++)
printf(“%d/n, a[i]”);
}
To delete an element into an array
#include<stdio.h>
main()
{
int a[20], i, pos, n;
printf("Enter number of elements:");
scanf("%d", &n);
for (i=0; i<n;i++)
{
scanf("%d", &a[i]);
}
printf(“enter the location of an element to be deleted:”);
scanf(“%d,”&pos);
if(pos>=n+1) //check whether the deletion is possible or not
printf(“Deletion is not possible”);
else
{
for (i=pos-1; i>=n-1;i++)
// use for loop to delete the element and update the index
{
a[i]=a[i+1];
}
printf(“Resultant array is:”);
for (i=0; i<n-1;i++)
printf(“%d/n, a[i]”);
}
Multi-Dimensional Arrays
Multi-dimensional array
• Requires a pair of square bracket for each subscript
• 2-d array requires two pairs of square brackets
• 3-d array requires three pairs of square brackets
• General syntax
data type arrayname[integer constant1]……………[integer constantn]
• An m x n, 2-d array can be thought of as a table of values having m
rows and n columns
• A 3-d array is a set of tables
2-d array
2-d array example
int values[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
• values[0][0] = 1 • values[1][0] = 5 • values[2][0] = 9
• values[0][1] = 2 • values[1][1] = 6 • values[2][1] = 10
• values[0][2] = 3 • values[1][2] = 7 • values[2][2] = 11
• values[0][3] = 4 • values[1][3] = 8 • values[2][3] = 12
• int values[3][4] = {
• Points to remember {1,2,3,4},
• Row index starts with 0 { 5,6,7,8} ,
• Column index starts with 0 {9,10,11,12}
};
2-d array example
• values[1][0] = 4
int values[3][4] = {
• values[1][1] = 5
{1,2,3},
• values[1][2] = 6
{4,5,6},
• values[1][3] = 0
{7,8,9}
}; • values[2][0] = 7
• values[0][0] = 1
• values[2][1] = 8
• values[0][1] = 2
• values[2][2] = 9
• values[0][2] = 3
• values[2][3] = 0
• values[0][3] = 0
2-d array example
int values[3][4] = {1,2,3,4,5,6, 7,8,9};

• values[0][0] = 1 • values[1][0] = 5 • values[2][0] = 9


• values[0][1] = 2 • values[1][1] = 6 • values[2][1] = 0
• values[0][2] = 3 • values[1][2] = 7 • values[2][2] = 0
• values[0][3] = 4 • values[1][3] = 8 • values[2][3] = 0
2-d array example
int values[3][4] = { • Compilation error
{1,2,3,4,5}, • Number of values in inner pair of
braces exceeds the defined array
{6,7,8,9,10}, size
{11,12,13,14,15}
};
Reading the elements of 2-d array
• Need a for loop to control the rows
• Need another loop to control the columns
• Since every row (one row) has n columns, Loops are nested
• Outer loop controls the row
• Inner loop controls the column
Sample program to Handle 2-d array
int main() for(i=0;i<3;i++) • Input
{ { • 123456789
int a[3][3],i,j; printf(“\n”);
• Displayed output
for(i=0;i<3;i++) for(j=0;j<3;j++)
• 123456789
{ {
for(j=0;j<3;j++) printf("%d\t",a[i][j]);
{ } • Input
scanf("%d",&a[i][j]); } • 123456789
} return 0; • Displayed output
} } 1 2 3
4 5 6
7 8 9
Addition of two matrices
1 2 3 9 8 7 • To add two matrices a,b
a= 4 5 6 b= 6 5 4 • Statement like c=a+b is not logically
7 8 9 3 2 1 correct
• Expected result will not be obtained
c= a + b • Need to add individual elements of
the matrices to get the result
• c[0][0]=a[0][0]+b[0][0]
10 10 10 • c[0][1]=a[0][1]+b[0][1]
c = 10 10 10 • In general, c[i][j]=a[i][j] + b[i][j]
10 10 10 Where 0<i<3 (three rows)
0<j<3 (three columns)
Program for adding two matrices
int main()
{ for(i=0;i<3;i++){
int a[3][3]={1,2,3,4,5,6,7,8,9}; printf("\n");
int b[3][3]={9,8,7,6,5,4,3,2,1}; for(j=0;j<3;j++){
int c[3][3],i,j; printf("%d\t",c[i][j]);
for(i=0;i<3;i++){ }
for(j=0;j<3;j++){ }
c[i][j]=a[i][j]+b[i][j]; return 0;
} }
}
Matrix Multiplication
𝑎11 𝑎12 𝑎13 𝑏11 𝑏12 𝑏13
𝑎11 𝑎12 𝑏11 𝑏12 a= 𝑎21 𝑎22 𝑎23 b= 𝑏21 𝑏22 𝑏23
a= 𝑎 𝑎22 b= 𝑏 𝑎31 𝑎32 𝑎33 𝑏31 𝑏32 𝑏33
21 21 𝑏22
c= a x b
c= a x b 𝑐11 = 𝑎11 ∗ 𝑏11 + 𝑎12∗ 𝑏21 + 𝑎13 ∗ 𝑏31
𝑐12 = 𝑎11 ∗ 𝑏12 + 𝑎12∗ 𝑏22 + 𝑎13 ∗ 𝑏32
𝑐11 = 𝑎11 ∗ 𝑏11 + 𝑎12∗ 𝑏21 𝑐13 = 𝑎11 ∗ 𝑏13 + 𝑎12∗ 𝑏23 + 𝑎13 ∗ 𝑏33
𝑐12 = 𝑎11 ∗ 𝑏12 + 𝑎12∗ 𝑏22 𝑐21 = 𝑎21 ∗ 𝑏11 + 𝑎22∗ 𝑏21 + 𝑎23 ∗ 𝑏31
𝑐21 = 𝑎21 ∗ 𝑏11 + 𝑎22∗ 𝑏21 𝑐22 = 𝑎21 ∗ 𝑏12 + 𝑎22∗ 𝑏22 + 𝑎23 ∗ 𝑏32
𝑐22 = 𝑎21 ∗ 𝑏12 + 𝑎22∗ 𝑏22 𝑐23 = 𝑎21 ∗ 𝑏13 + 𝑎22∗ 𝑏23 + 𝑎23 ∗ 𝑏33
Matrix Multiplication
• For a 2x2 matrix
• 𝑐𝑖 𝑗 = 𝑎𝑖 𝑘 ∗ 𝑏𝑘𝑗 + 𝑎𝑖 𝑘 ∗ 𝑏𝑘𝑗 𝑤ℎ𝑒𝑟𝑒 1 ≤ 𝑖 ≤ 2, 1 ≤ 𝑗 ≤ 2, 1 ≤ 𝑘
≤2
• For 3x3 matrix multiplication
• 𝑐𝑖𝑗 = 𝑎𝑖𝑘 ∗ 𝑏 𝑘𝑗 + 𝑎𝑖𝑘 ∗ 𝑏 𝑘𝑗 + 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗 𝑤ℎ𝑒𝑟𝑒 1 ≤ 𝑖 ≤ 3, 1 ≤ 𝑗 ≤ 3, 1 ≤ 𝑘 ≤ 3
• For nxn matrix multiplication
• 𝑐𝑖𝑗 = 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗 + 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗 + 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗 + ⋯ 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗
𝑤ℎ𝑒𝑟𝑒 1 ≤ 𝑖 ≤ 𝑛, 1 ≤ 𝑗 ≤ 𝑛, 1 ≤ 𝑘 ≤n
• In general, 𝑐𝑖𝑗 can be written as
• 𝑐𝑖 𝑗 = 𝑐𝑖 𝑗 + 𝑎𝑖 𝑘 ∗ 𝑏𝑘𝑗 𝑤ℎ𝑒𝑟𝑒 1 ≤ 𝑖 ≤ 𝑛, 1 ≤ 𝑗 ≤ 𝑛, 1 ≤ 𝑘
≤n
Matrix Multiplication
• It can be inferred that ,Three loops are required
• One to control i
• One to control j
• One to control k
• It can be inferred that, each change in i, there is change in j for n
times
• Similarly for every change in j, there is change in k for n times
• Finally it can be concluded that all three loops are nested with i as
outer most loop, k forming the inner most loop, and j loop is between
i and k loop.
Logic for multiplying two matrices
for (i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
Program for matrix multiplication
#include<stdio.h>
int main()
{
int a[10][10],b[10][10];
mul[10][10],r, c, i, j, k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the firstmatrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Additional Programs
• Write a program to multiply matrices of different sizes
• Write a program to check whether the given matrix is a
• Symmetric matrix
• Skew symmetric matrix
• Identity matrix
• Diagonal matrix
• Write a program to find the trace of given matrix.

You might also like