Write a c program to read n integers ranging from 1 to n that contain only one duplication and finding the repeated value and mixed in the array
Answers
Answered by
2
#include<stdio.h>int main(){ int arr[50]; int *p; int i,j,k,size,n; printf("\nEnter size of the array: "); scanf("%d",&n); printf("\nEnter %d elements into the array: ",n); for(i=0;i<n;i++) scanf("%d",&arr[i]); size=n; p=arr; for(i=0;i<size;i++){ for(j=0;j<size;j++){ if(i==j){ continue; } else if(*(p+i)==*(p+j)){ k=j; size--; while(k < size){ *(p+k)=*(p+k+1); k++; } j=0; } } } printf("\nThe array after removing duplicates is: "); for(i=0;i < size;i++){ printf(" %d",arr[i]); } return 0;}
kvnmurty:
some what inefficient program.. you do start comparing data from j = 0 onwards... you may do from j = i+1 onwards...
Answered by
0
#include <stdio.h>
main()
{
int A[100]; int i, j, n;
printf"size: ");
scanf("%d", &n);
printf("values : ");
for (i=0; i<n;i++) scanf("%d", &A[i] );
for (i = 0; i < n ; i++) {
for (j = i+1; j < n ;j++) {
if (A[i] == A[j]) printf("%d th data item = %d is repeated\n",i,A[i]);
for ( ; j<n-1; j++) A[j] = A[j+1] ; // remove the repeated one.
n --;
}
}
printf("the data items are : ");
for (i=0; i<n; i++) printf("%d ", A[i]);
}
main()
{
int A[100]; int i, j, n;
printf"size: ");
scanf("%d", &n);
printf("values : ");
for (i=0; i<n;i++) scanf("%d", &A[i] );
for (i = 0; i < n ; i++) {
for (j = i+1; j < n ;j++) {
if (A[i] == A[j]) printf("%d th data item = %d is repeated\n",i,A[i]);
for ( ; j<n-1; j++) A[j] = A[j+1] ; // remove the repeated one.
n --;
}
}
printf("the data items are : ");
for (i=0; i<n; i++) printf("%d ", A[i]);
}
Similar questions