Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-08-2003, 09:27 PM
|
#1
|
|
Member
Registered: Nov 2003
Distribution: Gentoo, Debian
Posts: 188
Rep:
|
Checking for repeat values
Im wondering how, using for loops, to generate a small array [5] of integers in c++ by going through the array and generating random numbers but making sure that they arn't already in the array. Im looking for a simple, no pointer, for, solution, any suggestions would be greatly appreciated, thanks.
|
|
|
|
12-08-2003, 09:48 PM
|
#2
|
|
Senior Member
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357
Rep:
|
Code:
// Warning: never tested, just writen in the fly.
int inTheArray(int a[],int value,int len) {
for(int i=0;i<len;i++) if(a[i]==value) return 1;
return 0;
}
int foo[5];
for(int i=0;i<5;i++) {
int number;
do{
number=random();
} while(inTheArray(foo,number,i));
foo[i]=number;
}
|
|
|
|
12-08-2003, 09:58 PM
|
#3
|
|
Member
Registered: Nov 2003
Distribution: Gentoo, Debian
Posts: 188
Original Poster
Rep:
|
Thank you for replying, I had though of that before, but what I need is a purely for looping check without functions, I should have stated that. Thanks for taking the time though  Something like:
Code:
for(i=0; i < 5; i++)
{
for(k=2; k < i; k++)
{
if(values[k] == values[k-1])
values[k] = round(10*rand()); //dont mind if these functions are incorrect right now, I can fix that later
}
}
The only problem is that there are repeated values still. Thanks
|
|
|
|
12-09-2003, 08:24 AM
|
#4
|
|
Member
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895
Rep:
|
So you are saying you can't figure out how to take ToniT's code and modify it so that it doesn't use a function? You could put the code he has in his function within the for loop like so:
Code:
// Warning: never tested, just writen in the fly.
int foo[5];
for(int i=0;i<5;i++) {
int number;
int inTheArray;
do {
number=random();
inTheArray = 0;
for(int j=0;j<i;j++) {
if (foo[j]==number) {
inTheArray = 1;
break;
}
}
} while(inTheArray);
foo[i]=number;
}
Last edited by deiussum; 12-09-2003 at 08:38 AM.
|
|
|
|
12-09-2003, 08:41 PM
|
#5
|
|
Member
Registered: Nov 2003
Distribution: Gentoo, Debian
Posts: 188
Original Poster
Rep:
|
Thanks for the help and replies, it wasn't that I didn't know how to change it, I was actually looking for something a little different, like a very primitive array search, probably slower than these examples, but it works. Actually, its very similar to the other ones, but a little different. I figured it out, its kinda like a bubble search. sorry this one is in pascal.
Code:
for i:=0 to 5 do
begin
num := random(15);
for k:=0 to i-1 do
begin
lotNumbers[i] := num;
if lotNumbers[i] = lotNumbers[k] then
begin
num := random(15);
k := 0;
i:=i-1;
end;
end;
end;
C++: This doesnt seem to work, but its the basic Idea:
EDIT: It actually does work, I had a wierd print out: '
Code:
#include <iostream.h>
for(i=0; i < 5; i++)
{
num = rand();
if(i > 0)
for(k=0; k < i-1; k++)
{
vs[i] = num;
if(vs[i] == vs[k])
{
num = rand();
k = 0;
i = i-1;
}
}
else
vs[i] = rand();
}
Last edited by techrolla; 12-09-2003 at 11:25 PM.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:10 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|