Docsity

data structure programs, Study notes of C programming

Toggle sidebar
bg1
EXPERIMENT
NO…… DATE………………
…..
Aim: Write a program to find the occurrence of an element in the
array.
Code:
#include<stdio.h>
void main()
{
int a[10],n,i,p,count=0; printf
("\nEnter the size of the array ");
scanf("%d",&n);
printf("\nEnter the elements into the array
"); for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the element to find ");
scanf("%d",&p);
for(i=0;i<n;i++)
{
if(p==a[i])
count++;
}
printf("\nOccurence = %d\n",count); }
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download data structure programs and more Study notes C programming in PDF only on Docsity!

EXPERIMENT NO…… DATE ……………… …..

Aim : Write a program to find the occurrence of an element in the array. Code :

#include<stdio.h> void main() { int a[10],n,i,p,count=0; printf ("\nEnter the size of the array "); scanf("%d",&n); printf("\nEnter the elements into the array "); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\nEnter the element to find "); scanf("%d",&p); for(i=0;i<n;i++) { if(p==a[i]) count++; } printf("\nOccurence = %d\n",count); }

e1 | P a g

e2 | P a g

EXPERIMENT NO…… DATE …………… ……..

Aim : Write a program to find the sum of the numbers at even positions in the array. Code : #include<stdio.h> void main() { int a[10],n,i,sum=0; printf("\nEnter the size of the array "); scanf("%d",&n); printf("\nEnter the elements into the array "); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i=i+2) { sum+=a[i]; } printf("\nSum of elements at even position = %d\n",sum); }

Output :

e4 | P a g

5 | P a g e

Output :

EXPERIMENT NO…… DATE …………… ……..

Aim : Write a program to insert a element into the array. Code : #include<stdio.h> void main() { int a[30], i, num, pos, el; printf("\nEnter no of elements :"); scanf("%d", &num); printf("\nEnter the elements"); //Read n elements in an array for (i = 0; i < num; i++) scanf("%d", &a[i]); printf("\nEnter element for insertion "); scanf("%d",&el); printf("\nEnter position for insertion "); scanf("%d",&pos); for(i=num;i>=pos;i--) { a[i+1]=a[i]; } a[pos]=el; num=num+1; printf("\nThe array is ");

7 | P a g e

8 | P a g e

EXPERIMENT NO…… DATE …………… ……..

Aim : Write a program to delete an element from the array. Code : #include<stdio.h> void main() { int a[30], i, num, pos; printf("\nEnter no of elements :"); scanf("%d", &num); printf("\nEnter the elements"); //Read n elements in an array for (i = 0; i < num; i++) scanf("%d", &a[i]); printf("\nEnter position for deletion "); scanf("%d",&pos); for(i=pos;i<num;i++) { a[i]=a[i+1]; } num=num-1; printf("\nThe array is "); for (i = 0; i < num; i++) printf("%d ", a[i]);

Output :

10 | P a g e