SlideShare a Scribd company logo
1 of 45
Download to read offline
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
1
Unit-4
Arrays
Why we have to go for arrays:
To store 100 integer items, we want 100 integer variables to store, i.e int a1, a2, a3,
…………….., a100;
To store 1000 integer items, we want 1000 integer variables to store, i.e int a1, a2,
a3,…………………, a1000;
Using arrays int a[10]; //10 integer items
int a[1000]; // 1000 integer items
int a[100000]; // 100000 integer items
What is Array?
Array is a collection of similar kind of data items.
i.e int a[10]; // a is an array, we can store 10 integer items
i.e float b[10]; // b is an array, we can store 10 floating items
Note: In array we can store only similar kind of data items, but we can’t store dissimilar kind
of data items.
Arrays has been classified into two types, they are
1) One dimensional array(1D)
2) Two dimensional array(2D)
One dimensional array(1D) – one dimensional array(1D) is a linear list which stores array
elements continuous one after the other in the memory.
How to declare 1D array
datatype array_name[size];
int a[10]; // a is an array which stores 10 integer data items
float b[100]; //b is an array which stores 100 floating data items
long int c[10]; // c is an array which stores 10 long integer items
How to find the size of an array
size of an array is calculated as = size * type of data storing in array
int a[4];
size of this array is = 4*2 i.e size of an array is 4 and type of data storing in array is int
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
2
int a[4];
2 bytes 2bytes 2 bytes 2 bytes
a[0] a[1] a[2] a[3]
this array size is 4*2=8
float b[5];
4 bytes 4 bytes 4 bytes 4bytes 4 bytes
b[0] b[1] b[2] b[3] b[4]
so, this array size is = 5*4= 20 bytes
char d[6]={‘a’,’b’,’c’,’d’,’e’};
1 byte 1 byte 1 byte 1byte 1byte 1byte
b[0] b[1] b[2] b[3] b[4] b[5]
so, this array size is = 6*1= 6 bytes
How to initialize 1D array
datatype array_name[size] = {v1,v2,v3,………………….,vn};
i.e v1,v2,v3,…………..,vn are the values stored in the array
Example:
int a[5]={10,20,30,40,50};
10 20 30 40 50
a[0] a[1] a[2] a[3] a[4]
float b[4]={2.0, 3.4, 1.6, 7.8};
2.0 3.4 1.6 7.8
a[0] a[1] a[2] a[3]
char d[5]={‘B’,’E’,’F’,’G’,’H’};
B E F G H
a[0] a[1] a[2] a[3] a[4]
Different ways of initializing 1D array
1) Initializing all specified memory locations
2) Partial array initialization
3) Array initialization without size
4) String initialization
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
3
Initialization all specified memory locations
In this array, all the memory locations are filled with values.
int a[5]={10,20,30,40,50}; //array a with five elements and the elements are 10,20,30,40,50
are stored in all the five memory locations
10 20 30 40 50
a[0] a[1] a[2] a[3] a[4]
float b[4]={3.1, 2.6, 7.9, 4.5}; //array a with 4 elements and the elements are 3.1, 2.6, 7.9,
4.5 are stored in all the four memory locations
Partial array initialization
In this array, partial filling of elements in an array.
int a[5]={10,20}; //in this array a, we can store five
elements but here we are storing two elements and the remaining spaces are filled with
zero’s. In this array we can store maximum five elements.
10 20 0 0 0
a[0] a[1] a[2] a[3] a[4]
Array initialization without size
We can declare an array without size
Example1 :
int a[ ]={10,20,30}; //in this array a, size is not specified that time it will see the number of
elements(i.e number of elements are 3 and data to be stored in an array will be int
so, size will be 3*2=6 bytes. So the data stores in the memory as shown below
10 20 30
a[0] a[1] a[2]
Example2:
float b[ ]={3.1, 2.6, 7.9, 4.5}; // in this array b, size is not specified that time it will see the
number of elements(i.e number of elements are 4 and data to be stored in an array will be of
float.
so, size will be 4*4=6 bytes. So the data stores in the memory as shown below
3.1 2.6 7.9 4.5
b[0] b[1] b[2] b[3]
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
4
so, array b with 4 elements and the elements are 3.1, 2.6, 7.9, 4.5 are stored in all the four
memory locations
String initialization
Initialize the array with a string(i.e array of characters is called string)
char e[10]=”computer”; //here string computer stores in the string variable e
C o M P u t E r 0 0
e[0] e[1] e[2] e[3] e[4] e[5] e[6] e[7] e[8] e[9]
char d[10]=”abc def”;
d[0] d[1] d[2] d[3] d[4] d[5] d[6] d[7] d[8] d[9]
How To Read An Array Elements
int a[5]; //declare an array a with five elements of type ineteger….but how to read an array
elements from the user or keyboard
int a[5];
a[0] a[1] a[2] a[3] a[4]
note: once we declare an array, memory will be allocated how to read array elements
int a[5];
int i;
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
Note: array initialization starts from zero position in the memory.
How to display array elements
int a[5]={10,20,30,40,50};
int i;
for(i=0;i<5;i++)
{
A b C d e F 0 0 0
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
5
printf(“%d “,a[i]);
}
Output:
10 20 30 40 50
How to read and display array of five integer elements
#include<stdio.h>
void main()
{
int a[5];
int i;
printf(“read array of five integer numbers”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
printf(“display array elements”);
for(i=0;i<5;i++)
{
printf(“%d “,a[i]);
}
1) Write a program to read and display array of 10 floating point numbers
#include<stdio.h>
void main( )
{
float a[10];
int i;
printf(“read array of 10 floating point numbers”);
for(i=0;i<10;i++)
Input:
Read array of five integer numbers
10 20 30 40 50
Output:
Display array elements
10 20 30 40 50
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
6
{
scanf(“%f”,&a[i]);
}
printf(“display array elements”);
for(i=0;i<10;i++)
{
printf(“%f “,a[i]);
}
}
Input :
read array of 10 floating point numbers
2.3 4.5 6.7 3.4 2.8 4.1 5.56 4.89 5.66 3.34
Output:
display array elements are
2.3 4.5 6.7 3.4 2.8 4.1 5.56 4.89 5.66 3.34
2) Write a program to read and display array of 8 very long integer numbers.
#include<stdio.h>
void main( )
{
long int a[8];
int i;
printf(“read array of 8 very long integer numbers”);
for(i=0;i<8;i++)
{
scanf(“%ld”,&a[i]);
}
printf(“display array elements”);
for(i=0;i<8;i++)
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
7
{
Printf(“%ld”,a[i]);
}
Output:
read array of 8 very long integer numbers
222 45345 56756 666678 789999 34567834 555674567 786786567
display array elements
222 45345 56756 666678 789999 34567834 555674567 786786567
3) Write a program to read and display an array of ‘N’ integer numbers
#include<stdio.h>
void main( )
{
int a[100];
int i,n;
printf(“enter size of an array”);
scanf(“%d”,&n);
printf(“enter array elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“display array elements”);
for(i=0;i<n;i++)
{
printf(“%d”,a[i]);
}
}
Output:
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
8
enter size of array
4
Enter array elements
10 20 30 40
display array elements
10 20 30 40
4) Write a C program to find the sum of an array of 5 integer numbers.
#include<stdio.h>
void main( )
{
int a[5];
int i,sum;
printf(“enter all five integer elements”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
sum=0;
for(i=0;i<5;i++)
{
sum=sum+i;
}
printf(“sum=%d”,sum);
}
Output:
Enter all five integer elements
1 2 3 4 5
Sum=15
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
9
5) Write a C program to find the sum of an array of N integer numbers.
#include<stdio.h>
void main( )
{
int a[100];
int i,n, sum;
printf(“enter the size of an array”);
scanf(“%d”,&n);
printf(“enter all integer elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
sum=0;
for(i=0;i<n;i++)
{
sum=sum+i;
}
printf(“sum=%d”,sum);
}
Input:
enter the size of an array
4
enter all integer elements
1 2 3 4
Sum=10
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
10
6) Write a C program to find the first largest element in array of N integer numbers.
#include<stdio.h>
Void main()
{
inta[100],big;
int i,n;
printf(“enter the size of an array”);
scanf(“%d”,&n);
printf(“read array elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
big=a[0];
for(i=0;i<n;i++)
{
If(a[i]>big)
{
big=a[i];
}
}
printf(“biggest element is=%d”,big);
}
Input:
enter the size of an array
4
read array elements
1 2 5 3
Output:
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
11
biggest element is = 5
7) Write a C program to find the sum of even numbers and the sum of odd numbers
from an array of N integer numbers.
#include<stdio.h>
void main( )
{
int a[100];
int i,n, esum,osum;
printf(“enter the size of an array”);
scanf(“%d”,&n);
printf(“enter all integer elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
esum=0;
osum=0;
for(i=0;i<n;i++)
{
if(a[i]%2==0)
esum=esum+a[i];
else
osum=osum+a[i];
}
printf(“esum=%d”,esum);
printf(“osum=%d”,osum);
}
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
12
8) Write a C program to read array A with 10 elements and array B with 10 elements
and find C array, such that C will be given below,
C[0]=A[0]+B[9];
C[1]=A[1]+B[8];
---------------------
---------------------
C[9]=A[9]+B[0];
#include<stdio.h>
void main( )
{
int A[10],B[10],C[10];
int i;
printf(“enter array A elements”);
for(i=0;i<10;i++)
{
scanf(“%d”,&A[i]);
}
printf(“enter array B elements”);
for(i=0;i<10;i++)
{
scanf(“%d”,&B[i]);
}
for(i=0;i<10;i++)
{
C[i]=C[i-1]+C[i-2];
}
printf(“C array will be ………………..”);
for(i=0;i<10;i++)
{
printf(“%d “,C[i]);
}
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
13
}
9) Write a C program to print ‘N’ Fibonacci numbers using arrays.
Note : Fibonacci numbers are 0,1,1,2,3,5,8,13,………………….,N
#include<stdio.h>
Void main()
{
int a[100];
int i,n;
printf(“enter the n value”);
scanf(“%d”,&n);
a[0]=0; //first Fibonacci numbers
a[1]=1; // second Fibonacci numbers
for(i=2;i<n;i++)
{
a[i]= a[i-1]+a[i-2];
}
printf(“Fibonacci numbers are………..”);
for(i=0;i<n;i++)
{
printf(“%d “,a[i]);
}
}
10. Write a C program to find the mean, varience and deviation for an array of N integer data.
Mean is the sum of all the elements by number of elements i.e mean= ∑𝑖=0
𝑛−1
Xi/ n
Varience= ∑𝑖=0
𝑛−1
(𝑋𝑖 − 𝑚𝑒𝑎𝑛)2
n
Deviation=√𝑣𝑎𝑟𝑖𝑒𝑛𝑐𝑒
#include<stdio.h>
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
14
Void main( )
{
int x[100],n,i;
float mean, varience, deviation,sum;
printf(“enter the size of an array”);
scanf(“%d”,&n);
printf(“enter array elements”);
for(i=0;i<n;i++)
{
scanf(“%d”, &x[i]);
}
Sum=0;
for(i=0;i<n;i++)
{
sum=sum + x[i];
}
mean=sum/n;
printf(“mean=%f”,mean);
sum=0;
for(i=0;i<n;i++)
{
sum=sum+(𝑋𝑖 − 𝑚𝑒𝑎𝑛) * (𝑋𝑖 − 𝑚𝑒𝑎𝑛) ;
}
varience=sum/n;
deviation=sqrt(varience);
printf(“varience=%f”,varience);
printf(“deviation=%f”,deviation);
}
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
15
Searching techniques in C language
How to search an element in an array, whether an element is found in an array or not using
following techniques
• Linear Search
• Binary Search
Sorting Technique in C language
How to sort array elements in ascending order or descender order using the following
techniques
• bubble sort
• selection sort
Searching technique – Linear search
In linear search, searching an key element from the first position element, compare the first
position element with key element, if the first position element is matches with key element
then we can display search is successful and stop searching.
If the first position element is not matching with the key element, then compare with the
second position element with key element, if it is matches then display search is successful
and stop the searching. Once again second position element is not matching with key element
then comparison start with third element, this process will repeat until the end of the element
in an array.
#include<stdio.h>
Void main()
{
int a[100],n,key,I;
printf(“enter the size of the array”);
scanf(“%d”,&n;
printf(“enter array elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
16
printf(“enter the key element to be search”);
scanf(“%d”,&key);
//comparison from first element with key element
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf(“item is found”);
exit(0);
}
}
printf(“item is not found”);
}
Input1:
Enter the size of an array
4
Enter array elements
10 20 40 30
Enter key element
20
Item is found
Input2:
Enter the size of an array
4
Enter array elements
10 20 40 30
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
17
Enter key element
70
Item is not found
Important Note: In the linear searching, input array elements to an array either in sorted
order or un-sorted order
Advantages in linear search
1. In linear search, read array elements in sorted order or un-sorted order.
Disadvantage in linear search
1. In linear search, number of comparison is more.
Binary Search
how the searching will be done in binary search????
In binary search, first we have to read array elements(i.e always in sorted order(i.e in
ascending or descending order)). Once we read array elements, first we have to find the mid
position element, then compare mid position element with key element, if mid position
element is matches with key element then we display item is found and stop searching the
element in an array. But if the mid position element is not matches with key element then we
verify whether key element is greater than the mid or the less than the mid.
If the key element is less the mid element, searching will be done in the left side otherwise
searching will be done in the right part.
So, In binary search, number of comparison is less and is one of the most efficient algorithm
#include<stdio.h>
Void main()
{
int a[100],n ,mid ,key, low, high, i;
printf(“enter the size of an array”);
scanf(“%d”,&n);
printf(“enter array elements”);
for(i=0;i<n;i++)
{
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
18
scanf(“%d”,&a[i]);
}
printf(“enter key to be search”);
scanf(“%d”,&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
{
printf(“item is found “);
exit(0);
}
If(key<a[mid])
high=mid-1;
else
low=mid+1;
}
printf(“item is not found”);
}
Input1:
Enter the size of an array
4
Enter array elements
10 20 40 30
Enter key element
20
Item is found
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
19
Input2:
Enter the size of an array
4
Enter array elements
10 20 40 30
Enter key element
60
Item is not found
Sorting techniques
Bubble sort
Selection sort
Bubble sort : how to sort the array elements in ascending or descending order
How the sorting will be done like this
In the first pass we will compare first element with second element, if the first position
element in the array is greater than second position element in the array, swap these two
elements in the array otherwise don’t swap, next compare second position elements with third
position element, if the second position element is greater than third position element, swap
those two elements otherwise don’t swap. This will repeat until the first ascending order
element is found . In the second pass, start compare the first position element with the second
position element, if the first position element is greater than second position element , swap
those two numbers otherwise don’t swap, this will repeat. Some sorting will take two passes
or some sorting will take three passes, number of passes will be depend on the complexity of
the problem.
#include<stdio.h>
Void main()
{
int a[100], n, i, temp;
printf(“enter the size of the array”);
scanf(“%d”,&n);
printf(“read array elements”);
for(i=0;i<n;i++)
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
20
{
scanf(“%d”,&a[i]);
}
for(j=1; j<n ; j++) //first for loop for the number of passes
{
for(i=0; i<n-j; i++) //comparison starts from the first position element
{
if(a[i]>a[i+1]) //compare first position element with second
{ position element
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf(“sorted elements are……”);
for(i=0;i<n;i++)
{
printf(“%d “,a[i]);
}
}
Input1:
Enter the size of an array
4
Enter array elements
50 20 40 30
The sorted elements are…..
20 30 40 50
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
21
Selection Sorting Technique
In the selection sorting, how the sorting will be done ….first search the position where the
first sorted element is there in the array, once we search the first position element, we have to
placed in the first position similarly search the second position element in the array and place
the second position element in the second place, repeat the process until all the elements are
sorted in the proper order.
#include<stdio.h>
Void main( )
{
int a[100],n,i,,pos,temp;
printf(“enter the number of elements”);
scanf(“%d”,&n);
printf(“enter array elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n-1;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
If(a[j]>a[pos])
pos=j;
}
temp=a[pos];
a[pos]=a[i];
a[i]=temp;
}
printf(“sorted elements are……”);
for(i=0;i<n;i++)
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
22
{
printf(“%d “,a[i]);
}
}
Input1:
Enter the size of an array
4
Enter array elements
50 20 40 30
The sorted elements are…..
20 30 40 50
Note: first we have to know how to read N names(or strings) or array of names.
Char a[5][10];
Number of names(or strings)
Length of the names
Size of an array = 5*10*1=50
0 1 2 3 4 5 6 7 8 9
R A M A 0
N I T H I N 0
K U M A R 0
A S H A 0
K A V A N A 0
In this manner array of names will be stored in the memory
How to read array of names is as follows:
Char a[5][10];
int i;
a[0]
a[1]
a[2]
a[3]
a[4]
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
23
printf(“read array of names”);
for(i=0;i<5;i++)
{
scanf(“%s”,a[i]);
}
How to display array of names
printf(“read array of names”);
for(i=0;i<5;i++)
{
printf(“%s n”,a[i]);
}
1. Write a C program to read N names and sort the names using bubble sort technique.
#include<stdio.h>
#include<string.h>
void main( )
{
char a[10][30],temp[30];
int n,i;
printf(“enter the N names”);
scanf(“%d”,&n);
printf(“read names”);
for(i=0;i<n;i++)
{
scanf(“%s”, a[i]);
}
//sort the names using bubble sort
for(j=1;j<n;j++)
{
for(i=0;i<n-j;i++)
{
if(strcmp(a[i],a[i+1])>0)
{
strcpy(temp,a[i]);
strcpy(a[i],a[i+1]);
strcpy(a[i+1],temp);
}
}
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
24
}
printf(“sorted names are………..”);
for(i=0;i<n;i++)
{
printf(“%sn”, a[i]);
}
}
Note: strcmp() and strcpy() are the string handling functions and these two functions are
present in <string.h>
Two -Dimensional array(2D)
2D array is a array with two subscripts is called 2D array.
How to declare 2D array
datatype arr_name[exp1][exp2];
exp1->number of rows
exp2->number of columns
int a[2][2]; //construct a matrix a with 2 rows and 2 columns
int b[3][4]; //construct a matrix b with 3 rows and 4 columns
how to find the size of the 2D array
int a[2][2];
size of the array a or size of the matrix a is= rows*columns*type of data
= 2*2*2
= 8 bytes
How was structure in the memory
how to initialize the 2D array
datatype arr_name[exp1][exp2]={{a1,a2,a3,……,an},
a[0][0] a[0][1]
a[1][0] a[1][1]
Row0
Row1
0 1
2 bytes
2 bytes
2 bytes
2 bytes
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
25
{b1,b2,b3,……,bn},
{c1,c2,c3,………..,cn},
-------------------------
{z1,z2,z3,…………,zn}};
Example:
int a[2][2]={{1,2},
{3,4}};
How it will be stored in the memory as given below
0 1
1 2
3 4
int a[3][4]={{10,20,30,40},
{50,60,70,80},
{90,100,110,120}};
0 1 2 3
10 20 30 40
50 60 70 80
90 100 110 120
Different ways of initializing 2D array
1. Initializing all specified memory location
2. Partial array initialization
Initializing all specified memory locations
Example:
int a[3][4]={{5,20,3,4},
{50,60,90,80},
{1, 2, 7, 8} } ;
Row 0
Row 1
Row 0
Row 1
Row 2
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
26
0 1 2 3
5 20 3 4
50 60 90 80
1 2 7 8
Partial array initialization
Example:
int a[3][3]={{10,20},
{30},
{40,50}};
How the data will be stored in the memory as given below in the table
0 1 2
10 20 0
30 0 0
40 50 0
How to read the matrix elements
int a[2][2]; i.e 2-rows and 2-columns
size of the array a is =2*2*2=8 bytes
coll 0 coll1
for(i=0;i<2;i++) // i for rows
{
for(j=0;j<2;j++) //j for columns
{
scanf(“%d”,&a[i][j]);
Row 0
Row 1
Row 2
Row 0
Row 1
Row 2
Row 0
Row 1
2 bytes
2 bytes 2 bytes
2 bytes
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
27
}
}
How to display matrix elements
for(i=0;i<2;i++) // i for rows
{
for(j=0;j<2;j++) //j for columns
{
printf(“%d ”,a[i][j]);
}
Printf(“n”);
}
How to read and display matrix elements(i.e read 2X2 matrix)
#include<stdio.h>
Void main( )
{
Int a[2][2],i,j;
printf(“read matrix a elements”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“matrix a elements are……..”);
for(i=0;i<2;i++)
{
1 2
2 3
Read matrix a elements
matrix a elements are……
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
28
for(j=0;j<2;j++)
{
printf(“%d ”,a[i][j]);
}
printf(“n”);
}
}
How to read and display matrix whose order is 4X4
#include<stdio.h>
Void main( )
{
int a[4][4],i,j;
printf(“read matrix a elements”);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“matrix a elements are……..”);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf(“%d ”,a[i][j]);
}
printf(“n”);
1 2
2 3
1 2 3 4
5 6 7 8
9 10 11 12
Read matrix a elements
matrix a elements are……
1 2 3 4
5 6 7 8
9 10 11 12
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
29
}
}
1. Write a C program to read and display a matrix whose order is mXn
#include<stdio.h>
void main( )
{
int a[10][10], i, j,m, n;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“matrix elements are….”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d ”,a[i][j]);
}
printf(“n”)
}}
enter the order(mXn) of the matrix
2 2
Read matrix a elements
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
30
3 4
4 6
matrix elements are……
3 4
4 6
2. Write a C program by reading matrix and find the sum of all the elements in a
matrix.
#include<stdio.h>
void main( )
{
int a[10][10], m, i, j, n,sum;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum=sum+a[i][j];
}
}
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
31
printf(“sum=%d”,sum);
}
Output:
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 4
2 6
Sum=13
3. Write a C program by reading a matrix and find the first largest element in that
matrix
#include<stdio.h>
void main( )
{
int a[10][10], m, n,big,i,j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
big=a[0][0];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
32
{
If(a[i][j]>big)
{
big=a[i][j];
}
}
}
printf(“the biggest element is=%d”,big);
}
Output:
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
5 9
The biggest element is = 9
4. Write a C program by reading a matrix and find its transpose matrix
Note: Transpose matrix is interchanging of rows and columns
#include<stdio.h>
void main( )
{
int a[10][10], m, n,big,i,j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
33
{
scanf(“%d”,&a[i][j]);
}
}
printf(“ transpose matrix is……….”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d “, a[j][i]);
}
printf(“n”);
}
enter the order(mXn) of the matrix
1 2
Read matrix a elements
3 4
5 6
Transpose matrix is……..
3 5
4 6
5. Write a C program by reading a matrix and find sum of each row
#include<stdio.h>
void main( )
{
int a[10][10], m, n,sum,i,j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
34
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum=sum+a[i][j];
}
printf(“sum of %d row= %d”,i+1,sum);
}
}
Output
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
Sum of 1 row is = 3
Sum of 2 row is = 7
6. Write a C program by reading a matrix and find the sum of each column
#include<stdio.h>
void main( )
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
35
{
int a[10][10], m, n,sum,i,j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum=sum+a[j][i];
}
printf(“sum of %d row= %d”,i+1,sum);
}
}
Output
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
Sum of 1 column is = 4
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
36
Sum of 2 column is = 6
7. Write a C program by reading a matrix and find sum of each row and sum of
each column
#include<stdio.h>
void main( )
{
int a[10][10], m, n,sum,i,j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
// sum of each row
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum=sum+a[i][j];
}
printf(“sum of %d row= %d”,i+1,sum);
}
//sum of each column
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
37
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum=sum+a[j][i];
}
printf(“sum of %d column= %d”,i+1,sum);
}
}
Output
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
Sum of 1 row is = 3
Sum of 2 row is = 7
Sum of 1 column = 4
Sum of 2 column = 6
8. Write a C program by reading a matrix and find the trace of a matrix
Note:trace of a matrix is the sum of the principal diagonal elements
#include<stdio.h>
void main( )
{
int a[10][10], m, n,sum,i,j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
38
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
sum=0;
for(i=0;i<m;i++)
{
sum=sum+a[i][i];
}
printf(“trace of the matrix is=%d”,sum);
}
Output
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
trace of the matrix is = 5
9. Write a C program by reading a matrix and find the norm of the matrix
(Note: Norm of a matrix is the square root of the sum of the squares of all the elements
in a matrix)
#include<stdio.h>
void main( )
{
int a[10][10], m, n, sum,i,j;
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
39
float nsum;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum=sum+a[i][j]*a[i][j];
}
}
nsum=sqrt(sum);
printf(“nsum = %f ”, nsum);
}
Output
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
nsum = 5.47722557
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
40
10. Write a C program to perform addition of two matrix
#include<stdio.h>
void main( )
{
int a[10][10],b[10][10],c[10][10], m, n, i, j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“read matrix b elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&b[i][j]);
}
}
// sum of two matrix
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
41
}
}
printf(“C matrix is ………………..”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d ”,c[i][j]);
}
printf(“n”);
}
}
Output:
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
Read matrix b elements
1 2
3 4
C matrix is……….
2 4
6 8
11. Write a C program to perform subtraction of two matrix
#include<stdio.h>
void main( )
{
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
42
int a[10][10],b[10][10],c[10][10], m, n, i, j;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“read matrix b elements”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&b[i][j]);
}
}
// sum of two matrix
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=b[i][j]-a[i][j];
}
}
printf(“C matrix is ………………..”);
for(i=0;i<m;i++)
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
43
{
for(j=0;j<n;j++)
{
printf(“%d ”,c[i][j]);
}
printf(“n”);
}
}
Output:
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
Read matrix b elements
7 8
9 10
C matrix is……….
6 6
6 6
12. Write a C program to perform multiplication of two matrix
#include<stdio.h>
void main( )
{
int a[10][10],b[10][10],c[10][10], m, n, p, q ,i, j, k,sum;
printf(“enter the order(mXn) of the matrix”);
scanf(“%d%d”,&m,&n);
printf(“read matrix a elements”);
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
44
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“enter the order(pXq) of the matrix”);
scanf(“%d%d”,&p,&q);
printf(“read matrix b elements”);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf(“%d”,&b[i][j]);
}
}
// multiplication of two matrix
for(i=0;i<m;i++)
{
for(j=0;j<q ; j++)
{
sum=0;
for(k=0;k<n ; k++)
{
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
}
Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya
45
}
printf(“C matrix is ………………..”);
for(i=0;i<m;i++)
{
for(j=0;j<q ;j++)
{
printf(“%d ”,c[i][j]);
}
printf(“n”);
}
}
Output:
enter the order(mXn) of the matrix
2 2
Read matrix a elements
1 2
3 4
enter the order(pXq) of the matrix
2 2
Read matrix b elements
1 2
3 4
C matrix is……….
7 10
15 22

More Related Content

What's hot

Arrays in java language
Arrays in java languageArrays in java language
Arrays in java languageHareem Naz
 
Python For Data Science Cheat Sheet
Python For Data Science Cheat SheetPython For Data Science Cheat Sheet
Python For Data Science Cheat SheetKarlijn Willems
 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arraysUma mohan
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayimtiazalijoono
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : ArraysGagan Deep
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to ArraysTareq Hasan
 
Python matplotlib cheat_sheet
Python matplotlib cheat_sheetPython matplotlib cheat_sheet
Python matplotlib cheat_sheetNishant Upadhyay
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheetGil Cohen
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Kumar Boro
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Namgee Lee
 
Arrays in python
Arrays in pythonArrays in python
Arrays in pythonLifna C.S
 

What's hot (20)

Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
Python For Data Science Cheat Sheet
Python For Data Science Cheat SheetPython For Data Science Cheat Sheet
Python For Data Science Cheat Sheet
 
Array lecture
Array lectureArray lecture
Array lecture
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Python matplotlib cheat_sheet
Python matplotlib cheat_sheetPython matplotlib cheat_sheet
Python matplotlib cheat_sheet
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Array in Java
Array in JavaArray in Java
Array in Java
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Array
ArrayArray
Array
 
NumPy Refresher
NumPy RefresherNumPy Refresher
NumPy Refresher
 

Similar to Array notes (20)

Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
02 arrays
02 arrays02 arrays
02 arrays
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
C_Arrays.pptx
C_Arrays.pptxC_Arrays.pptx
C_Arrays.pptx
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
SPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in CSPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in C
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
python_lab_manual_final (1).pdf
python_lab_manual_final (1).pdfpython_lab_manual_final (1).pdf
python_lab_manual_final (1).pdf
 
ARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptxARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptx
 

More from Deepika,Assistant Professor,PES College of Engineering ,Mandya (10)

python_unit2.pdf
python_unit2.pdfpython_unit2.pdf
python_unit2.pdf
 
introduction_to_python.pdf
introduction_to_python.pdfintroduction_to_python.pdf
introduction_to_python.pdf
 
python_datastructures.pdf
python_datastructures.pdfpython_datastructures.pdf
python_datastructures.pdf
 
Databases with SQLite3.pdf
Databases with SQLite3.pdfDatabases with SQLite3.pdf
Databases with SQLite3.pdf
 
regular_exp_python.pdf
regular_exp_python.pdfregular_exp_python.pdf
regular_exp_python.pdf
 
Digital Logic Design -Unit 1
Digital Logic Design -Unit 1Digital Logic Design -Unit 1
Digital Logic Design -Unit 1
 
Medium access control unit 3-33
Medium access control  unit 3-33Medium access control  unit 3-33
Medium access control unit 3-33
 
Unit 33-routing protocols for wsn
Unit 33-routing protocols for wsnUnit 33-routing protocols for wsn
Unit 33-routing protocols for wsn
 
Unit 2-basic wireless sensor
Unit 2-basic wireless sensorUnit 2-basic wireless sensor
Unit 2-basic wireless sensor
 
Unit1
Unit1Unit1
Unit1
 

Recently uploaded

Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 

Recently uploaded (20)

Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 

Array notes

  • 1. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 1 Unit-4 Arrays Why we have to go for arrays: To store 100 integer items, we want 100 integer variables to store, i.e int a1, a2, a3, …………….., a100; To store 1000 integer items, we want 1000 integer variables to store, i.e int a1, a2, a3,…………………, a1000; Using arrays int a[10]; //10 integer items int a[1000]; // 1000 integer items int a[100000]; // 100000 integer items What is Array? Array is a collection of similar kind of data items. i.e int a[10]; // a is an array, we can store 10 integer items i.e float b[10]; // b is an array, we can store 10 floating items Note: In array we can store only similar kind of data items, but we can’t store dissimilar kind of data items. Arrays has been classified into two types, they are 1) One dimensional array(1D) 2) Two dimensional array(2D) One dimensional array(1D) – one dimensional array(1D) is a linear list which stores array elements continuous one after the other in the memory. How to declare 1D array datatype array_name[size]; int a[10]; // a is an array which stores 10 integer data items float b[100]; //b is an array which stores 100 floating data items long int c[10]; // c is an array which stores 10 long integer items How to find the size of an array size of an array is calculated as = size * type of data storing in array int a[4]; size of this array is = 4*2 i.e size of an array is 4 and type of data storing in array is int
  • 2. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 2 int a[4]; 2 bytes 2bytes 2 bytes 2 bytes a[0] a[1] a[2] a[3] this array size is 4*2=8 float b[5]; 4 bytes 4 bytes 4 bytes 4bytes 4 bytes b[0] b[1] b[2] b[3] b[4] so, this array size is = 5*4= 20 bytes char d[6]={‘a’,’b’,’c’,’d’,’e’}; 1 byte 1 byte 1 byte 1byte 1byte 1byte b[0] b[1] b[2] b[3] b[4] b[5] so, this array size is = 6*1= 6 bytes How to initialize 1D array datatype array_name[size] = {v1,v2,v3,………………….,vn}; i.e v1,v2,v3,…………..,vn are the values stored in the array Example: int a[5]={10,20,30,40,50}; 10 20 30 40 50 a[0] a[1] a[2] a[3] a[4] float b[4]={2.0, 3.4, 1.6, 7.8}; 2.0 3.4 1.6 7.8 a[0] a[1] a[2] a[3] char d[5]={‘B’,’E’,’F’,’G’,’H’}; B E F G H a[0] a[1] a[2] a[3] a[4] Different ways of initializing 1D array 1) Initializing all specified memory locations 2) Partial array initialization 3) Array initialization without size 4) String initialization
  • 3. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 3 Initialization all specified memory locations In this array, all the memory locations are filled with values. int a[5]={10,20,30,40,50}; //array a with five elements and the elements are 10,20,30,40,50 are stored in all the five memory locations 10 20 30 40 50 a[0] a[1] a[2] a[3] a[4] float b[4]={3.1, 2.6, 7.9, 4.5}; //array a with 4 elements and the elements are 3.1, 2.6, 7.9, 4.5 are stored in all the four memory locations Partial array initialization In this array, partial filling of elements in an array. int a[5]={10,20}; //in this array a, we can store five elements but here we are storing two elements and the remaining spaces are filled with zero’s. In this array we can store maximum five elements. 10 20 0 0 0 a[0] a[1] a[2] a[3] a[4] Array initialization without size We can declare an array without size Example1 : int a[ ]={10,20,30}; //in this array a, size is not specified that time it will see the number of elements(i.e number of elements are 3 and data to be stored in an array will be int so, size will be 3*2=6 bytes. So the data stores in the memory as shown below 10 20 30 a[0] a[1] a[2] Example2: float b[ ]={3.1, 2.6, 7.9, 4.5}; // in this array b, size is not specified that time it will see the number of elements(i.e number of elements are 4 and data to be stored in an array will be of float. so, size will be 4*4=6 bytes. So the data stores in the memory as shown below 3.1 2.6 7.9 4.5 b[0] b[1] b[2] b[3]
  • 4. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 4 so, array b with 4 elements and the elements are 3.1, 2.6, 7.9, 4.5 are stored in all the four memory locations String initialization Initialize the array with a string(i.e array of characters is called string) char e[10]=”computer”; //here string computer stores in the string variable e C o M P u t E r 0 0 e[0] e[1] e[2] e[3] e[4] e[5] e[6] e[7] e[8] e[9] char d[10]=”abc def”; d[0] d[1] d[2] d[3] d[4] d[5] d[6] d[7] d[8] d[9] How To Read An Array Elements int a[5]; //declare an array a with five elements of type ineteger….but how to read an array elements from the user or keyboard int a[5]; a[0] a[1] a[2] a[3] a[4] note: once we declare an array, memory will be allocated how to read array elements int a[5]; int i; for(i=0;i<5;i++) { scanf(“%d”,&a[i]); } Note: array initialization starts from zero position in the memory. How to display array elements int a[5]={10,20,30,40,50}; int i; for(i=0;i<5;i++) { A b C d e F 0 0 0
  • 5. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 5 printf(“%d “,a[i]); } Output: 10 20 30 40 50 How to read and display array of five integer elements #include<stdio.h> void main() { int a[5]; int i; printf(“read array of five integer numbers”); for(i=0;i<5;i++) { scanf(“%d”,&a[i]); } printf(“display array elements”); for(i=0;i<5;i++) { printf(“%d “,a[i]); } 1) Write a program to read and display array of 10 floating point numbers #include<stdio.h> void main( ) { float a[10]; int i; printf(“read array of 10 floating point numbers”); for(i=0;i<10;i++) Input: Read array of five integer numbers 10 20 30 40 50 Output: Display array elements 10 20 30 40 50
  • 6. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 6 { scanf(“%f”,&a[i]); } printf(“display array elements”); for(i=0;i<10;i++) { printf(“%f “,a[i]); } } Input : read array of 10 floating point numbers 2.3 4.5 6.7 3.4 2.8 4.1 5.56 4.89 5.66 3.34 Output: display array elements are 2.3 4.5 6.7 3.4 2.8 4.1 5.56 4.89 5.66 3.34 2) Write a program to read and display array of 8 very long integer numbers. #include<stdio.h> void main( ) { long int a[8]; int i; printf(“read array of 8 very long integer numbers”); for(i=0;i<8;i++) { scanf(“%ld”,&a[i]); } printf(“display array elements”); for(i=0;i<8;i++)
  • 7. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 7 { Printf(“%ld”,a[i]); } Output: read array of 8 very long integer numbers 222 45345 56756 666678 789999 34567834 555674567 786786567 display array elements 222 45345 56756 666678 789999 34567834 555674567 786786567 3) Write a program to read and display an array of ‘N’ integer numbers #include<stdio.h> void main( ) { int a[100]; int i,n; printf(“enter size of an array”); scanf(“%d”,&n); printf(“enter array elements”); for(i=0;i<n;i++) { scanf(“%d”,&a[i]); } printf(“display array elements”); for(i=0;i<n;i++) { printf(“%d”,a[i]); } } Output:
  • 8. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 8 enter size of array 4 Enter array elements 10 20 30 40 display array elements 10 20 30 40 4) Write a C program to find the sum of an array of 5 integer numbers. #include<stdio.h> void main( ) { int a[5]; int i,sum; printf(“enter all five integer elements”); for(i=0;i<5;i++) { scanf(“%d”,&a[i]); } sum=0; for(i=0;i<5;i++) { sum=sum+i; } printf(“sum=%d”,sum); } Output: Enter all five integer elements 1 2 3 4 5 Sum=15
  • 9. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 9 5) Write a C program to find the sum of an array of N integer numbers. #include<stdio.h> void main( ) { int a[100]; int i,n, sum; printf(“enter the size of an array”); scanf(“%d”,&n); printf(“enter all integer elements”); for(i=0;i<n;i++) { scanf(“%d”,&a[i]); } sum=0; for(i=0;i<n;i++) { sum=sum+i; } printf(“sum=%d”,sum); } Input: enter the size of an array 4 enter all integer elements 1 2 3 4 Sum=10
  • 10. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 10 6) Write a C program to find the first largest element in array of N integer numbers. #include<stdio.h> Void main() { inta[100],big; int i,n; printf(“enter the size of an array”); scanf(“%d”,&n); printf(“read array elements”); for(i=0;i<n;i++) { scanf(“%d”,&a[i]); } big=a[0]; for(i=0;i<n;i++) { If(a[i]>big) { big=a[i]; } } printf(“biggest element is=%d”,big); } Input: enter the size of an array 4 read array elements 1 2 5 3 Output:
  • 11. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 11 biggest element is = 5 7) Write a C program to find the sum of even numbers and the sum of odd numbers from an array of N integer numbers. #include<stdio.h> void main( ) { int a[100]; int i,n, esum,osum; printf(“enter the size of an array”); scanf(“%d”,&n); printf(“enter all integer elements”); for(i=0;i<n;i++) { scanf(“%d”,&a[i]); } esum=0; osum=0; for(i=0;i<n;i++) { if(a[i]%2==0) esum=esum+a[i]; else osum=osum+a[i]; } printf(“esum=%d”,esum); printf(“osum=%d”,osum); }
  • 12. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 12 8) Write a C program to read array A with 10 elements and array B with 10 elements and find C array, such that C will be given below, C[0]=A[0]+B[9]; C[1]=A[1]+B[8]; --------------------- --------------------- C[9]=A[9]+B[0]; #include<stdio.h> void main( ) { int A[10],B[10],C[10]; int i; printf(“enter array A elements”); for(i=0;i<10;i++) { scanf(“%d”,&A[i]); } printf(“enter array B elements”); for(i=0;i<10;i++) { scanf(“%d”,&B[i]); } for(i=0;i<10;i++) { C[i]=C[i-1]+C[i-2]; } printf(“C array will be ………………..”); for(i=0;i<10;i++) { printf(“%d “,C[i]); }
  • 13. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 13 } 9) Write a C program to print ‘N’ Fibonacci numbers using arrays. Note : Fibonacci numbers are 0,1,1,2,3,5,8,13,………………….,N #include<stdio.h> Void main() { int a[100]; int i,n; printf(“enter the n value”); scanf(“%d”,&n); a[0]=0; //first Fibonacci numbers a[1]=1; // second Fibonacci numbers for(i=2;i<n;i++) { a[i]= a[i-1]+a[i-2]; } printf(“Fibonacci numbers are………..”); for(i=0;i<n;i++) { printf(“%d “,a[i]); } } 10. Write a C program to find the mean, varience and deviation for an array of N integer data. Mean is the sum of all the elements by number of elements i.e mean= ∑𝑖=0 𝑛−1 Xi/ n Varience= ∑𝑖=0 𝑛−1 (𝑋𝑖 − 𝑚𝑒𝑎𝑛)2 n Deviation=√𝑣𝑎𝑟𝑖𝑒𝑛𝑐𝑒 #include<stdio.h>
  • 14. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 14 Void main( ) { int x[100],n,i; float mean, varience, deviation,sum; printf(“enter the size of an array”); scanf(“%d”,&n); printf(“enter array elements”); for(i=0;i<n;i++) { scanf(“%d”, &x[i]); } Sum=0; for(i=0;i<n;i++) { sum=sum + x[i]; } mean=sum/n; printf(“mean=%f”,mean); sum=0; for(i=0;i<n;i++) { sum=sum+(𝑋𝑖 − 𝑚𝑒𝑎𝑛) * (𝑋𝑖 − 𝑚𝑒𝑎𝑛) ; } varience=sum/n; deviation=sqrt(varience); printf(“varience=%f”,varience); printf(“deviation=%f”,deviation); }
  • 15. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 15 Searching techniques in C language How to search an element in an array, whether an element is found in an array or not using following techniques • Linear Search • Binary Search Sorting Technique in C language How to sort array elements in ascending order or descender order using the following techniques • bubble sort • selection sort Searching technique – Linear search In linear search, searching an key element from the first position element, compare the first position element with key element, if the first position element is matches with key element then we can display search is successful and stop searching. If the first position element is not matching with the key element, then compare with the second position element with key element, if it is matches then display search is successful and stop the searching. Once again second position element is not matching with key element then comparison start with third element, this process will repeat until the end of the element in an array. #include<stdio.h> Void main() { int a[100],n,key,I; printf(“enter the size of the array”); scanf(“%d”,&n; printf(“enter array elements”); for(i=0;i<n;i++) { scanf(“%d”,&a[i]); }
  • 16. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 16 printf(“enter the key element to be search”); scanf(“%d”,&key); //comparison from first element with key element for(i=0;i<n;i++) { if(a[i]==key) { printf(“item is found”); exit(0); } } printf(“item is not found”); } Input1: Enter the size of an array 4 Enter array elements 10 20 40 30 Enter key element 20 Item is found Input2: Enter the size of an array 4 Enter array elements 10 20 40 30
  • 17. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 17 Enter key element 70 Item is not found Important Note: In the linear searching, input array elements to an array either in sorted order or un-sorted order Advantages in linear search 1. In linear search, read array elements in sorted order or un-sorted order. Disadvantage in linear search 1. In linear search, number of comparison is more. Binary Search how the searching will be done in binary search???? In binary search, first we have to read array elements(i.e always in sorted order(i.e in ascending or descending order)). Once we read array elements, first we have to find the mid position element, then compare mid position element with key element, if mid position element is matches with key element then we display item is found and stop searching the element in an array. But if the mid position element is not matches with key element then we verify whether key element is greater than the mid or the less than the mid. If the key element is less the mid element, searching will be done in the left side otherwise searching will be done in the right part. So, In binary search, number of comparison is less and is one of the most efficient algorithm #include<stdio.h> Void main() { int a[100],n ,mid ,key, low, high, i; printf(“enter the size of an array”); scanf(“%d”,&n); printf(“enter array elements”); for(i=0;i<n;i++) {
  • 18. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 18 scanf(“%d”,&a[i]); } printf(“enter key to be search”); scanf(“%d”,&key); low=0; high=n-1; while(low<=high) { mid=(low+high)/2; if(a[mid]==key) { printf(“item is found “); exit(0); } If(key<a[mid]) high=mid-1; else low=mid+1; } printf(“item is not found”); } Input1: Enter the size of an array 4 Enter array elements 10 20 40 30 Enter key element 20 Item is found
  • 19. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 19 Input2: Enter the size of an array 4 Enter array elements 10 20 40 30 Enter key element 60 Item is not found Sorting techniques Bubble sort Selection sort Bubble sort : how to sort the array elements in ascending or descending order How the sorting will be done like this In the first pass we will compare first element with second element, if the first position element in the array is greater than second position element in the array, swap these two elements in the array otherwise don’t swap, next compare second position elements with third position element, if the second position element is greater than third position element, swap those two elements otherwise don’t swap. This will repeat until the first ascending order element is found . In the second pass, start compare the first position element with the second position element, if the first position element is greater than second position element , swap those two numbers otherwise don’t swap, this will repeat. Some sorting will take two passes or some sorting will take three passes, number of passes will be depend on the complexity of the problem. #include<stdio.h> Void main() { int a[100], n, i, temp; printf(“enter the size of the array”); scanf(“%d”,&n); printf(“read array elements”); for(i=0;i<n;i++)
  • 20. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 20 { scanf(“%d”,&a[i]); } for(j=1; j<n ; j++) //first for loop for the number of passes { for(i=0; i<n-j; i++) //comparison starts from the first position element { if(a[i]>a[i+1]) //compare first position element with second { position element temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } } } printf(“sorted elements are……”); for(i=0;i<n;i++) { printf(“%d “,a[i]); } } Input1: Enter the size of an array 4 Enter array elements 50 20 40 30 The sorted elements are….. 20 30 40 50
  • 21. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 21 Selection Sorting Technique In the selection sorting, how the sorting will be done ….first search the position where the first sorted element is there in the array, once we search the first position element, we have to placed in the first position similarly search the second position element in the array and place the second position element in the second place, repeat the process until all the elements are sorted in the proper order. #include<stdio.h> Void main( ) { int a[100],n,i,,pos,temp; printf(“enter the number of elements”); scanf(“%d”,&n); printf(“enter array elements”); for(i=0;i<n;i++) { scanf(“%d”,&a[i]); } for(i=0;i<n-1;i++) { pos=i; for(j=i+1;j<n;j++) { If(a[j]>a[pos]) pos=j; } temp=a[pos]; a[pos]=a[i]; a[i]=temp; } printf(“sorted elements are……”); for(i=0;i<n;i++)
  • 22. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 22 { printf(“%d “,a[i]); } } Input1: Enter the size of an array 4 Enter array elements 50 20 40 30 The sorted elements are….. 20 30 40 50 Note: first we have to know how to read N names(or strings) or array of names. Char a[5][10]; Number of names(or strings) Length of the names Size of an array = 5*10*1=50 0 1 2 3 4 5 6 7 8 9 R A M A 0 N I T H I N 0 K U M A R 0 A S H A 0 K A V A N A 0 In this manner array of names will be stored in the memory How to read array of names is as follows: Char a[5][10]; int i; a[0] a[1] a[2] a[3] a[4]
  • 23. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 23 printf(“read array of names”); for(i=0;i<5;i++) { scanf(“%s”,a[i]); } How to display array of names printf(“read array of names”); for(i=0;i<5;i++) { printf(“%s n”,a[i]); } 1. Write a C program to read N names and sort the names using bubble sort technique. #include<stdio.h> #include<string.h> void main( ) { char a[10][30],temp[30]; int n,i; printf(“enter the N names”); scanf(“%d”,&n); printf(“read names”); for(i=0;i<n;i++) { scanf(“%s”, a[i]); } //sort the names using bubble sort for(j=1;j<n;j++) { for(i=0;i<n-j;i++) { if(strcmp(a[i],a[i+1])>0) { strcpy(temp,a[i]); strcpy(a[i],a[i+1]); strcpy(a[i+1],temp); } }
  • 24. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 24 } printf(“sorted names are………..”); for(i=0;i<n;i++) { printf(“%sn”, a[i]); } } Note: strcmp() and strcpy() are the string handling functions and these two functions are present in <string.h> Two -Dimensional array(2D) 2D array is a array with two subscripts is called 2D array. How to declare 2D array datatype arr_name[exp1][exp2]; exp1->number of rows exp2->number of columns int a[2][2]; //construct a matrix a with 2 rows and 2 columns int b[3][4]; //construct a matrix b with 3 rows and 4 columns how to find the size of the 2D array int a[2][2]; size of the array a or size of the matrix a is= rows*columns*type of data = 2*2*2 = 8 bytes How was structure in the memory how to initialize the 2D array datatype arr_name[exp1][exp2]={{a1,a2,a3,……,an}, a[0][0] a[0][1] a[1][0] a[1][1] Row0 Row1 0 1 2 bytes 2 bytes 2 bytes 2 bytes
  • 25. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 25 {b1,b2,b3,……,bn}, {c1,c2,c3,………..,cn}, ------------------------- {z1,z2,z3,…………,zn}}; Example: int a[2][2]={{1,2}, {3,4}}; How it will be stored in the memory as given below 0 1 1 2 3 4 int a[3][4]={{10,20,30,40}, {50,60,70,80}, {90,100,110,120}}; 0 1 2 3 10 20 30 40 50 60 70 80 90 100 110 120 Different ways of initializing 2D array 1. Initializing all specified memory location 2. Partial array initialization Initializing all specified memory locations Example: int a[3][4]={{5,20,3,4}, {50,60,90,80}, {1, 2, 7, 8} } ; Row 0 Row 1 Row 0 Row 1 Row 2
  • 26. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 26 0 1 2 3 5 20 3 4 50 60 90 80 1 2 7 8 Partial array initialization Example: int a[3][3]={{10,20}, {30}, {40,50}}; How the data will be stored in the memory as given below in the table 0 1 2 10 20 0 30 0 0 40 50 0 How to read the matrix elements int a[2][2]; i.e 2-rows and 2-columns size of the array a is =2*2*2=8 bytes coll 0 coll1 for(i=0;i<2;i++) // i for rows { for(j=0;j<2;j++) //j for columns { scanf(“%d”,&a[i][j]); Row 0 Row 1 Row 2 Row 0 Row 1 Row 2 Row 0 Row 1 2 bytes 2 bytes 2 bytes 2 bytes
  • 27. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 27 } } How to display matrix elements for(i=0;i<2;i++) // i for rows { for(j=0;j<2;j++) //j for columns { printf(“%d ”,a[i][j]); } Printf(“n”); } How to read and display matrix elements(i.e read 2X2 matrix) #include<stdio.h> Void main( ) { Int a[2][2],i,j; printf(“read matrix a elements”); for(i=0;i<2;i++) { for(j=0;j<2;j++) { scanf(“%d”,&a[i][j]); } } printf(“matrix a elements are……..”); for(i=0;i<2;i++) { 1 2 2 3 Read matrix a elements matrix a elements are……
  • 28. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 28 for(j=0;j<2;j++) { printf(“%d ”,a[i][j]); } printf(“n”); } } How to read and display matrix whose order is 4X4 #include<stdio.h> Void main( ) { int a[4][4],i,j; printf(“read matrix a elements”); for(i=0;i<4;i++) { for(j=0;j<4;j++) { scanf(“%d”,&a[i][j]); } } printf(“matrix a elements are……..”); for(i=0;i<4;i++) { for(j=0;j<4;j++) { printf(“%d ”,a[i][j]); } printf(“n”); 1 2 2 3 1 2 3 4 5 6 7 8 9 10 11 12 Read matrix a elements matrix a elements are…… 1 2 3 4 5 6 7 8 9 10 11 12
  • 29. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 29 } } 1. Write a C program to read and display a matrix whose order is mXn #include<stdio.h> void main( ) { int a[10][10], i, j,m, n; printf(“enter the order(mXn) of the matrix”); scanf(“%d%d”,&m,&n); printf(“read matrix a elements”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&a[i][j]); } } printf(“matrix elements are….”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(“%d ”,a[i][j]); } printf(“n”) }} enter the order(mXn) of the matrix 2 2 Read matrix a elements
  • 30. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 30 3 4 4 6 matrix elements are…… 3 4 4 6 2. Write a C program by reading matrix and find the sum of all the elements in a matrix. #include<stdio.h> void main( ) { int a[10][10], m, i, j, n,sum; printf(“enter the order(mXn) of the matrix”); scanf(“%d%d”,&m,&n); printf(“read matrix a elements”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&a[i][j]); } } sum=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { sum=sum+a[i][j]; } }
  • 31. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 31 printf(“sum=%d”,sum); } Output: enter the order(mXn) of the matrix 2 2 Read matrix a elements 1 4 2 6 Sum=13 3. Write a C program by reading a matrix and find the first largest element in that matrix #include<stdio.h> void main( ) { int a[10][10], m, n,big,i,j; printf(“enter the order(mXn) of the matrix”); scanf(“%d%d”,&m,&n); printf(“read matrix a elements”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&a[i][j]); } } big=a[0][0]; for(i=0;i<m;i++) { for(j=0;j<n;j++)
  • 32. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 32 { If(a[i][j]>big) { big=a[i][j]; } } } printf(“the biggest element is=%d”,big); } Output: enter the order(mXn) of the matrix 2 2 Read matrix a elements 1 2 5 9 The biggest element is = 9 4. Write a C program by reading a matrix and find its transpose matrix Note: Transpose matrix is interchanging of rows and columns #include<stdio.h> void main( ) { int a[10][10], m, n,big,i,j; printf(“enter the order(mXn) of the matrix”); scanf(“%d%d”,&m,&n); printf(“read matrix a elements”); for(i=0;i<m;i++) { for(j=0;j<n;j++)
  • 33. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 33 { scanf(“%d”,&a[i][j]); } } printf(“ transpose matrix is……….”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(“%d “, a[j][i]); } printf(“n”); } enter the order(mXn) of the matrix 1 2 Read matrix a elements 3 4 5 6 Transpose matrix is…….. 3 5 4 6 5. Write a C program by reading a matrix and find sum of each row #include<stdio.h> void main( ) { int a[10][10], m, n,sum,i,j; printf(“enter the order(mXn) of the matrix”); scanf(“%d%d”,&m,&n);
  • 34. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 34 printf(“read matrix a elements”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&a[i][j]); } } for(i=0;i<m;i++) { sum=0; for(j=0;j<n;j++) { sum=sum+a[i][j]; } printf(“sum of %d row= %d”,i+1,sum); } } Output enter the order(mXn) of the matrix 2 2 Read matrix a elements 1 2 3 4 Sum of 1 row is = 3 Sum of 2 row is = 7 6. Write a C program by reading a matrix and find the sum of each column #include<stdio.h> void main( )
  • 35. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 35 { int a[10][10], m, n,sum,i,j; printf(“enter the order(mXn) of the matrix”); scanf(“%d%d”,&m,&n); printf(“read matrix a elements”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&a[i][j]); } } for(i=0;i<m;i++) { sum=0; for(j=0;j<n;j++) { sum=sum+a[j][i]; } printf(“sum of %d row= %d”,i+1,sum); } } Output enter the order(mXn) of the matrix 2 2 Read matrix a elements 1 2 3 4 Sum of 1 column is = 4
  • 36. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 36 Sum of 2 column is = 6 7. Write a C program by reading a matrix and find sum of each row and sum of each column #include<stdio.h> void main( ) { int a[10][10], m, n,sum,i,j; printf(“enter the order(mXn) of the matrix”); scanf(“%d%d”,&m,&n); printf(“read matrix a elements”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&a[i][j]); } } // sum of each row for(i=0;i<m;i++) { sum=0; for(j=0;j<n;j++) { sum=sum+a[i][j]; } printf(“sum of %d row= %d”,i+1,sum); } //sum of each column
  • 37. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 37 for(i=0;i<m;i++) { sum=0; for(j=0;j<n;j++) { sum=sum+a[j][i]; } printf(“sum of %d column= %d”,i+1,sum); } } Output enter the order(mXn) of the matrix 2 2 Read matrix a elements 1 2 3 4 Sum of 1 row is = 3 Sum of 2 row is = 7 Sum of 1 column = 4 Sum of 2 column = 6 8. Write a C program by reading a matrix and find the trace of a matrix Note:trace of a matrix is the sum of the principal diagonal elements #include<stdio.h> void main( ) { int a[10][10], m, n,sum,i,j; printf(“enter the order(mXn) of the matrix”); scanf(“%d%d”,&m,&n);
  • 38. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 38 printf(“read matrix a elements”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&a[i][j]); } } sum=0; for(i=0;i<m;i++) { sum=sum+a[i][i]; } printf(“trace of the matrix is=%d”,sum); } Output enter the order(mXn) of the matrix 2 2 Read matrix a elements 1 2 3 4 trace of the matrix is = 5 9. Write a C program by reading a matrix and find the norm of the matrix (Note: Norm of a matrix is the square root of the sum of the squares of all the elements in a matrix) #include<stdio.h> void main( ) { int a[10][10], m, n, sum,i,j;
  • 39. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 39 float nsum; printf(“enter the order(mXn) of the matrix”); scanf(“%d%d”,&m,&n); printf(“read matrix a elements”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&a[i][j]); } } sum=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { sum=sum+a[i][j]*a[i][j]; } } nsum=sqrt(sum); printf(“nsum = %f ”, nsum); } Output enter the order(mXn) of the matrix 2 2 Read matrix a elements 1 2 3 4 nsum = 5.47722557
  • 40. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 40 10. Write a C program to perform addition of two matrix #include<stdio.h> void main( ) { int a[10][10],b[10][10],c[10][10], m, n, i, j; printf(“enter the order(mXn) of the matrix”); scanf(“%d%d”,&m,&n); printf(“read matrix a elements”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&a[i][j]); } } printf(“read matrix b elements”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&b[i][j]); } } // sum of two matrix for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=a[i][j]+b[i][j];
  • 41. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 41 } } printf(“C matrix is ………………..”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(“%d ”,c[i][j]); } printf(“n”); } } Output: enter the order(mXn) of the matrix 2 2 Read matrix a elements 1 2 3 4 Read matrix b elements 1 2 3 4 C matrix is………. 2 4 6 8 11. Write a C program to perform subtraction of two matrix #include<stdio.h> void main( ) {
  • 42. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 42 int a[10][10],b[10][10],c[10][10], m, n, i, j; printf(“enter the order(mXn) of the matrix”); scanf(“%d%d”,&m,&n); printf(“read matrix a elements”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&a[i][j]); } } printf(“read matrix b elements”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&b[i][j]); } } // sum of two matrix for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=b[i][j]-a[i][j]; } } printf(“C matrix is ………………..”); for(i=0;i<m;i++)
  • 43. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 43 { for(j=0;j<n;j++) { printf(“%d ”,c[i][j]); } printf(“n”); } } Output: enter the order(mXn) of the matrix 2 2 Read matrix a elements 1 2 3 4 Read matrix b elements 7 8 9 10 C matrix is………. 6 6 6 6 12. Write a C program to perform multiplication of two matrix #include<stdio.h> void main( ) { int a[10][10],b[10][10],c[10][10], m, n, p, q ,i, j, k,sum; printf(“enter the order(mXn) of the matrix”); scanf(“%d%d”,&m,&n); printf(“read matrix a elements”);
  • 44. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 44 for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&a[i][j]); } } printf(“enter the order(pXq) of the matrix”); scanf(“%d%d”,&p,&q); printf(“read matrix b elements”); for(i=0;i<p;i++) { for(j=0;j<q;j++) { scanf(“%d”,&b[i][j]); } } // multiplication of two matrix for(i=0;i<m;i++) { for(j=0;j<q ; j++) { sum=0; for(k=0;k<n ; k++) { sum=sum+a[i][k]*b[k][j]; } c[i][j]=sum; }
  • 45. Deepika, Asst. Professor, Dept. of CS and E, PESCE, Mandya 45 } printf(“C matrix is ………………..”); for(i=0;i<m;i++) { for(j=0;j<q ;j++) { printf(“%d ”,c[i][j]); } printf(“n”); } } Output: enter the order(mXn) of the matrix 2 2 Read matrix a elements 1 2 3 4 enter the order(pXq) of the matrix 2 2 Read matrix b elements 1 2 3 4 C matrix is………. 7 10 15 22