0% found this document useful (0 votes)
19 views22 pages

Module 3 Arrays

This document provides an overview of arrays in programming, including their declaration, initialization, and operations such as insertion, deletion, and searching. It explains how to access elements, calculate addresses, and the difference between single and multi-dimensional arrays. Additionally, it includes code examples for various array operations in C programming.

Uploaded by

Kangana W. M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views22 pages

Module 3 Arrays

This document provides an overview of arrays in programming, including their declaration, initialization, and operations such as insertion, deletion, and searching. It explains how to access elements, calculate addresses, and the difference between single and multi-dimensional arrays. Additionally, it includes code examples for various array operations in C programming.

Uploaded by

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

Module-3: Functions and Arrays

Arrays: Declaration of arrays, accessing the elements of an array, storing


values in arrays, Operations on arrays, Passing arrays to functions

Introduction:

• An array is a collection of items of same data type stored at contiguous


memory locations.

• Array of character is a string.

• Each data item of an array is called an element.

• And each element is unique and located in separated memory location.

• Each of elements of an array share a variable but each element having


different index no. known as subscript.

Any element in an array can be accessed using

1. Name of the array

2. Position of the element in an array.

There are 2 types of array

1. Single dimensional array

2. Multi-dimensional array

Declaration of 1-Dimensional arrays

• Arrays are declared using following syntax:

data_type array_name[size];

where, type can be int, float or char.

name is the name of the array.

size indicates number of elements in the array.

Example: int marks[10];

Nisha S K, Dept. of ECE, SVIT Page 1


Module-3: Functions and Arrays

Calculating the address of Array

• Address of data element, A[k] = BA(A) + w( k – lower_bound)

Here,

A is the array

k is the index of the element of which we have to calculate the address

BA is the base address of the array A.

w is the word size of one element in memory, for example, size of int is 2.

Example 1:

Given an array int marks[ ]={99,67,78,56,88,90,34,85}. Calculate the address


of marks[4] if base address=1000.

Address(Marks[4])=1000+2(4-0)// size of int=2

=1000+2*4

=1000+8

=1008

Example 2:

Given an array float avg[ ]={99.0,67.0,78.0,56.0,88.0,90.0,34.0,85.0}. Calculate


the address of avg[4] if base address=1000.

Address(Avg[4]) =1000+4(4-0)// size of float=4

=1000+4*4

=1000+16

=1016

Nisha S K, Dept. of ECE, SVIT Page 2


Module-3: Functions and Arrays

Calculating the length of Array

Length of the array is given by:

Length= upper_bound - lower_bound+1

where

Upper_bound=index of the last element

Lower_bound=index of the first element

Usually Lower_bound is zero but this is not a compulsion.

Example 1:

Let Age[5] be Age[0]=2, Age[1]=5, Age[2]=3, Age[3]=1, Age[4]=7.

Length=Upper_bound-Lower_bound+1

=4-0+1=5

Storing values in an Array

Store
values in
the array

Initialize Input Assign

Initialization can be done using the following syntax:

Nisha S K, Dept. of ECE, SVIT Page 3


Module-3: Functions and Arrays

type array_name[size]={list of values};

1. Initializing all specified memory location:

int a[5]={10,20,30,40,50}

2. Partial array initialization

int a[5]={10,20}

3. Array initialization without size:

int a[ ]={10,20,30,40,50}

4. Array initialization without elements

int a[5]={0}

Inputting values from keyboard

int i, marks[10];

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

scanf(“%d”, &marks[i]);

Nisha S K, Dept. of ECE, SVIT Page 4


Module-3: Functions and Arrays

Assigning values to Individual Elements

int i, arr1[10], arr2[10];

arr1[i]={0,1,2,3,4,5,6,7,8,9};

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

arr2[i] = arr1[i];

WAP to read and write one dimensional array.

#include<stdio.h>

void main()

int i,a[5];

printf("Enter the elements: ");

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

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

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

printf("Array a[%d]=%d\n",i,a[i]);

Output:

Enter the elements: 1 2 3 4 5

Nisha S K, Dept. of ECE, SVIT Page 5


Module-3: Functions and Arrays

Array a[0]=1

Array a[1]=2

Array a[2]=3

Array a[3]=4

Array a[4]=5

WAP to search an element in an array.

#include<stdio.h>

void main()

int i,a[20],n,key;

printf("Enter the number of elements: ");

scanf("%d",&n);

printf("Enter the elements: ");

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

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

printf("Enter the key element to be searched: ");

scanf("%d",&key);

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

if(key==a[i])

printf("Element found at position %d\n",i+1);

Nisha S K, Dept. of ECE, SVIT Page 6


Module-3: Functions and Arrays

Output:

Enter the number of elements: 5

Enter the elements: 1 2 3 4 5

Enter the key element to be searched: 5

Element found at position 5

WAP to print the position of smallest of numbers using array.

#include<stdio.h>

void main()

int i,a[20],n,small,pos;

printf("Enter the number of elements: ");

scanf("%d",&n);

printf("Enter the elements: ");

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

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

small=a[0];

pos=0;

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

if(a[i]<small)

Nisha S K, Dept. of ECE, SVIT Page 7


Module-3: Functions and Arrays

small=a[i];

pos=i;

printf("The smallest element is %d and the position is %d",small,pos+1);

Output:

Enter the number of elements: 5

Enter the elements: 2 3 4 5 6

The smallest element is 2 and the position is 1

Operations on array

1. Traversing an array

2. Inserting an element in an array

3. Deleting an element from an array

4. Merging 2 arrays

5. Searching an element in an array

6. Sorting an array in ascending or descending order

1. Traversing an array

• Traversing an array means accessing each and every element of the array
for a specific purpose.

Algorithm for Array Traversal:

Step 1: [Initialization] SET I=lower_bound


Step 2: Repeat steps 3 and 4 while I<=upper_bound
Step 3: Apply process to A[I]
Step 4: Set I=I+1;
Step 5: Exit

Nisha S K, Dept. of ECE, SVIT Page 8


Module-3: Functions and Arrays

2. Inserting an element in an array


• Inserting an element in an array means adding a new data element to an
already existing array.

Algorithm to insert a new element to the end of the array:

Step 1: Set upper_bound= upper_bound+1//Increment the value of Upper


bound
Step 2: Set A[upper_bound]= VAL(Value that has to be inserted]//New value is
stored
Step 3: EXIT

Algorithm to insert a new element in middle of the array:

Step 1: Set I=N//N=Number of elements


Step 2: Repeat 3 and 4 while I>=POS//POS=position at which element has to
be inserted
Step 3: Set A[I+1]=A[I]
Set A[I+1]=A[I]
Step 4: Set N=N+1
Step 5: Set A[POS]=VAL
Step 6: EXIT

WAP to insert a number at a given location in an array.


#include<stdio.h>
void main()
{
int i,a[20],n,num,pos;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be inserted: ");
scanf("%d",&num);
printf("Enter the postion at which number has to be inserted: ");
scanf("%d",&pos);
for(i=n-1;i>=pos;i--)

Nisha S K, Dept. of ECE, SVIT Page 9


Module-3: Functions and Arrays

a[i+1]=a[i];
a[pos]=num;
n++;
printf("The array after insertion of %d is :",num);
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
Output:

Enter the number of elements: 5

Enter the elements: 1 2 4 5 6

Enter the number to be inserted: 3

Enter the postion at which number has to be inserted: 2

The array after insertion of 3 is : 1 2 3 4 5 6

3. Deleting an element in an array

• Deleting an element from an array means removing a data element from


an already existing array.

Algorithm to delete a new element to the end of the array:

Step 1: Set upper_bound= upper_bound-1

Step 2: EXIT

Algorithm to delete element from middle of the array:

Step 1: Set I=POS// POS=position at which element has to be deleted

Step 2: Repeat 3 and 4 while I<=N-1//N=Number of elements in the array

Step 3: Set A[I] = A[I+1]

Step 4: Set I=I+1

Step 5: Set N=N-1

Step 6: EXIT

Nisha S K, Dept. of ECE, SVIT Page 10


Module-3: Functions and Arrays

WAP to delete a number from a given location in an array.

#include<stdio.h>

void main()

int i,a[20],n,pos;

printf("Enter the number of elements: ");

scanf("%d",&n);

printf("Enter the elements: ");

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

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

printf("Enter the postion from which number has to be deleted: ");

scanf("%d",&pos);

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

a[i]=a[i+1];

n--;

printf("The array after deletion is :");

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

printf("\nA[%d]=%d",i,a[i]);

Output:

Enter the number of elements: 5

Enter the elements: 1 2 3 4 5

Nisha S K, Dept. of ECE, SVIT Page 11


Module-3: Functions and Arrays

Enter the postion from which number has to be deleted: 4

The array after deletion is :

A[0]=1

A[1]=2

A[2]=3

A[3]=4

4. Merging 2 arrays

• Merging of 2 arrays in a third array means first copying the contents of


the first array into the third array and then copying the contents of
second array into the third array.

• Hence, the merged array contains contents of the second array.

WAP to merge 2 unsorted arrays.

#include <stdio.h>

void main()

int a1[10],a2[10],a3[10],i,n1,n2,m,index=0;

printf("Enter the number of elements in array1:");

scanf("%d",&n1);

printf("Enter the elements in array1:");

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

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

printf("Enter the number of elements in array2:");

scanf("%d",&n2);

printf("Enter the elements in array2:");

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

Nisha S K, Dept. of ECE, SVIT Page 12


Module-3: Functions and Arrays

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

m=n1+n2;

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

a3[index]=a1[i];

index++;

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

a3[index]=a2[i];

index++;

printf("\n\nThe merged array is\n");

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

printf("\t Arr3[%d]=%d\n",i,a3[i]);

Output:

Enter the number of elements in array1:3

Enter the elements in array1:1 2 3

Enter the number of elements in array2:3

Enter the elements in array2:4 5 6

The merged array is

Arr3[0]=1

Arr3[1]=2

Nisha S K, Dept. of ECE, SVIT Page 13


Module-3: Functions and Arrays

Arr3[2]=3

Arr3[3]=4

Arr3[4]=5

Arr3[5]=6

5. Searching for a value in an array

• Searching means to find whether a particular value is present in the


array or not.

• If the value is present in the array then search is said to be successful


and the search process gives the location of that array.

• If the value is not present, the search process displays the appropriate
message.

Linear search: ALGORITHM

Step1: [Initialization] Set pos=-1

Step2: [Initialization] Set I=0

Step3: Repeat Step 4 while I<=N

Step4: IF A[I]= val

SET POS=I

PRINT POS

Go to Step 6

[END OF IF]

SET I=I+1

[END OF LOOP]

Step5: IF POS= -1,

PRINT “VALUE IS NOT PRESENT IN THE ARRAY”

[END OF IF]

Step6: EXIT
Nisha S K, Dept. of ECE, SVIT Page 14
Module-3: Functions and Arrays

Program:

#include<stdio.h>

void main()

int a[10],num,i,n,found=0,pos=-1;

printf("Enter the number of elements in an array: ");

scanf("%d",&n);

printf("Enter the elements: ");

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

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

printf("Enter the number that has to be searched: ");

scanf("%d",&num);

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

if(a[i]==num)

found=1;

pos=i;

printf("\n%d is found in the array at position %d",num,i+1);

break;

if(found==0)

printf("Element not found in the array");

Nisha S K, Dept. of ECE, SVIT Page 15


Module-3: Functions and Arrays

Output:

Enter the number of elements in an array: 5

Enter the elements: 50 9 6 7 1

Enter the number that has to be searched: 6

6 is found in the array at position 3

Binary Search:

#include<stdio.h>

void main()

int i,low,high,mid,n,key,a[20];

printf("Enter the number of elements in an array: ");

scanf("%d",&n);

printf("Enter the elements: ");

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

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

printf("Enter the value to find: ");

scanf("%d",&key);

low=0;

high=n-1;

while(low<=high)

mid=(low+high)/2;

if(a[mid]==key)

Nisha S K, Dept. of ECE, SVIT Page 16


Module-3: Functions and Arrays

printf("%d found at location %d",key,mid+1);

break;

else if(a[mid]<key)

low=mid+1;

else

high=mid-1;

if(low>high)

printf("%d not found in the array",key);

Output:

Enter the number of elements in an array: 5

Enter the elements: 1 2 3 4 5

Enter the value to find: 3

3 found at location 3

WAP to sort n numbers in ascending order using bubble sort technique:

#include<stdio.h>

void main()

int i,j,n,temp,a[20];

printf("Enter the number of elements in an array: ");

scanf("%d",&n);

Nisha S K, Dept. of ECE, SVIT Page 17


Module-3: Functions and Arrays

printf("Enter the elements: ");

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

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

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

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

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

printf("Array after implementing bubble sort:");

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

printf("%d\t",a[i]);

Output:

Enter the number of elements in an array: 5

Enter the elements: 60 9 8 5 100

Array after implementing bubble sort: 5 8 9 60 100

Nisha S K, Dept. of ECE, SVIT Page 18


Module-3: Functions and Arrays

WAP to sort n numbers in decending order using bubble sort technique:

#include<stdio.h>

void main()

int i,j,n,temp,a[20];

printf("Enter the number of elements in an array: ");

scanf("%d",&n);

printf("Enter the elements: ");

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

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

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

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

if(a[j]<a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

printf("Array after implememting bubble sort:");

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

Nisha S K, Dept. of ECE, SVIT Page 19


Module-3: Functions and Arrays

printf("%d\t",a[i]);

Output:

Enter the number of elements in an array: 5

Enter the elements: 90 7 6 100 99

Array after implememting bubble sort: 100 99 90 7 6

Using arrays with functions

• Putting individual elements of the array

• Passing the whole array

Passing individual elements of the array

#include<stdio.h>

void square(int x);

void main()

int n,a[10],i;

printf("Enter the number of elements: ");

scanf("%d",&n);

printf("Enter the elements: ");

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

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

printf("The square of given elements are: ");

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

square(a[i]);

Nisha S K, Dept. of ECE, SVIT Page 20


Module-3: Functions and Arrays

void square(int x)

printf("%d\t",x*x);

return;

Output:

Enter the number of elements: 5

Enter the elements: 1 2 3 4 5

The square of given elements are: 1 4 9 16 25

Passing whole array

#include<stdio.h>

void avg(int a[]);

void main()

int b[6]={1,2,3,4,5,6};

avg(b);

void avg(int a[])

int i,Average,sum=0;

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

sum=sum+a[i];

Nisha S K, Dept. of ECE, SVIT Page 21


Module-3: Functions and Arrays

Average=sum/6;

printf("Average=%d",Average);

Output:

Average=3

Nisha S K, Dept. of ECE, SVIT Page 22

You might also like