-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ujjwal Kumar
authored
Oct 2, 2020
1 parent
c78425a
commit f846de4
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/*This program shows the use of | ||
*very basic functions of Dynamic memory | ||
*allocation in C. | ||
*/ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
int linearsearch(int *a,int n,int key) | ||
{ | ||
int i=0; | ||
for(int i=0;i<n;i++) | ||
{ | ||
if(a[i]==key) | ||
return i; | ||
} | ||
return -1; | ||
} | ||
int main() | ||
{ | ||
int i,*a,n,key; | ||
printf("Enter the size of array\t"); | ||
scanf("%d",&n); | ||
a=(int*)malloc(n*sizeof(int)); | ||
//Similar function calloc can also be used | ||
for(i=0;i<n;i++) | ||
scanf("%d",a+i); | ||
printf("Enter the number to be searched\t"); | ||
scanf("%d",&key); | ||
if(linearsearch(a,n,key)==-1) | ||
printf("Number is not present in the array\n"); | ||
else printf("Element found at index %d\n",linearsearch(a,n,key)); | ||
} | ||
|