Hello, I meet a problem with a programming question.I am wondering how to compar
ID: 3567916 • Letter: H
Question
Hello, I meet a problem with a programming question.I am wondering how to compare two numbers in an array and get the duplicates. Could anyone can help me? Thank you very much!
Question:Read in at leat 10 ?not more than 100) non-negative integers. Print them out. Then find the duplicates, and print out where they are. You should not print out the information about duplicates more than once. See the following example. Notice how the the number 10, which occurs at 0, 5, 13 is flagged: each duplicate occurrence is reported only once. It doesn't print again that "value 10 at position 5 also occurs at 13", because we already said that the value at position 5 is a duplicate of value at position 0. You can ignore the case when there are more than 100 inputs.
Out:
Enter next number, negative number to stop > 10
Enter next number, negative number to stop > 20
Enter next number, negative number to stop > 30
Enter next number, negative number to stop > 40
Enter next number, negative number to stop > 50
Enter next number, negative number to stop > 10
Enter next number, negative number to stop > 0
Enter next number, negative number to stop > 70
Enter next number, negative number to stop > 80
Enter next number, negative number to stop > 100
Enter next number, negative number to stop > 110
Enter next number, negative number to stop > 30
Enter next number, negative number to stop > 40
Enter next number, negative number to stop > 10
Enter next number, negative number to stop >-1
You entered 14 numbers
The numbers are 10 20 30 40 50 10 0 70 80 100 110 30 40 10
value 10 at position 0 is also at 5
value 10 at position 0 is also at 13
value 30 at position 2 is also at 11
value 40 at position 3 is also at 12
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include <malloc.h>
void duplicate(int arr[], int num)
{
int i, j;
for (i = 0; i < num ; i++)
{
if(arr[i] >= 0)
for (j = i + 1;j < num; j++)
{
int flag = 0;
if (arr[i] == arr[j])
{
arr[j] = -1;
printf("The value %d at position %d is also at %d ", arr[i], i , j);
}
}
}
}
int main()
{
int arr[100];
int input;
int size = 0, i;
while(true)
{
printf(" Enter next number, negative number to stop > ");
scanf("%d", &input);
if(input < 0)
break;
arr[size++] = input;
}
printf(" You entered %d numbers ", (size));
printf("The numbers are: ");
for(i=0; i<size; i++)
{
printf("%d ", arr[i]);
}
printf(" ");
duplicate(arr, size);
return 0;
}
---------------------------------------------
output
Enter next number, negative number to stop > 10
Enter next number, negative number to stop > 20
Enter next number, negative number to stop > 30
Enter next number, negative number to stop > 40
Enter next number, negative number to stop > 50
Enter next number, negative number to stop > 10
Enter next number, negative number to stop > 0
Enter next number, negative number to stop > 70
Enter next number, negative number to stop > 80
Enter next number, negative number to stop > 100
Enter next number, negative number to stop > 110
Enter next number, negative number to stop > 30
Enter next number, negative number to stop > 40
Enter next number, negative number to stop > 10
Enter next number, negative number to stop > -1
You entered 14 numbers
The numbers are: 10 20 30 40 50 10 0 70 80 100 110 30 40 10
The value 10 at position 0 is also at 5
The value 10 at position 0 is also at 13
The value 30 at position 2 is also at 11
The value 40 at position 3 is also at 12
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.