Question:
Extract distinct integers from an array and store them in another array.
Input:
10
1 2 3 4 5 6 1 2 3 9
First input is total no of inputs
Output:
Distinct Array:
1 2 3 4 5 6 9
Actual array:
1 2 3 4 5 6 1 2 3 9
Explanation:
While taking the input check if the number was in distinct array or not.
If the element was not in the distinct array add it at the end of the array increase the pointer to last element of the array.
If the element was found in the distinct array , jump out of loop and continue as usual without adding it into the disitnct array.
Code:
#include<stdio.h>
int main()
{
int n,temp,i;
scanf("%d",&n);
int dist[n],actual[n];
scanf("%d",&actual[0]);
dist[0]=actual[0];
int distcount=1,j;
for(i=1;i<n;i++)
{
int repeated=0;
scanf("%d",&temp);
actual[i]=temp;
for(j=0;j<distcount;j++)
{
if(temp==dist[j])
{
repeated=1;
break;
}
}
if(!repeated)
{
dist[distcount]=temp;
distcount++;
}
}
printf("Distinct Array:\n");
for(i=0;i<distcount;i++)
{
printf("%d ",dist[i]);
}
printf("\n");
printf("Actual array:\n");
for(i=0;i<n;i++)
{
printf("%d ",actual[i]);
}
return 0;
No comments:
Post a Comment