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

Module 3

Covers basic topics related to arrays and functions

Uploaded by

ushashank720
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Module 3

Covers basic topics related to arrays and functions

Uploaded by

ushashank720
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 118

Module-3

Chapter1-Functions
Introduction
Every C program should consist of one or more functions. Among these
functions main() function is compulsory. All programs start execution
from main function.
Functions
• A function is a block of code to perform a specific task.
• Every c program has atleast one function main(). Without main() function, there is
technically no c program.
• In modular programming, the program is divided into separate small programs
called modules.
• Each module is designed to perform a specific task.
• Modules make our actual program shorter. Hence easier to read and write.
Advantages of Functions
Functions based modular programming is advantageous in many ways:
• Managing huge programs and software packages is easier by dividing them into
functions/modules—Maintenance is easier
• Error detection is easier—Debugging is easier
• Functions once written can be re-used in any other applications – Reusability is
enhanced
• We can protect our data from illegal users—Data protection becomes easier
Top-down modular programming using
functions
Modular programming
• Modular programming is defined as organizing a large program into small,
independent program segments called modules that are separately named and
individually callable program units.
• It is basically a “divide-and-conquer” approach to problem solving.
Characteristics of Modular programming:
1. Each module should do only one thing.
2. Communication between modules is allowed only by calling module.
3. No communication can take place directly between modules that do not have
calling-called relationship.
4. All modules are designed as single-entry, single-exit systems using control
structures.
5. A module can be called by one and only higher module.
Function types
1. Built-in(library) function
2. User defined function
Advantages of user defined functions
• Decompose the large program into small segments which makes the programmer
easier to understand, maintain and debug.
• If repeated code occurs in a program, then function can include those codes and
execute when needed by calling that function.
• Programmer working on large project can divide the workload by making
different functions.
Basic structure of c program with functions
Actual and Formal parameters/arguments
• Arguments refer to data that is passed to function(function
definition) while calling
• Arguments listed in function call are referred to as actual arguments.
• The arguments used in function declaration are called formal
parameters/arguments. They receive values from actual parameters
• The number of actual and formal arguments and their datatypes
should always be same.
Location of Functions
#include<stdio.h>
void func(int arr[]);
int main()
{
int arr[10], i;
printf("Enter 10 array elements: ");
for(i=0; i<10; i++)
scanf("%d", &arr[i]);
printf("\nPassing array to the function...\n");
func(arr);
return 0;
}
void func(int arr[])
{
int i;
printf("\nThe array is:\n");
for(i=0; i<10; i++)
printf("%d ", arr[i]);
}
Categories of Functions
Void Functions without parameters – No arguments and no return values

• When a function has no arguments, it does not receive any data from
the calling function.
• Similarly, when it does not return a value, the calling function does
not receive any data from the called function.
Void Functions with parameters – Arguments but no return values

• The calling function accepts the input, checks its validity and pass it
on to the called function.
• The called function does not return any values back to calling
function.
• The actual and formal arguments should match in number, type and
order.
Functions with parameters and return values – Arguments with return values

• The calling function sends the data to called function and also accepts
the return values from called function.
• Actual Parameters: When a function is called, the values that are
passed in the call are called actual parameters.
• Formal Parameters:The values which are received by the function,
and are assigned to formal parameters.
Functions without parameters and with return values – No arguments but returns values

• The calling function does not send any data to called function and
but, accepts the return values from called function
Inter-Function Communication

• Two functions can communicate with each other by


two methods:
• By Passing parameters as values (Pass by value)
• By passing parameters as address (Pass by address)
By Passing parameters as values (Pass by value):

• In pass by value calling function sends the copy of parameters( Actual


Parameters) to the called function (Formal Parameters).

• The change does not affect the actual value.


By Passing parameters as address (Pass by
address)

• In pass-by-reference the calling function sends the address of


parameters (Actual Parameters) to the called function (Formal
Parameters).

• The change affects the actual value.


PASSING ARRAYS TO FUNCTIONS

• In large programs that use functions we can pass Arrays as


parameters. Two ways of passing arrays to functions are:
• Pass individual elements of array as parameter
• Pass complete array as parameter
• Let us take an example program that uses a function square( ) to
calculate square of numbers stored in an array. In this program each
array element is passed one by one.
• Next program illustrates how to pass an entire array to a function
average( ) and calculate average marks. First sum of all the elements
of array marks (i.e.35+65+75+95+85) is calculated and average is
stored in a variable avg. value in avg variable is returned to main( )
and stored in avg1
PASSING STRINGS TO FUNCTIONS
Basic rules are:
• The string to be passed must be declared as a formal argument of the
function when it is defined.
EX: void display(char item_name[])
{
......
......
}
• The function prototype must show that the argument is a string. Ex: void
display(char str[])
• A call to the function must have a string array name without subscripts as
its actual argument.
Ex: display(names);
• Where names is a properly declared string in the calling function.
C program to show use of call by value
#include <stdio.h> int main()
{
void swap(int var1, int var2) int var1 = 3, var2 = 2;
{ printf("Before swap Value of var1
int temp = var1; and var2 is: %d, %d\n",var1, var2);
var1 = var2; swap(var1, var2);
var2 = temp; printf("After swap Value of var1
and var2 is: %d, %d",var1, var2);
}
return 0;
}
C program to show use of call by Reference

#include <stdio.h> int main()


void swap(int *var1, int *var2) {
{ int var1 = 3, var2 = 2;
int temp = *var1; printf("Before swap Value of var1
*var1 = *var2; and var2 is: %d, %d\n",var1, var2);
*var2 = temp; swap(&var1, &var2);
} printf("After swap Value of var1
and var2 is: %d, %d",var1, var2);
return 0;
}
Write a C program to find factorial of a
number using functions.
#include<stdio.h> int fact()
#include<math.h> {
int main() int i,fact=1,n;
{ scanf("%d",&n);
printf("Enter a Number to Find for(i=1; i<=n; i++)
Factorial: "); {
printf("\nFactorial of a Given Number fact=fact*i;
is: %d ",fact());
}
return 0;
return fact;
}
}
C program to Swap two numbers using the
function
#include<stdio.h> void swap(int x, int y)
void swap(int, int); {
int main() int temp;
{ temp = x;
int a, b; x = y;
printf("Enter values for a and b\n"); y = temp;
printf("\nAfter swapping: a =
scanf("%d%d", &a, &b); %d and b = %d\n", x, y);
printf("\n\nBefore swapping: a = }
%d and b = %d\n", a, b);
swap(a, b);
return 0;
}
C Program To Find Biggest of Three Numbers using
Function

#include<stdio.h> // function definition


int biggest(int, int, int); // function prototy int biggest(int x, int y, int z)
pe
{
int main()
if(x > y && x > z)
{
{
int a, b, c;
return x;
printf("Enter 3 integer numbers\n");
}
scanf("%d%d%d",&a,&b,&c);
else
//function call biggest(a,b,c)
{
printf("Biggest of %d,%d and
%d is %d\n",a,b,c,biggest(a,b,c)); if(y > z)
return 0; return y;
} else
return z;
}
}
C Program using function to find the area
of circle
# include < stdio.h > float area(float r)
# include < conio.h > {
float area(float) ;
float ar ;
int main( ) ar = 3.14 * r * r ;
{ return ar ;

float r, ar = 0.0 ; }
printf(" Enter the radius of circle : ") ;
scanf("%f", &r) ;
ar = area(r) ;
printf("\n Area of circle : %f ", ar) ;
return (0);
}
C Program to check even or odd
number
#include <stdio.h> /* If isEven() function returns 0 then the
int isEven(int num) number is even */
{ if(isEven(num))
return !(num & 1); {
} printf("The number is even.");
int main() }
{ else
int num; {
printf("The number is odd.");
/* Input number from user */ }
printf("Enter any number: ");
scanf("%d", &num); return 0;
}
Module-3

Chapter 1
Arrays
Contd...
Arrays are used to represent multiple data items
of the same type using single name.
Structure of an Array
Example of an array
One-dimensional array
A list of items can be given one variable name using
only one subscript and such a variable is called as
one-dimensional array.
• Ex: int marks[4];
Declaration of one-dimensional array
Syntax:
data_type array_name[array_size];

Examples: int marks[4];


float temperature[5];

Note: Declaration specifies the data type of array, its


name and size.
Initialization of one-dimensional
arrays
Values can be stored in arrays using following methods:
1. Initializing all specified memory locations
(compile time initialization)
2. Initializing individual elements (compile time
initialization)
3. Partial array initialization
4. Initialization during program execution (runtime
initialization)
Initializing all specified memory
locations (compile time initialization)
we can initialize values to all the elements of the array.
Syntax:
data_type array_name[array_size]={list of values};
data_type can be int, float, or char
array_name is the name of the array
array_size indicates number of elements in an array
list of values are values of same data type
Examples: int marks[4]={ 95,35, 67, 87};
float temperature[3]={29.5, 30.7, 35.6};
Initializing individual elements
(compile time initialization)
We needn’t have to specify the size of array provided
we are initializing the values in beginning itself.
Syntax:
data_type array_name[index]={list of values};
Examples: int marks[4]={ 95,35, 67, 87};
float temperature[ 5]={29.5, 30.7, 35.6, 45.7, 19.5};
marks[0]=95; temperature[0]=29.5;
marks[1]=35; temperature[1]=30.7;
marks[2]=67; temperature[2]=35.6;
marks[3]=87; temperature[3]=45.7;
temperature[4]=19.5;
Partial array initialization
C language allows the number of values to be initialized is less
than the size of the array, then the remaining locations will be
initialized to zero, automatically by the compiler.

Example1: int marks[5]={10,12,20};


Here, marks[3] and marks[4] will be initialized to zero.
10 12 20 0 0

Example2: int age[5]={2,3};

2 3 0 0 0
Initialization during program
execution (runtime initialization)
The below example shows the reading and writing to
the one-dimensional array:

Example: int age[5];


Reading: for(i=0;i<5;i++)
scanf(“%d”,&age[i]);
Writing: for(i=0;i<5;i++)
printf(“%d”,age[i]);
Write a program in C to read an array
containing n numbers and find sum and average of
given numbers.
#include<stdio.h> /* Finding sum */
int main() for(i=0; i< n; i++)
{ {
float a[100], sum=0, avg; sum = sum + a[i];
int i, n; }
printf("Enter n: "); /* Calculating average */
scanf("%d", &n); avg = sum/n;
/* Reading array */ /* Displaying Result */
printf("Enter numbers:\n"); printf("Sum is %f\n", sum);
for(i=0; i< n; i++) printf("Average is %f", avg);
{ return 0;
printf("a[%d] = ", i); }
scanf("%f", &a[i]);
}
Output
Initialization of Two-Dimensional
Array
• After array is declared, next is storing values into
an array is called initialization.
• There are two types of array initialization:
1. Compile-time initialization
2. Run-time initialization
Compile time initialization
• If we assign values to the array during declaration it
is called compile time initialization.
Syntax:
data_type array_name[row_size][column_size]={list ofvalues};
Examples: int marks[3][4]={ 1,2,3, 4, 5, 6, 7,8,9,10,11,12};
float city_temper[2][2]={29.5, 30.7, 35.6, 45.7};

• Note: In the above examples we are storing values in


marks array and temperature array respectively. It can be
represented pictorially as well.
Run time initialization
Run time initialization is storing values in an array when
program is running or executing.
Following example illustrates run time storing of values
using scanf and for loop:
Example: printf(“Enter the marks”);
for(i=0; i<3; i++)
for(j=0;j<3;j++)
{
scanf(“ %d”, &marks[i][j]);
}
Write a program to print the elements of a 2D array
#include<stdio.h>
int main()
{
int rows, columns;
int a[2][2] = {{10, 20}, {30, 70}};
printf("\nPrinting the 2D Array\n");
for(int rows = 0; rows < 2; rows++)
{
for(int columns = 0; columns < 2; columns++)
{
printf("%d ", a[rows][columns]);
}
printf("\n");
}
return 0;
}
Write a program to transpose a 3*3 matrix
#include <stdio.h>
void main()
{
static int array[10][10],transpose[10][10];
int i, j, m, n;
printf("Enter the order of the matrix \n");
scanf("%d %d", &m, &n);
printf("Enter the coefficients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array[i][j]);
}
printf("\n");
}
for (i = 0;i < m;i++)
{
for (j = 0; j < n; j++)
{
transpose[j][i] = array[i][j];
}
}
// printing the transpose of a matrix
printf("Transpose of matrix is \n");
for (i = 0; i< n; i++)
{
for (j = 0; j < m; j++)
{
printf("%d ", transpose[i][j]);
}
printf("\n");
}
}
Output

Enter the order of the matrix


22
Enter the coefficients of the matrix
1234
The given matrix is
12
34
Transpose of matrix is
13
24
Advantages of arrays
• Arrays represent multiple data items of the same type using a
single name.
• In arrays, the elements can be accessed randomly by using the
index number.
• Arrays allocate memory in contiguous memory locations for
all its elements. Hence there is no chance of extra memory
being allocated in case of arrays. This avoids memory
overflow or shortage of memory in arrays.
Contd....
• Using arrays, other data structures like linked lists, stacks,
queues, trees, graphs etc can be implemented.
• Two-dimensional arrays are used to represent matrices.
Disadvantages of arrays
• The number of elements to be stored in an array should be known in
advance.
• An array is a static structure (which means the array is of fixed size).
Once declared the size of the array cannot be modified. The memory
which is allocated to it cannot be increased or decreased.
• Insertion and deletion are quite difficult in an array as the elements
are stored in consecutive memory locations and the shifting
operation is costly.
• Allocating more memory than the requirement leads to wastage of
memory space and less allocation of memory also leads to a
problem.
• An array which is formed will be homogeneous. Thus, no array can
have values of two data types.
• An array does not check boundaries.
• Shifting is required for insertion or deletion of elements in an array.
Applications of arrays
1) Array stores data elements of the same data type.
2) Maintains multiple variable names using a single name. Arrays
help to maintain large data under a single variable name. This
avoid the confusion of using multiple variables.
3) Arrays can be used for sorting data elements. Different sorting
techniques like Bubble sort, Insertion sort, Selection sort etc
use arrays to store and sort elements easily.
4) Arrays can be used for performing matrix
operations. Many databases, small and large, consist of one-
dimensional and two-dimensional arrays whose elements
are records.
5) Arrays can be used for CPU scheduling.
6) Lastly, arrays are also used to implement other data structures
like Stacks, Queues, Heaps, Hash tables etc.
Write a C program to find sum of two matrices
#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++)
scanf("%d", &second[c][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
Operations performed
on one-dimensional
array
Searching
• It is the process of finding the location of the
specified element in a list.
• The specified element is often called the search key.
• If it is found search is successful, otherwise it is
unsuccessful.

• Ex: Binary Search, Linear Search, Sequential


Search.
Binary Search
In computer science, binary search is also known as half-interval
search/ logarithmic search. It is used to search for a key
element.
Steps for Binary Search:
Step1: Binary search locates the middle element of the array and
compares the search value by having low and high boundary
values.
low=0;
high=n-1;
and by finding mid as (low+high)/2;
Step2: If they are equal, it displays index of that element,
otherwise search is reduced to 0ne-half
Step3: If the search value is less than middle value of the array,
the first-half is searched; otherwise, the second half is
searched.
Step4: This continues until search value is equal to the middle
value of the array or until search value is not found.
#include <stdio.h>
#include<conio.h>
void main()
{
int n,i,a[10],key,low,high,mid,flag=0;
clrscr();
printf("\n Enter the no. of elements : ");
scanf("%d",&n);
printf("\n Enter %d elements in ascending order ",n);
for(i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
printf("\n Enter the key element to search : ");
scanf("%d",&key);
low = 0;
high = n-1;
while(low <= high)
{
mid=(low+high)/2;
if(key==a[mid])
{
flag=1;
break;
}
if(key > a[mid])
low=mid+1;
else
high = mid-1;
}
if(flag==1)
printf("\n Successful Search %d at position %d",key,mid+1);
else
printf("\n Unsuccessful Search");
getch();
}
output
Enter the no. of elements : 5
Enter 5 elements in ascending order 1 3 5 7 9
Enter the key element to search : 9
Successful Search 9 at position 5

Enter the no. of elements : 7


Enter 7 elements in ascending order 10 17 35 45 67 90 100
Enter the key element to search : 67
Successful Search 67 at position 5

Enter the no. of elements : 5


Enter 5 elements in ascending order 1 3 5 7 9
Enter the key element to search : 0
Unsuccessful Search
Linear Search
• It is a very simple search algorithm.
• Linear search in C to find whether a number is present in an
array. If it's present, then at what location it occurs. It is also
known as a sequential search
• Every item is checked and if a match is found then that
particular item is returned, otherwise the search continues till
the end of data collection.
Working of linear search
Program
#include <stdio.h>
void main()
{
int num;
int i, keynum, found = 0;
printf("Enter the number of elements ");
scanf("%d", &num);
int array[num];
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Enter the element to be searched ");
scanf("%d", &keynum);
Contd...
/* Linear search begins */
for (i = 0; i < num ; i++)
{
if (keynum == array[i] )
{
found = 1;
break;
}
}
if (found == 1)
printf("Element is present in the array at position %d",i+1);
else
printf("Element is not present in the array\n");
}
Output
Enter the number of elements 4
Enter the elements one by one
10 20 30 40
Enter the element to be searched 20
Element is present in the array at position 2

Enter the number of elements 4


Enter the elements one by one
10 20 30 40
Enter the element to be searched 50
Element is not present in the array
Difference between Binary Search and
Linear Search
Binary Search Linear Search
Input data to be sorted is required Not required

Requires an ordering comparison Requires equality comparisons

Requires random access to data Requires sequential access to data


C Programs
Arrays
Program to read and print marks of n students
void main()
{
int i,n;
printf("enter the number of students");
scanf("%d",&n);
int mark[n];
for(i=0;i<n;i++)
{
printf("enter %d marks",i+1);
scanf("%d",&mark[i]);
}
for(i=0;i<n;i++)
{
printf("\n marks of %d student is : %d",i+1,mark[i]);
}
}
Write a program to print smallest number and its position in an
array of size n
#include<stdio.h> small=a[0];
void main() for(i=1;i<n;i++)
{ {
int i,n,small,pos; if(a[i]<small)
printf("enter the size of array"); {
scanf("%d",&n); small=a[i];
int a[n]; pos=i;
printf("enter the elements"); }
for(i=0;i<n;i++) }
{ printf("the smallest element is %d \n
scanf("%d",&a[i]); its position is %d",small,pos);
} }
Program to add all the elements of 1D array
void main()
{
int n,sum=0;
printf("enter size of array");
scanf("%d",&n);
int a[n];
printf("enter array elements");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("\n sum is %d",sum);
}
Program to find if there are duplicates in the array
#include<stdio.h> for(i=0;i<n;i++)
void main() {
{ for(int j=i+1;j<n;j++)
int i,n,pos; {
printf("enter the size of array"); if(a[i]==a[j])
scanf("%d",&n); {
int a[n]; printf("\n Duplicate number
printf("enter the elements"); found at location %d and %d",i,j);
for(i=0;i<n;i++) }
{ }
scanf("%d",&a[i]); }
} }
Inserting an element at a specific position in an array
Expected Output

• pos, as this is now empty.


Algorithm
Step 1: Start
Step 2: Declare and initialize an array a[n] , Where n is number of elements in
the array
Step 3: Read the number to be inserted and position to be inserted into
Step 4: Set i=n-1
Step 5: While i >= pos
Set a[i+1]=a[i]
i=i-1;
Step 6 : Set a[pos] = num;
Step 7: Set n=n+1;
Step 8: Display the array
Step 9 :Stop
void main()
{
int pos,n,num,i;
printf("enter the size of array :");
scanf("%d",&n);
int a[n];
printf("enter the elements : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter number to be inserted : ");
scanf("%d",&num);
printf("enter position to insert : ");
scanf("%d",&pos);
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=num;
n++;
printf("\n The array after insertion is : ");
for(i=0;i<=n-1;i++)
printf(" %d \t",a[i]);
}
Program to delete an element in a specific position in an array
Algorithm
Step 1: Start
Step 2: Declare and initialize an array a[n] , Where n is number of elements
in the array
Step 3: Read position at which number should be deleted
Step 4: Set i=pos
Step 5: While i < n-1
Set a[i]=a[i+1]
i=i+1;
Step 6: Set n=n-1;
Step 7: Display the array
Step 8 :Stop
void main()
{
int pos,n,num,i;
printf("enter the size of array :");
scanf("%d",&n);
int a[n];
printf("enter the elements : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter position from which element should be deleted : ");
scanf("%d",&pos);
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n--;
printf("\n The array after deletion is : ");
for(i=0;i<=n-1;i++)
printf(" %d \t",a[i]);
}
Searching
• It is the process of finding the location of the specified
element in a list.
• The specified element is often called the search key.
• If it is found search is successful, otherwise it is
unsuccessful.

• Ex: Binary Search, Linear Search, Sequential


Search.
Program to demonstrate Binary search
Expected Output
Algorithm
Step 1: Start
Step 2: Declare and Initialise the array a[n] where n is the size of array. The array elements need to be
in ascending order
Step 3: Read the number that has to be searched – key
Step 4: Set low= 0 and high = n-1
Step 5: Compute mid = (low+high)/2
Step 6: while low <= high
if(array[mid] < key)
set low = mid + 1;
else if (array[mid] == key)
Print key found at location mid+1
Exit loop -go to step 9
else
Set high = mid - 1;
Step 7 : compute mid = (low + high)/2 and repeat step 6
Step 8: if(low > high)
Print element Not found
Step 9: Stop
void main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elements in the array");
scanf("%d",&n);
printf("Enter the array elements");
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to be searched ");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("\n %d found at location %d", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list", key);
}
Program to demonstrate Linear search
Expected Output
Algorithm
Step 1: Start
Step 2: Declare and Initialise an array a[n] where n is the size of array
Step 3: Read the number that has to be searched - key
Step 4: Set i=0
Step 5: while i < n
if a[i]= key
print that the element is found in position i
exit from loop – go to step 7
i=i+1;
Step 6: if end of array is reached
then print that the element is not found in the array
Step 7: Stop
void main()
{
int a[100], key, i, n;
printf("Enter number of elements in array: ");
scanf("%d", &n);
printf("\n Enter elements of the array: ");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("\n Enter a number to search: \n");
scanf("%d", &key);
for (i = 0; i < n; i++)
{
if (a[i] == key)
{
printf("%d is present at location %d \n", key, i+1);
break;
}
}
if (i == n)
printf("%d isn't present in the array\n",key);
}

You might also like