473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

Nov 14 '05 #1
5 1674
<ca********@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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.
Nov 14 '05 #2
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;
}
Nov 14 '05 #3
ca********@hotmail.com wrote on 25/12/04 :
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.

Nov 14 '05 #4
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>

Nov 14 '05 #5
Groovy hepcat ca********@hotmail.com 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"?
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
13
by: mike | last post by:
I have ListArray with number in Eg: 1, 1.456, 2.43, 4, 6.78 next i have a decimal variable containing one number EG: 1.786 Could someone please tell me how i find the "closest match" number...
4
by: Arjen | last post by:
Hi, I need to add this inside an array: 1 3 2 4 3 2 4 5 5 1 I think of using this:
1
by: yawnmoth | last post by:
Given an element ID, is there a way to figure out what index one would need to use in the parentNode's childNodes array to get at that element? For example... <body> <div id="parent"> <div...
2
by: Ros | last post by:
Peeps, I need help with trying to find a value in array of arrays. Public Module myModule Private Site_Access() As Array Property Set_SA() Get Return Site_Access End Get
5
by: Manish | last post by:
This is the print_r() for a variable $categories. $categories :: Array ( =Array ( =Array (
12
by: begum | last post by:
in this program they want me to find the max value of a subsequence in a given sequnce of numbers(the sequence is represented by an array) by using templates and pointers.also i must write a main...
1
by: msggeca | last post by:
I want the source code in c++ of a program which finds factorial of any no between 1yo 365 and also displays how many times each digit is repeted in it eg 4 24 0=0 1=0 2=1 3=0 4=1 5=0
4
by: raylopez99 | last post by:
I would like to know if there's a quick "Linq" way to find the index of an array having a particular value. I can do this the long way by sequential iteration, but would like to know if there's a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.