ca********@hotmail.com wrote:
Anyone has the solution to the question "Given an array a[N] with value
1...N, find a repeted value" in C code?
Since an array a[N] has offsets 0 through N - 1, I presume that by
"value 1...N" you are referring to the values stored in the array
elements.
The best way to get a solution to a simple problem like this is
to write it yourself.
Start like this:
#include <stdio.h>
int find_repeated_value(int *a, size_t n, int *repval)
{
int found = 0;
/* insert your code here */
if(found)
{
/* write the repeated value to *repval */
}
return found;
}
int main(void)
{
/* change this test array to include/not include repeated
* values, putting them in various places in the array
* on successive tests.
*/
int array[] = { 31, 41, 59, 26, 53, 58, 97, 93, 23, 84 };
int repeated = 0;
if(find_repeated_value(array,
sizeof array / sizeof array[0],
&repeated))
{
printf("At least one value was repeated: %d\n", repeated);
}
else
{
puts("All Values Unique!");
}
return 0;
}