Hello Laserlight
How r u?thanks for your reply
Ok, so far I have managed to write the program as follows.
The code creates an array, checks if the element to be added is already present in the array list.If not present added to the array otherwise prints out "id present".However, when I call the display function it displays the array 30 times.But i want to display only the elements that is present in the array. Thats it.How do i proceed from here.Also please suggest is the code is alright?
Please help.Thank u
Code:
#include <stdio.h>
#define MAX 30
int find_dev(int a[],int data)
{
int i;
for (i=0; i<MAX; i++)
{
if (a[i] == data)
{
return(data);
}
}
return(-1);
}
int add_id(int a[],int data)
{
int i;
for(i=0;i<30;i++)
a[i] = data;
printf("ID added %d\n",a[i-1]);
}
int display(int a[])
{
int i;
for(i=0;a[i]!=0;i++)
printf("ID added %d\n",a[i]);
}
int main()
{
int array[30]={};
int i,ret;
int data;
while(1)
{
scanf("%d",&data);
ret=find_dev(array,data);
if(ret==-1)
{
add_id(array,data);
display(array);
}
else
{
printf("ID already exists");
}
}
return 0;
}