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

Arrays-unit3 (1)

An array is a data structure that stores a fixed-size collection of elements of the same type, accessible via index numbers. There are two main types of arrays: one-dimensional and multi-dimensional, with various operations such as traversal, searching, insertion, deletion, sorting, and merging applicable to them. Additionally, arrays can be passed to functions in C programming, and sparse matrices are defined as matrices with a majority of zero elements.

Uploaded by

sudharani.am
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Arrays-unit3 (1)

An array is a data structure that stores a fixed-size collection of elements of the same type, accessible via index numbers. There are two main types of arrays: one-dimensional and multi-dimensional, with various operations such as traversal, searching, insertion, deletion, sorting, and merging applicable to them. Additionally, arrays can be passed to functions in C programming, and sparse matrices are defined as matrices with a majority of zero elements.

Uploaded by

sudharani.am
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Arrays

Q) What is Array?
Array is a kind of data structure that can store a fixed-size sequential collection of
elements of the same type
(OR)
An array is a collection of data that holds fixed number of values of same type and share same
name.
(OR)

Array is a collection of homogenous data stored under unique name. The values in an
array are called as 'elements of an array.' These elements are accessed by numbers called as
'subscripts or index numbers.' Arrays may be of any variable type.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.

Q) What are different types of arrays?


Based on the subscript we used arrays are classified into
There are 2 types of C arrays. They are,
1. One dimensional array
2. Multi dimensional array
 Two dimensional array
 Three dimensional array
 Four dimensional arrays etc…

Single dimensional arrays:

1. Single or One Dimensional array is used to represent and store data in a linear form.
2. Array having only one subscript variable is called One-Dimensional array
3. It is also called as Single Dimensional Array or Linear Array

1. Declaration:

Syntax:
<data-type><array_name> [size];

Syntax
Significance
Parameter

data_type Data Type of Each Element of the array

Array_name Valid variable name

size Maximum number of elements in the array


1
Example:
int a[3];--it is an array name a that can store 3 integer values.
char name[20];--It is an array name is name that can store 20 characters.
float salary[5]; --It is an array name is salary that can store 5 float values.

2) Initializing single dimensional arrays:

Initializing array means assigning values to the array. Arrays can be initialized in two ways:

i) Compile time initialization:


Compile time initialization of array elements is same as ordinary variable initialization. The
general form of initialization of array is,

Syntax:
Data type arr_name[size]={value1,value2,value3….};
(OR)
Data type arr_name[ ]={value1,value2,value3….};

Examples:

int marks[4]={ 67, 87, 56, 77 }; //integer array initialization


float area[ ]={ 23.4, 6.8, 5.5 }; //float array initialization that have size 3.

ii) run-time array initialization:


An array can also be initialized at runtime using scanf() function. This approach is usually used for
initializing large array, or to initialize array with user specified values.
Example:
int a[4];
for(i=0;i<4;i++)
{
scanf(“%d”,&a[i]);
}

3. Accessing Array Elements:


An element is accessed by indexing the array name. This is done by placing the index of the
element within square brackets after the name of the array.
Syntax: arr_name[index];

Int a[4]={1,2,3,4};
Here
a[0]=1; a[1]=2;a[2]=3;a[3]=4;

Program :/* Program to demonstrate one dimensional array.


#include <stdio.h>
#include <conio.h>
void main()
{
int a[3], i;;
clrscr();

2
printf("\n\t Enter three numbers : ");
for(i=0; i<3; i++)
{
scanf("%d", &a[i]); // read array
}
printf("\n\n\t Numbers are : ");
for(i=0; i<3; i++)
{
printf("\t %d", a[i]); // print array
}
getch();
}

Write a program to accept values into array and display them at run time.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n;
clrscr();
printf(“enter the size of array\n”);
scanf(“%d”,&n);
printf(“enter the elements into array\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“the array elements are\n”);
for(i=0;i<n;i++)
printf(“%d is %d\n”,i,a[i]);
getch();
}
Output:
Enter the size of array 3
Enter the elements into array 1
2
3
The array elements are 1 2 3

Q) Operations on one dimensional array:


Operations can be performed on single dimensional arrays are:
1. Traversing
2. Searching
3. Insertion
4. Deletion
5. Sorting
6. Merging

3
1. Traversing:
Traversing means accessing each and every element of the array for a specific purpose. An array is
homogeneous data elements, then traversing of the data elements can include printing every
element, counting the total number of elements, or performing any process on these elements.

2) Searching:
 Searching means weather a particular element value is present in the array or not.
 If the value is present searching returns the position of the value otherwise it returns
appropriate message.
 There are two popular methods for searching
Linear search:-It is sequential search by comparing every element of the array one by one in
Sequence.
Binary search:-It is a searching algorithm that works on sorted list.
We have linear array A as below:
A={15,50,35,20,25};
Suppose item to be searched is 20.
We will start from beginning and will compare 20 with each element. This process will continue until
element is found or array is finished. Here:
1)Compare20with15
20 # 15, go to next element.
2)Compare20with50
20#50,gotonextelement.
3)Compare20with35
20#35,gotonextelement.
4)Compare20with20
20 = 20, so 20 is found and its location is 4.

3. Insertion: Insertion means adding a new element in an already existing array.

Example:
A={10,20,50,30,15};
New element to be inserted is 100 and location for insertion is 3. So shift the elements from 5th
location to 3rd location downwards by 1 place. And then insert 100 at 3rd location. It is shown below:

4. Deletion: Deleting an element from the array means removing a data element from an already
existing array.
Examlpe:
A={10,20,50,40,25};
The element to be deleted is 50 which is at 3rd location. So shift the elements from 4th to 6th location
upwards by 1 place. It is shown below:
4
After deletion the array will be:
1 2 3 4 5 6
1 6
0 20 40 25 0

5. Sorting: It is used to arrange the data items in some order i.e. in ascending or descending order
in case of numerical data and in dictionary order in case of alphanumeric data.
E.g.
We have linear array A as below:
1 2 3 4 5
10 50 40 20 30

After arranging the elements in increasing order by using a sorting technique, the array will be:
1 2 3 4 5
10 20 30 40 50

6.Merging: It is used to combine the data items of two arrays into single array.
Example:
a={1,4,2,5};
b={5,8,3}
After merging merged array C is as below:
Merged array c={1,4,2,5,5,8,3};

multi dimensional arrays:

1. Array having more than one subscript variable is called Multi-Dimensional array.
2. Multi Dimensional Array is also called as Matrix.
i) Two dimensional array
1. Two Dimensional Array requires Two Subscript Variables
2. Two Dimensional Array stores the values in the form of matrix.
3. One Subscript Variable denotes the “Row” of a matrix.
4. Another Subscript Variable denotes the “Column” of a matrix.

5
i) Declaring Two-Dimensional Arrays
Syntax:

<data-type><array_nm> [row_subscript][column-subscript];

Example:
int a[3][3];
In above example, a is an array of type integer which has storage size of 3 * 3 matrix.
The total elements are 3*3 =9.

ii) Initializing Two-Dimensional Arrays:

Initializing array means assigning values to the array. Arrays can be initialized in two ways:

i) Compile time initialization:


Two dimensional arrays may be initialized by specifying bracketed values for each row.

int c[2][3]={{1,3,0}, {-1,5,9}};


OR
int c[][3]={{1,3,0}, {-1,5,9}};
OR
int c[2][3]={1,3,0,-1,5,9};

ii) Runtime initialization:


An array can also be initialized at runtime using scanf() function. This approach is usually used for
initializing large array, or to initialize array with user specified values.

Example:
Int a[2][2];
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf(“%d”,a[i][j]);
Program :
/* Program to demonstrate two dimensional array.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[3][3], i, j;
clrscr();
printf("\n\t Enter matrix of 3*3 : ");
for(i=0; i<3; i++)
{
6
for(j=0; j<3; j++)
{
scanf("%d",&a[i][j]); //read 3*3 array
}
}
printf("\n\t Matrix is : \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("\t %d",a[i][j]); //print 3*3 array
}
printf("\n");
}
getch();
}

Q) Operations that can be performed on two dimensional arrays

1. Transpose: transpose of a m*n matrix A is given as a n*m matrix B.


2. Sum (or) addition:
1. One Matrix can be added with another only if the order of both matrices is Equal
2. No of rows of MAT-1 = No of rows of MAT-2
3. No of col of MAT-1 = No of col of MAT-2
4. During addition a[0][0] is added with b[0][0] and result is stored in c[0][0]

3. Difference (OR) subtraction:


1. One Matrix can be subtracted with another only if the order of both matrices is Equal
2. No of rows of MAT-1 = No of rows of MAT-2
3. No of columns of MAT-1 = No of columns of MAT-2.
4. During addition a[0][0] is subtracted with b[0][0] and result is stored in c[0][0].

4. Product (or) multiplication:


Matrix multiplication is possible is number of columns in first matrix is equal to the number of rows in
second matrix.
For example:
Suppose two matrixes A and B of size of 2 x 2 and 2 x 3 respectively:

7
Q) Arrays and Functions (OR) How to pass array to functions:
In C programming, a single array element or an entire array can be passed to a function. Also, both
one-dimensional and multi-dimensional array can be passed to function as argument.

Passing One-dimensional Array to Function


C program to pass a single element of an array to function
#include<stdio.h>
void display(int a)
{
printf("%d",a);
}
int main()
{
int c[]={2,3,4};
display(c[2]);//Passing array element c[2] only.
return0;
}
Output
4

Passing entire array to a function


While passing arrays to the argument, the name of the array is passed as an argument (,i.e, starting
address of memory area is passed as argument).
Example:
#include<stdio.h>
void disp(int a[])
{
int i;
for(i=0;i<3;i++)
printf("%d\t",a[i]);
}
void disp1(int a[2][2])
8
{
int i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("%d\t",a[i][j]);
}
void main()
{
int a[3]={1,2,3};
int b[2][2]={10,20,30,40};
clrscr();
printf("the array a elements are\n");
disp(a);
printf("\n the two dimensional array b elemts are\n");
disp1(b);
getch();
}

Q) Sparse matrices:
a sparse matrix is a matrix in which most of the elements are zero.
Basically ,there are two types of sparse matrices.
In the first type of sparse matrix all elements above or below the diagonal have zeros.

Lower triangle matrix upper triangular matrix.


In the second type elements with a non-zero values can appear only in the diagonal.

Lab Programs:
1. Write a program to search a element in the array.

#include<stdio.h>
#include<conio.h>

void main()
{
int a[10];
int n,i,x,flag=0;
clrscr();
printf("enter the array size\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the array elements are\n");
9
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n enter the element to search element\n");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(a[i]==x)
{
printf("\nelement %d found at %d position",x,(i+1));
flag=1;
break;
}
}
if(flag==0)
printf("\nelement %d not found",x);
getch();
}

2. write a program to sort an array elements.

#include<conio.h>

void main()
{
int a[10],i,j,n,temp;
clrscr();
printf("enter the array size\n");
scanf("%d",&n);
printf("enter elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n array elements before deletion are \n");
for(i=0;i<n;i++)
printf("%d",a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
If(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf(“\n array elements after sortinf are\n");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
10
}

3. Write a program for matrix addition:

#include<stdio.h>
#include<conio.h>

void read_arr(int a[10][10],int row,int col)


{
int i,j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);

void add_arr(int m1[10][10],int m2[10][10],int m3[10][10],int row,int col)


{
int i,j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
m3[i][j] = (m1[i][j] + m2[i][j]);

void print_arr(int m[10][10],int row,int col)


{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d ",m[i][j]);
}
printf("\n");
}
}

void main()
{
int a[10][10],b[10][10],c[10][10],m1,n1,m2,n2;
clrscr();
printf("Enter row and col size os matrix A\n");
scanf("%d%d",&m1,&n1);
printf("Enter row and col size of matrix B\n");
scanf("%d%d",&m2,&n2);
if(m1==m2 && n1==n2)
{
printf("enter A matrix elements\n");
read_arr(a,m1,n1);
11
printf("enter B matrix elements\n");
read_arr(b,m2,n2);
add_arr(a,b,c,m1,n1);
printf("the addition of A and B matrix is\n");
print_arr(c,m1,n1);
}
else
printf("matrix addition not possible");
getch();
}

4. Write a program for Matrix multiplication:

#include<stdio.h>
#include<conio.h>

void read_arr(int a[10][10],int row,int col)


{
int i,j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);

void add_arr(int a[10][10],int b[10][10],int c[10][10],int m1,int n1,int n2)


{
int i,j,k;

for (i=0;i<m1;i++)
for(j=0;j<n2;j++)
{
c[i][j]=0;
for(k=0;k<n1;k++)
c[i][j]+= a[i][k]*b[k][j];
}

void print_arr(int m[10][10],int row,int col)


{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d ",m[i][j]);
}
printf("\n");
}
12
}

void main()
{
int a[10][10],b[10][10],c[10][10],m1,n1,m2,n2;
clrscr();
printf("Enter row and col size os matrix A\n");
scanf("%d%d",&m1,&n1);
printf("Enter row and col size of matrix B\n");
scanf("%d%d",&m2,&n2);
if(n1==m2)
{
printf("enter A matrix elements\n");
read_arr(a,m1,n1);
printf("enter B matrix elements\n");
read_arr(b,m2,n2);
add_arr(a,b,c,m1,n1,n2);
printf("the multiplication of A and B matrix is\n");
print_arr(c,m1,n2);
}
else
printf("matrix multiplication not possible");
getch();
}

5. Write a c program to find both the largest and smallest number in a list of integers.

#include<stdio.h>
void main()
{
int a[50],size,i,big,small;
clrscr();
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter elements in to the array\n");
for(i=0;i<size;i++)
scanf("%d",&a[i]);

big=a[0];
for(i=1;i<size;i++)
{
if(big<a[i])
big=a[i];
}
printf("\nLargest element: %d",big);

small=a[0];
for(i=1;i<size;i++)
13
{
if(small>a[i])
small=a[i];
}
printf("\nSmallest element: %d",small);

getch();
}

14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy