Given an array a[N] with value 1...N, find a repeted value

C

cai_rongxi

Anyone has the solution to the question "Given an array a[N] with value
1...N, find a repeted value" in C code?

Thanks a lot
 
X

xarax

Anyone has the solution to the question "Given an array a[N] with value
1...N, find a repeted value" in C code?

Thanks a lot

My solution is to ask your class teacher.
 
I

infobahn

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;
}
 
E

Emmanuel Delahaye

Anyone has the solution to the question "Given an array a[N] with value
1...N, find a repeted value" in C code?

No, because the valid indexes are from 0 to N-1.

That fixed, yes, there is a solution (maybe more). Just write it and
submit here for an expertise...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
P

prabhat143

I will give you a clue for an efficient solution. It's easy to write
O(n^2) solution and O(n) solution as well with O(n) extra space. But
solution can also be O(n) with O(1) extra space which uses the fact
that <b> possible values in an array of size N are 1..N </b>
 
P

Peter Shaggy Haywood

Groovy hepcat (e-mail address removed) was jivin' on 25 Dec 2004
08:29:55 -0800 in comp.lang.c.
Given an array a[N] with value 1...N, find a repeted value's a cool
scene! Dig it!
Anyone has the solution to the question "Given an array a[N] with value
1...N, find a repeted value" in C code?

Your instructor misspelled "repeated". The specification isn't even
clear. And he apparently failed to teach you how to do your own
homework. So, if I were you, I'd find a better school.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top