Reputation: 1
The problem is to take numbers as input to an array and to display the number of duplicates in it.
I came across this problem while practicing to improve my coding skills.
#include<stdio.h>
int main()
{
int i=0,j=1,m,n,arry[100],count=0;
printf("Enter the number of elements in an array\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(m=0;m<n;n++)
{
scanf("%d",&arry[m]);
}
while(i!=n-1)
{
for(;j<n;j++)
{
if(arry[i]==arry[j])
count++;
}
i++;
}
printf("The number of duplicates in the array are %d",count);
}
This seems like a simple problem and I think the algorithm is correct but I'm not getting the required output. I executed the code on Dev C++ on which it crashes after entering array elements. And I tried a few online compilers too. They sometimes display 'segmentation fault' while entering elements. I think this is related to array index.
PS: I'm using this platform for the first time and I have no clue about the difficulty of the problems discussed here. If you find this too easy and silly, don't laugh, Help me out!!!
Upvotes: 0
Views: 137
Reputation: 155
You have made a lot of silly mistakes in the program like for(m=0;m<n;n++)
. it should be for(m=0;m<n;m++)
. there are other mistakes too. You defined the array size to be 100 and then only fill the desired number of integers thus wasting the remaining space or what if the user wishes to enter 1000 integers in the array? so what you should do is give the array a user defined memory.
this is the finished code:-
#include<stdio.h>
int main()
{
int i=0,n,count=0;
printf("Enter the number of elements in an array\n");
scanf("%d",&n);
int arry[n];
printf("Enter the array elements\n");
for(int m=0;m<n;m++)
{
scanf("%d",&arry[m]);
}
while(i!=n-1)
{
for(int j=0;j<n;j++)
{
if(arry[i]==arry[j])
count++;
}
i++;
}
printf("The number of duplicates in the array are %d",count);
return 0;
}
i only corrected your mistakes but your program still has logical error. your logic of finding the duplicate count is totally wrong. the way you are doing it, it will just count all the duplicates n*n times.
if you want the duplicate count of every number separately then what you can do is make a separate array containing the unique numbers. or if you just want the over all duplicate count, you will need to make a function to weed out all the numbers that have already been counted once.
#include<stdio.h>
int main() {
int n;
printf("Enter The Size Of Array: ");
scanf("%d",&n);
int a[n],b[n];
int i;
int p=0;
printf("Enter The Elements Of The Array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
int j;
for(j=0;j<i;j++)
{
if(a[i]==a[j])
{
break;
}
}
if(j==i)
{
b[p]=a[i];
p++;
}
}
printf("\nUnique Numbers Are: ");
for(i=0;i<p;i++)
{
printf("%d ",b[i]);
}
printf("\n");
return 0;
}
above given code has the logic to separate out the unique numbers in an array.
This is the complete code for finding duplicates in an array:-
#include<stdio.h>
int main() {
int n;
printf("Enter The Size Of Array: ");
scanf("%d",&n);
int a[n],b[n],c[n];
int p=0;
printf("Enter The Elements Of The Array:\n");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++)
{
int j;
for(j=0;j<i;j++)
{
if(a[i]==a[j])
{
break;
}
}
if(j==i)
{
b[p]=a[i];
p++;
}
}
for(int i=0;i<p;i++)
{
int count=0;
for(int j=0;j<n;j++)
{
if(a[j]==b[i])
{
count++;
}
}
c[i]=count;
}
for(int i=0;i<p;i++)
{
printf("The Unique Number %d Was Repeated %d Times In The Array.\n",b[i],c[i]);
}
printf("\n");
return 0; }
Upvotes: 1