0% found this document useful (0 votes)
10 views47 pages

Module 3 Arrays

Uploaded by

Joel George
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
10 views47 pages

Module 3 Arrays

Uploaded by

Joel George
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 47

MODULE 3

ARRAYS
ARRAYS

• An array is a fixed size sequenced collection of elements of the same


data type.
• It is simply a grouping of elements of similar type data
• Arrays have 0 as the first index
eg- List of employees in an organization
List of marks of students in a class
List of products and their cost sold by a store
TYPES OF ARRAYS
• One- dimensional array
• Two-dimensional array
• Multi-dimensional array
One- dimensional array
A list of elements may be given one variable name, and the
individual elements may be accessed by the specification
of their relative positions with respect to the start of the list

Declaration of one dimensional array

syntax is data type variable name[size]


eg- if we want to store 5 elements say, (20, 35, 40 ,34,56)
by an array variable name number, it as follows;
int number[5]
Initialization of one dimensional array
• After an array is declared, its elements must be initialized
• An array can be initialized at either of the following stages
at compile time
at run time
Compile time initialization
• An array can be initialized at compile time ie while writing the source code
type array name[size]={list of values}
eg:- int number[5] ={20,35,40,34,56};
Memory allocated and initialized
Run time initialization
• An array can be initialized at run time ie, during the execution of
source code
• This approach is used for large size arrays
Program to take 5 values from the user and store them in an array and print them
#include <stdio.h>
int main() {
int values[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(int i = 0; i < 5; ++i)
{
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
// printing elements of an array
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
LINEAR SEARCH
Linear search is a sequential searching algorithm where we start from one end and
check every element of the list until the desired element is found. It is the simplest
searching algorithm.
Algorithm Linear search
step 1 : start
step 2: Input array size n
step 3: Input array element s array[]
step 4: input search element key
step 5: set i to 0
step 6: Repeat steps 7-8, while i<n
step 7:if array[i] = = key then
print element found at index i
step 8: set i to i+1
step 9: if i= =n then
print element not found
step 10: End
Pseudocode Linear Search

procedure LINEAR SEARCH(array, key)


FOR(i=0; i<n; i++)
IF(A[i]==key)
RETURN elements index
END IF
RETURN -1
END procedure
/*linear search*/
#include<stdio.h>
int main()
{
int array[100], i,n,key;

printf("Enter the size of array\n");


scanf("%d", &n);

printf("Enter the array elements”);

for (i = 0; i < n; i++)


scanf("%d", &array[i]);
printf("Enter a number to search\n");
scanf("%d", &key);

for (i = 0; i< n; i++)


{
if (array[i] == key) /* If required element is found */
{
printf("%d is present at index %d.\n", key, i);
break;
}
}
if(i==n)
{
printf("Element not found");
}
return 0;
Bubble sort in C
• Bubble sort is a simple sorting algorithm that works by comparing the
adjacent elements in the list and swapping them if the elements are not
in the specified order.
• Bubble sort in C to arrange numbers in ascending order
Working of Bubble Sort
Suppose we are trying to sort the elements in ascending order.
1. First Iteration (Compare and Swap)
1.Starting from the first index, compare the first and the second
elements.
2.If the first element is greater than the second element, they are
swapped.
3.Now, compare the second and the third elements. Swap them if
they are not in order.
The above process goes on until the last element.
2. Remaining Iteration
The same process goes on for the remaining iterations.
After each iteration, the largest element among the unsorted
elements is placed at the end.
The array is sorted when all the unsorted elements are placed at
their correct positions.
BUUBLE SORT ALGORITHM

•Run two loops nested in one another.

•The outer loop will run from i = 0 to i < n – 1, where n is the number of elements in
the list.

•The inner loop will run from j = 0 to j < n – i – 1. It is because, after each iteration of
the outer loop, one element at the end (or at the start if the order is decreasing order)
will be in its right place so we can leave it as it is.

•In the inner loop, we will check if the arr[ j ] > arr[ j + 1 ].
• If it’s true, then we will swap places of these elements.
• If false, we will continue to the next iteration.
Algorithm Bubble sort

Step 1: start
Step 2: input array size n
Step 3 : input array elementS A[]
Step 4: set i=0, temp=0
Step 5: Repeat steps 6-10, while i<n-1
Step 6 :set j=0
Step 7: Repeat steps 8-9 while j<n-1-i
Step 8: if A[j]>A[j+1], then
temp= A[j]
A[j]= A[j+1]
A[j+1]=temp
Step 9:set j=j+1
Step 10: set i=i+1
Step 11: print sorted array elements
Step 12:end
Pseudocode bubble sort
Pseudocode Bubble sort
READ array size n
READ array elements A[]
FOR i=o to i< n-1 do
{
FOR j=0 to j<n-1-i do
{
IF A[j]>A[j+1] then
{
temp= A[j]
A[j]= A[j+1]
A[j+1]=temp
}
}
PRINT sorted array elements
}
BUBBLE SORT PROGRAM
#include<stdio.h>
int main()
{
int array[100], n, i, j, temp=0;

printf("Enter array size\n");


scanf("%d", &n);
printf("Enter array elements\n");

for (i = 0; i < n; i++)


scanf("%d", &array[i]);

for (i = 0 ; i < n - 1; i++)


{
for (j = 0 ; j < n - i - 1; j++)
{
if (array[j] > array[j+1]) /* For decreasing order use '<' instead of '>' */
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("Sorted list in ascending order:\n");
for (i= 0; i < n; i++)
printf("%d\n", array[i]);
return 0;
}
TWO DIMENSIONAL ARRAY
• 2D array in C is an array that has exactly two dimensions. They can be
visualized in the form of rows and columns organized in a
two-dimensional plane.
• Two dimensional arrays also known as matrix

Syntax of 2D Array in C

datatype array_name[row size] [column size];


Initialization of 2D array
1.Compile time initialization

int table[2][3] ={0,0,0,1,1,1}


or
int table[2][3] ={{0,0,0},(1,1,1}}
Run time initialization
#include<stdio.h>
int main()
{
int i, j, r, c, matrix[3][4];
for (i = 0; i < 3; ++i)
{
for (j = 0; j <4 ; ++j)
{
scanf(" %d", &matrix[i][j]);
}
}
printf(“Entered 2-D array is:\n”);
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
{
printf(“%d”, arr[i][j]);
}
printf(“\n”);
}
return 0;
}
C Program to find sum of diagional elements of matrix

#include<stdio.h>
int main()
{
int i, j, r, c, sum = 0, matrix[10][10];
printf(“Enter rows and columns of matrix\n”);
scanf(“%d%d”, &r, &c)
for (i = 0; i < r; ++i)
{
for (j = 0; j <c ; ++j)
{
scanf(" %d", &matrix[i][j]);
}
}
if(r==c)
{
for (i = 0; i < r; ++i)
{
for(j=0;j<c;j++)
{
If(i==j)
{
// calculating the main diagonal sum
sum = sum + matrix[i][j];

}}}
// printing the result
printf("\nMain diagonal elements sum is = %d\n", sum);
else
// if both rows and columns are not equal then it is
// not possible to calculate the sum
printf("not a square matrix\n");
return 0;
}
C Program to check whether a matrix is identity matrix or not
#include<stdio.h>
int main()
{
int a[20][20], r,c,i, j, flag=0;
printf(“Enter the order of matrix\n”);
scanf(“%d%d”, &r, &c);
printf(“Enter the elements of matrix\n”);
for (i = 0; i < r; ++i)
{
for (j = 0; j <c ; ++j)
{
scanf(" %d", &a[i][j]);
}
}
// checking for identity matrix
for (i = 0; i < r; ++i)
{
for (j = 0; j <c ; ++j)
{
if(a[i][j]!=0 && a[i][j] !=1)
{
flag=1;
break;
}
}
if(flag==0)
printf(“identity matrix\n”);
else
printf(“Not an identity matrix”);
return 0
}
C Program to find transpose of a matrix
• The transpose of a matrix is a new matrix that is obtained by
exchanging the rows and columns.
• Transpose is obtained by changing A[i][j] to A[j][i].
#include<stdio.h>
int main()
{
int a[20][20], transpose[20][20], r,c,i,j;
printf(“Enter the order of matrix\n”);
scanf(“%d%d”, &r, &c);

//Inputting array elements


printf(“Enter the elements of matrix\n”);
for (i = 0; i < r; ++i)

for (j = 0; j <c ; ++j)

scanf(" %d", &a[i][j]);


//Printing the entered matrix
printf(“Display the entered matrix\n”);
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
printf(“%d”, a[i][j]);
If(j==c-1)
printf(“\n”);
}
//computing the transpose
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
transpose[j][i]= a[i][j];
//Printing the transpose matrix
for(i = 0; i < c; i++)
{
for(j = 0; j < r; j++)
{
printf(“%d”, transpose[i][j]
}
printf(“\n”);
}
return 0;
}
C program to find the product of two matrices

• IF MATRIX 1 is of order m*n & MATRIX 2 is of order p*q

• Then to find the product of two matrices no. of columns of MATRIX 1


should be equal to no. of rows of MATRIX 2 ie n= =p

• Resultant matrix will be of order m*p


/*Product of two matrices*/
#include<stdio.h>
int main()
{
int a[20][20], b[20[20], c[20[20];
int m, n, i, j, p, q, k;
printf(“Enter the order of matrix 1 \n”);
scanf(“%d%d”, &m, &n);

//Inputting matrix 1 elements


printf(“Enter the elements of matrix 1\n”);
for (i = 0; i < m; ++i)
{
for (j = 0; j <n ; ++j)
{
scanf(" %d", &a[i][j]);
}
}
printf(“Enter the order of matrix 2 \n”);
scanf(“%d%d”, &p, &q);

//Inputting matrix 2 elements


printf(“Enter the elements of matrix 2\n”);
for (i = 0; i < p; ++i)
{
for (j = 0; j <q ; ++j)
{
scanf(" %d", &b[i][j]);
}
}
if(n==p)
for (i = 0; i < m; ++i)
{
for (j = 0; j <q ; ++j)
{
c[i][j] = 0;
for(k=0; k<n; k++)
{
c[i][j]= c[i][j]+ a[i]k] * b[k][j];
}
}
}
//Printing the resultant matrix
for (i = 0; i < m; ++i)
{
for (j = 0; j <q ; ++j)
{
printf(“%d\t”, c[i][j]);
}
printf(“\n”);
}
else
{ printf(“Multiplication not possible”);
} return 0;

You might also like