unit 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 40

UNIT II

ARRAY
PRESENTED BY

Ms.S.JANANI,DEPT OF CSE,
ASSISTANT PROFESSOR
SYLLABUS

Introduction to Arrays – One dimensional arrays:


Declaration – Initialization - Accessing elements –
Operations: Traversal, Insertion, Deletion,Searching –
Two dimensional arrays: Declaration –Initialization
Accessing elements – Operations:Read – Print – Sum
– Transpose – Exercise Programs: Print the number
of positive and negative values present in the array
Sort the Numbers using bubble sort - Find whether
the given is matrix is diagonal or not.
ARRAY
 Array is a fixed size

sequential collection of
elements of same data types
that share a common name.
 Example where arrays are

used,
to store list of Employee
or Student names,
to store marks of
ARRAY TYPES
 Singledimensional array
 Two dimensional array
 multi-dimensional array

SINGLE DIMENSIONAL ARRAY


 A variable which represent the list of

items using only one index(subscript)is


called one dimensional array
DECLARING AN SINGLE
DIMENSIONAL ARRAY
 Like any other variable, arrays

must be declared before they are


used.
 General form of array declaration

is,
data-type variable-name[size];
/* Example of array declaration */
int arr[10];
DECLARING AN SINGLE
DIMENSIONAL ARRAY (CONT…)

Here int is the data type, arr is the


name of the array and 10 is the size
of array. It means array arr can only
contain 10 elements of int type.
Index of an array starts
from 0 to size-1 i.e first element
of arr array will be stored
INITIALIZATION OF SINGLE
DIMENSIONAL ARRAY
An one dimensional array can be
initialized in two ways.
 compile time and Run time

COMPILE TIME ARRAY


INITIALIZATION
data-type array-name[size] =
{ list of values };
/* Here are a few examples */
int marks[4]={ 67, 87, 56, 77 }; //
integer array initialization
Compile time array
Initialization(EXAMPLE)
#include<stdio.h>
void main()
{
int i;
int arr[] = {2, 3, 4}; // Compile time array
initialization
for(i = 0 ; i < 3 ; i++)
{
printf("%d\t",arr[i]);//traversal
Output
}
} 234
RUNTIME ARRAY
INITIALIZATION
 An array can also be initialized at
runtime using scanf() function.
 This approach is usually used for

initializing large arrays, or to initialize


arrays with user specified values.
 Example,

#include<stdio.h>
void main()
{
int arr[4],i, j;
CONT…
printf("Enter array element");
for(i = 0; i < 4; i++)
{
scanf("%d", &arr[i]); //Run time array
initialization
}
for(j = 0; j < 4; j++)
{
printf("%d\n", arr[j]); //traversal
}
}
Accessing elements operations

Accessing elements in an
single dimensional array

 Traversal
 Insertion
 Deletion
 Searching
INSERTION
 Input the array elements, the
position of the new element to
be inserted and the new
element.
 Insert the new element at that

position and shift the rest of the


elements to right by one
position.
C program to insert an element
in an array
#include <stdio.h>
int main()
{
int n;
scanf(“%d”,&n);
int arr[n];
int i;
for(i = 0; i < n; i++)
{
scanf(“%d”,&arr[i]);
}
int pos;
scanf(“%d”,&pos);
int ele;
scanf(“%d”,&ele);
if(pos > n)
printf(“Invalid Input”);
else
{
for (i = n – 1; i >= pos – 1; i–)
arr[i+1] = arr[i];
arr[pos-1] = ele;
printf(“Array after insertion is:\n”);
for (i = 0; i <= n; i++)
printf(“%d\n”, arr[i]);
}
return 0;
}
OUTPUT
5 (size of the array)
1 (array elements)
2
3
4
5
4 (Position)
10 (Element to be inserted)
Array after insertion is : 1 2 3 10 4 5
Program to delete an element in an
array
C program to delete an element in an array */
#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf(“Enter the number of elements of the array :
“);
scanf(“%d”, &n);
printf(“\nInput the array elements : “);
for (c = 0; c < n; c++)
scanf(“%d”, &array[c]);
printf(“\nEnter the position : “);
scanf(“%d”, &position);
if (position >= n+1)
printf(“\nDeletion not possible.\n”);
else
{
for (c = position – 1; c < n – 1; c++)
array[c] = array[c+1];
printf(“\nArray after deletion : “);
for (c = 0; c < n – 1; c++)
printf(“%d\n”, array[c]);
}
return 0;
}
OUTPUT
5 (size of the array)
1 (array elements)
2
3
4
5
4 (Position to be deleted)
Array after deletion is : 1 2 3 5
Program to search an
element in an array
// C program to search an element in an array
#include <stdio.h>
int main()
{
int array[100], ele, c, n;
printf(“Enter the number of elements of the
array : “);
scanf(“%d”, &n);
printf(“\nInput the array elements : “);
for (c = 0; c < n; c++)
scanf(“%d”, &array[c]);
printf(“\nEnter element : “);
scanf(“%d”, &ele);
for(c = 0; c < n ; c++)
{
if(array[c] == ele)
{
printf(“\nElement found\n”);
}
}
return 0;
}
OUTPUT
5 (size of the array)
1 (array elements)
2
3
4
5
5 (Element to be searched)
Element found
TWO DIMENSIONAL ARRAY
Two-dimensional array are
declared as follows,
data-type array-name[row-size]
[column-size
/* Example */
int a[3][4];
An array can also be declared and
initialized together. For example,
int arr[2][2]={1,2,3,4};
int arr[2][2]={ {1,2},{3,4} }
int arr[][2]={1,2,3,4}
int arr[2][]={1,2,3,4}//invalid
int a[][]={1,2,3,4}//invalid

Column Size is compulsary


Runtime initialization of a two dimensional
Array
#include<stdio.h>
void main()
{
int arr[3][4];
int i, j, k;
printf("Enter array element");
for(i = 0; i < 3;i++)
{
for(j = 0; j < 4; j++)
{
scanf("%d", &arr[i][j]);
}
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
{
printf("%d", arr[i][j]); //Print an array
}
}
}
ADDITION OF TWO MATRIX
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10]
[10], sum[10][10];
printf("Enter the number of rows and columns
of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
printf("Sum of entered matrices:-\n");
for (c = 0; c < m; c++)
{
for (d = 0 ; d < n; d++)
{
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
return 0;
}
OUTPUT

Enter the number of rows and columns of matrix


2
2
Enter the elements of first matrix
12
34
Enter the elements of Second matrix
45
-1 5
Sum of entered matrices
57
29
TRANSPOSE OF MATRIX
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
// Assigning elements to the matrix
printf("\nEnter matrix elements:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
// Displaying the matrix a[][]
printf("\nEntered 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");
}
// Finding the transpose of matrix a
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
transpose[j][i] = a[i][j];
}
// Displaying the transpose of matrix a
printf("\nTranspose of the matrix:\n");
for (i = 0; i < c; ++i)
for (j = 0; j < r; ++j)
{
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
OUTPUT
Enter rows and columns: 2 3
Enter matrix elements:
Enter element a11: 1
Enter element a12: 4
Enter element a13: 0
Enter element a21: -5
Enter element a22: 2
Enter element a23: 7
Entered matrix:
1 4 0
-5 2 7
Transpose of the matrix:
1 -5
4 2
0 7
Print the number of positive and
negative values present in the array
#include<stdio.h>
int main()
{
int Size, i, a[10];
int Positive_Count = 0, Negative_Count = 0;
printf("\n Please Enter the Size of an Array : ");
scanf("%d", &Size);
printf("\nPlease Enter the Array Elements\n");
for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}
for(i = 0; i < Size; i ++)
{
if(a[i] >= 0)
{
Positive_Count++;
}
else
{
Negative_Count++;
}
}
printf(“\n Total Number of Positive Numbers in this Array = %d “, Positive_Count);
printf(“\n Total Number of Negative Numbers in this Array = %d “,
Negative_Count);
return 0;
}
`
Sort the numbers using bubble sort
#include <stdio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, j, num, temp;
printf("Enter the value of num \n");
scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array is \n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
/* Bubble sorting begins */
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
}
OUTPUT
$ cc pgm21.c
$ a.out
Enter the value of num 6
Enter the elements one by one
23 45 67 89 12 34
Input array is
23 45 67 89 12 34
Sorted array is...
12 23 34 45 67 89
Find whether the given is matrix is diagonal
or not
#include <stdio.h>
#define MAXROWS 10
#define MAXCOLS 10
int main()
{
int i, j, row, col, flag = 0;
int matrix[MAXROWS][MAXCOLS];
/* get the number of rows from the user */
printf("Enter the number of rows:");
scanf("%d", &row);
/* get the number of columns from the user */
printf("Enter the number of columns:");
scanf("%d", &col);
/* Boundary Check */
if (row > MAXROWS || row < 0 || col > MAXCOLS || col < 0)
{
printf("Boundary Level Exceeded!!\n");
return 0;
}
/* get the entries for the input matrix */
printf("\nEnter the matrix entries:\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
scanf("%d", &matrix[i][j]);
}
}
/* checking for diagonal matrix */
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (i != j && matrix[i][j] != 0)
{
flag = 1;
goto end;
}
}
}
end:
/* printing the result */
if (flag)
{
printf("Given Matrix is not a diagonal matrix!!\
n");
}
else
{
printf("Given Matrix is a diagonal matrix!!\n");
}
return 0;
}
OUTPUT
Enter the number of rows:3
Enter the number of columns:3
Enter the matrix entries:
10 00 00
00 10 00
00 00 11
Given Matrix is a diagonal matrix!!

You might also like