C LAB‎ > ‎(Sem2) Data Structures‎ > ‎

Q8. WAP to find the last position of the redundant element.

posted Jan 7, 2011, 11:52 PM by Neil Mathew   [ updated Jan 9, 2011, 12:17 AM ]

SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>
 
int Lsearch(int *a, int n, int item)
{
 
int i,pos=-1;
 
for(i=0; i<n; i++)
if(a[i]==item)
pos=i; // last position overwrites the first position stored
 
return pos;
 
}
 
 
main()
{
int a[50],n,i,item;
 
printf("Enter size of your array: ");
scanf("%d",&n);
 
printf("Enter elements of your array: ");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
 
printf("Enter item to search: ");
scanf("%d",&item);
 
int pos=Lsearch(a,n,item);
 
if(pos==-1)
printf(" Element not found. ");
else
printf(" The position is at %d. ",(pos+1));
 
}
OUTPUT:

Enter size of your array: 6
Enter elements of your array: 7 3 5 2 5 9
Enter item to search: 5
 The position is at 5. 
Comments