LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 12-08-2003, 09:27 PM   #1
techrolla
Member
 
Registered: Nov 2003
Distribution: Gentoo, Debian
Posts: 188

Rep: Reputation: 30
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.
 
Old 12-08-2003, 09:48 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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;
}
 
Old 12-08-2003, 09:58 PM   #3
techrolla
Member
 
Registered: Nov 2003
Distribution: Gentoo, Debian
Posts: 188

Original Poster
Rep: Reputation: 30
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
 
Old 12-09-2003, 08:24 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
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.
 
Old 12-09-2003, 08:41 PM   #5
techrolla
Member
 
Registered: Nov 2003
Distribution: Gentoo, Debian
Posts: 188

Original Poster
Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
keyboard repeat rate joshknape Mandriva 1 11-03-2005 07:46 PM
checking for null values from config file? zerointeger Programming 1 10-12-2005 11:29 AM
amp repeat? slinky2004 Linux - Software 0 09-29-2005 04:53 PM
Script to repeat commands satimis Programming 8 08-12-2005 04:03 AM
Repeat questions DaOne Slackware 13 06-24-2003 10:00 AM


All times are GMT -5. The time now is 06:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration