-
Array sorting problems
I have two array x and y. I want to find out duplicate value store in array y
I wrote program and I can find duplicate value
Code:
#include<stdio.h>
int main(void) {
int i = 0, j = 0;
int x[5] = { 1, 2, 4, 3, 5 };
int y[5] = { 10, 3, 13, 2, 17 };
int temp[5];
for ( i = 0; i < 5; i ++)
{
for ( j = 0; j < 5; j ++)
{
if ( x[i] == y[j])
{
printf("Number %d found \n", x[i] );
}
}
}
return 0;
}
program output
Code:
Number 2 found
Number 3 found
I am trying to solve following problem where duplicate number 3 is repeating two times
Code:
int x[5] = { 1, 2, 4, 3, 5 };
int y[5] = { 10, 3, 3, 2, 17 };
I don't understand how to make a logic that find how many time number repeats in array ?
-
It often helps to write down what you want the output to be.
That will at least guide you do discover what information you have, and what information you need to compute.
Code:
1 appears 0 time(s).
2 appears 1 time(s).
...
You could then change your program to print the output with a question mark where the count should be, and that should hopefully indicate where you will need to make changes.
-
You should be able to create a counter inside your outer loop.