GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Apr-2005, 17:18
glulu76 glulu76 is offline
New Member
 
Join Date: Apr 2005
Posts: 22
glulu76 is on a distinguished road
Unhappy

Hi I am kind of new at programming and I need some help please.


I am working on a project trying to to get an array of 20 numbers. To be filled by the random generator with the range from 1 to 50 , with no duplicates and to be displayed with the first line the first number the second line the firtst and second number etc -----first through twenty. I have been working on for awhile but I just can't seem to get the numbers to come out to be 1 - 50 I end up with something like 0000000000000000204120

Then when I do get some numbers to come up I mess it up again trying to get it to diplay right. Please PLease Help. This is what I've done and it isn't giving me any numbers now.

CPP / C++ / C Code:
#include <iostream> 
#include <ctime> 
#include <cstdlib>


using namespace std;

void insert(int [], int);      // function prototype

int main()
{
  const int MAXNUM = 20;
  int newcode, i;
  int id[MAXNUM]={0} ;
	srand(time(NULL));
for (i=1; i<= id[MAXNUM]; i++)
	newcode= int(rand()/RAND_MAX * 50);

cout<<newcode;

insert (id,newcode);


return 0;
}

	void insert(int idcode[], int newcode)
{
  int i, newpos, trlpos;

  // find correct position to insert the new code
  i = 1;
  while (idcode[i] < newcode)
    i++;
  if (idcode[i] == newcode)
    cout << "\nThis identification code is already in the list";
  else
  {
    newpos = i;    // found the position for the new code

    // find the end of the list
    while (idcode[i] <51)
      i++;
    trlpos = i;

    // move idcodes over one position
    for (i = trlpos; i >= newpos; i++)
	
		idcode[i+1] = idcode[i];

    // insert the new code
    idcode[newpos] = newcode;
	 }

  return;
  }
Last edited by LuciWiz : 03-Apr-2005 at 23:37. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 03-Apr-2005, 19:16
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Pls use code tags while posting your code to preserve the indentation. Read this sticky first.
  #3  
Old 03-Apr-2005, 19:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,642
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by glulu76
I am working on a project trying to to get an array of 20 numbers. To be filled by the random generator with the range from 1 to 50 ,


Why don't you do things one at a time:

Task 1: Generate a random number between 1 and 50.

Now you have a good start, I think: use srand() to start the random number generate with something you got from time() so that every run will give dirrerent results.

Now, you have an expression that could be used to scale the random numbers, but there are a couple of problems:

rand() returns an int that is greater than or equal to zero and less than or equal to some system-defined large int named RAND_MAX.

Your expression had rand()/RAND_MAX. The result of this integer division is always zero. How do you get something else? Tell the compiler to convert one (or both) to a floating point number. The result of the floating point division will be a floating point number that is greater than or equal to zero and less than or equal to 1. If you want a number that is greater than or equal to zero and less than 1 (and I think that this is most useful) you can do it by:
CPP / C++ / C Code:
double x;
x = (double)rand()/(double)(RAND_MAX + 1);

Now if you want a number that is greater than or equal to 0.0 and less than 50.0, you could do this:
CPP / C++ / C Code:
double x;
x = (double)rand()/(double)(RAND_MAX + 1) * 50.0;
Then, if you want a number that is greater than or equal to 1.0 and less than 51.0 you could to this:

CPP / C++ / C Code:
double x;
x = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 50.0;

Finally, if you want an int that is greater than or equal to 1 and less than or equal to 50 you could simply have this (since converting a floating point number to an int is accomplished by truncating the fractional part):

CPP / C++ / C Code:
int choice;
choice = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 50.0;

Try this in a loop by itself and see if the numbers look like they are all between 1 and 50 (inclusive).

Now, step back and look at the real task. How would you do it if you had a bowl full of marbles with numbers on it. Every time you pull a ball, you write the number down in your list and put the ball back in the bowl. (This is like the random number generator, since the same number can occur again.)

Now how do you get your list with all different numbers?

You have a loop, and a counter that keeps track of how many numbers you have so far. (Initially the counter = 0.)

You remain in this loop until you have a total of 20 successful draws.

Inside the loop you have another loop:

Draw number. Check the entire list (entries from 0 to the current value of the counter). If this number is already in the list, then draw another number.
Keep doing this until you get a number that isn't already in the list. Put this number in the position indicated by the counter, and increment the counter.

After incrementing the counter, if its new value is less than 20, go back and do the inner loop again.

You now know how to draw a number between 1 and 50.

You need to know how to make a loop.

Regards,

Dave
  #4  
Old 03-Apr-2005, 22:40
glulu76 glulu76 is offline
New Member
 
Join Date: Apr 2005
Posts: 22
glulu76 is on a distinguished road

Thanks for the help with random number range Dave.


Thanks alolt for your help I did get the random generator to work in the range from 1 50. I took apart the program and started with just that and now I am trying to add the while loop into it for the unique numbers but they won't diplay. I am still working on it though. Thanks again.

I would have posted the code with tags like someone suggested I am just not sure where to go to do that. This is my first day on the site sorry.

CPP / C++ / C Code:
#include <iostream.h> 
#include <time.h> 
#include <stdlib.h>
int main()
{
const int MAXNUM=20;
int x, id,  newcode;

int counter = 0;


{while(counter= 0)
srand(time(NULL));
for (int i=1; i <= 1; i++)

x = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 50.0;
newcode = x ;
cout<< newcode<<endl;//If I leave this here the random numbers diplay 
                                   not uniquely so I tried to implement a while
                                   statemtent with very little success.
	
	if (newcode> x || newcode<x)
	{
		cout<< newcode<<endl;
		counter ++;
	}



}

return 0;

}
Last edited by LuciWiz : 03-Apr-2005 at 23:38. Reason: Please insert your C code between [c] & [/c] tags
  #5  
Old 04-Apr-2005, 08:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,642
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by glulu76
I would have posted the code with tags like someone suggested I am just not sure where to go to do that. This is my first day on the site sorry.

Code tags:

Before the first line of code put [ c ] (but don't put spaces between the bracket and the letter.

After the last line of code put [ /c ] (without the spaces).

Or, you could read the sticky at the top of the forum: GIDForums enables New [ C ] / [ C++ ] bbcode.

Now: look at this in your code:

CPP / C++ / C Code:
for (int i=1; i <= 1; i++)

x = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 50.0;
newcode = x ;

Point number 1. You must read up on how to make a for loop. This is a very good way to make a loop that executes only once. Is this what you had in mind? Why would you make a loop that executes once?

Point number 2. A for loop includes only the very next statement. So the only thing in the loop is the statement assigning a value to x. If you want the loop to include more than one statement, you put a block of statements in braces {}.

Point number 3. Where is the array of numbers where you will store the 20 distinct values that you are trying to get?

Point number 4. Have you really thought about how you would do this without a computer? I tried to give a few hints. I will try some pseudo-code, but not everybody gets much out of pseudo-code. The problem that I have is that I am kind of a bottom-up kind of guy, and I am already thinking in terms of actual code, so it's kind of hard to be abstract in creating a top-down description. However, you might consider something like this for your algorithm:

Pseudocode:

start with an array of int, say x[20].

Set a counter, say, numberValid, to 0.

Here's the iteration:

outer loop:

Start with numberValid = 0, and repeat the inner loop until numberValid = 20


inner loop:
1. Generate a random number between 1 and 50.

2. See if that number is in the array (look at x[0] up to, but not including x[numberValid]). If it is already in the array, then do number 1 and number 2 again. If it is not in the array, then set x[numberValid] to this number, and increment numberValid.


Regards,

Dave
  #6  
Old 04-Apr-2005, 15:55
glulu76 glulu76 is offline
New Member
 
Join Date: Apr 2005
Posts: 22
glulu76 is on a distinguished road

Ok this is what I have so far I am just not getting this.


CPP / C++ / C Code:
#include <iostream.h>
#include <time.h>
#include <stdlib.h>

//void insert(int [], int);      // function prototype

int main()
{
  const int MAXNUM = 20;

  int newcode, i;
  int id[MAXNUM];
  
 for(int count = 0; count<20; count ++)
 {
	
    cout<<id[MAXNUM]<<endl;
	

	for ( i=0; i <= id[MAXNUM]; i++)
		{	srand(time(NULL));
			newcode = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 50.0;
			cout<<newcode<<endl;
			
		}
			
	
		
	
}
 
 return 0;
}

We are to suppose to modify this program in our book to have a random number generator put the numbers in order with unique numbers and have them display starting with the first number adding one each time a number is added till end. I can get the random generator to work but not when I mess around with it trying to get unique numbers. It is driving me crazy because I have been working on it for days now.
  #7  
Old 04-Apr-2005, 18:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,642
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by glulu76
We are to suppose to modify this program in our book to have a random number generator put the numbers in order with unique numbers and have them display starting with the first number adding one each time a number is added till end. I can get the random generator to work but not when I mess around with it trying to get unique numbers. It is driving me crazy because I have been working on it for days now.

Well I have no idea of what exactly what the program in your book does, let alone how it does it and how you would go about 'modifying' it to do something else. I have tried to give you my reasoning about how I would approach the problem, and I guess I haven't been very helpful at getting you into some kind of mindset to make a program. It's tough starting out, when you haven't really got a handle on the language to see how to do something like this.

OK, here's a way to implement what I was trying to get at in my previous hints.

CPP / C++ / C Code:
  //Here's something new: you will have to make the function 

  bool AlreadyThere(int array[], int value, int index);

  int count;
  int id[MAXNUM];
  int newcode, i;
  bool LookingForNew;

  srand(time(NULL)); // Do this once at the beginning of the program
  
  for(count = 0; count < MAXNUM; count++) {
    LookingForNew = true;
    while(LookingForNew) {
      newcode = (int)(1.0 + (double)rand()/(double)RAND_MAX * 50.0);
      //cout << "newcode = " << newcode << endl;// print for debug purposes
      if(!AlreadyThere(id, newcode, count)) {
        id[count] = newcode;
        LookingForNew = false;
      }
    }
  }

This is actual working code, but you have to make a function that I call AlreadyThere(). I could have put the logic in the loop, but maybe having it as a separate function keeps from cluttering up the main loop logic.

So, what does it do:

There is a loop that is going to fill in MAXNUM (that's 20 for this example) numbers in the int array id[]. All of this is the same that you set up. The loop counter, count points to the next element of id[] that needs a number.

Now there is an inner loop that is going to repeatedly get numbers from rand() and see if the current number is already in the array. If it is already there, then the inner loop gets another number and checks again. Eventually there will be a number that is not already in the loop. So we put it there (that is store the new number in id[count] and continue with the outer loop --- the variable count is incremented in the outer loop, and the outer loop continues until we have all 20 required values.

What about the function AlreadyThere()?

Well it must search the array looking for the new number. It searches from 0 up to (but not including) the value of the index that we pass it (the position where we want to store the next value). The function AlreadyThere returns a boolean value "true" if the number is in the array, and "false" if it is not, so that the inner loop logic can know whether to store the new value or to get another number from rand() to try again.

Regards,

Dave
  #8  
Old 05-Apr-2005, 03:25
glulu76 glulu76 is offline
New Member
 
Join Date: Apr 2005
Posts: 22
glulu76 is on a distinguished road

I keep trying to do that function.


this is original code from book
CPP / C++ / C Code:
#include <iostream.h>

void insert(int [], int);      // function prototype

int main()
{
  const int MAXNUM = 100;

  int newcode, i;
  int id[MAXNUM] = {109, 122, 136, 144, 157, 162, 178, 185, 192, 9999};

  cout << "\nEnter the new identification code: ";
  cin  >> newcode;

  insert(id, newcode);

  cout << "\nThe updated list is:";
  i = 0;
  while(id[i] != 9999)
  {
    cout << " " << id[i];
    i++;
  }
  cout << endl;

  return 0;
}

void insert(int idcode[], int newcode)
{
  int i, newpos, trlpos;

  // find correct position to insert the new code
  i = 0;
  while (idcode[i] < newcode)
    i++;
  if (idcode[i] == newcode)
    cout << "\nThis identification code is already in the list";
  else
  {
    newpos = i;    // found the position for the new code

    // find the end of the list
    while (idcode[i] != 9999)
      i++;
    trlpos = i;

    // move idcodes over one position
    for (i = trlpos; i >= newpos; i--)
      idcode[i+1] = idcode[i];

    // insert the new code
    idcode[newpos] = newcode;
  }

  return;
}

this is code now
CPP / C++ / C Code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>

void insert(int [], int);      // function prototype


int main()
{

bool AlreadyThere(int array[],int value,int index);
  int count;
  const int MAXNUM = 20;
  int id[MAXNUM];
  int newcode;
  bool lookingForNew;
  
  

 srand(time(NULL));
  
 for(count = 0; count<MAXNUM; count ++)
 {
	lookingForNew= true;
	while(lookingForNew)
	{
		newcode = 1.0 + (double)rand()/(double)(RAND_MAX + 1) * 50.0;
	//cout<<newcode<<endl;
		if(AlreadyThere(id,newcode,count))
		
		{
		
			id[count]=newcode;			
			lookingForNew = false;

			printf(" %d ",id[count]);//print

		}
	}
	}

 insert(id,newcode);
  cout << endl;




  return 0;
}

void insert(int idcode[], int newcode)
{
  int i, newpos, trlpos;

  // find correct position to insert the new code
  i = 0;
  while (idcode[i] < newcode)
    i++;
  
    newpos = i;    // found the position for the new code

    // find the end of the list
    while (idcode[i] != 51)
      i++;
    trlpos = i;

    // move idcodes over one position
    for (i = trlpos; i >= newpos; i--)
      idcode[i+1] = idcode[i];

    // insert the new code
    idcode[newpos] = newcode;
  

  return;
}

I have tried to input function and I have tried to do it with an if statement to check for dups. I am just not sure of what to check against what. I also dont' understand why the bottom of the code isn't sorting anymore. This project has been harder for me than any other project. I don't know if because I had to start off with code to modify or if I am just not getting this project at all. I understand srand a little but trying to find if the number exists already in the array goes from what to what.
  #9  
Old 05-Apr-2005, 03:27
glulu76 glulu76 is offline
New Member
 
Join Date: Apr 2005
Posts: 22
glulu76 is on a distinguished road

Me again


I also wanted to thank you for all of your help. I have been getting so fustrated with this program that I forgot to write it earlier.
  #10  
Old 05-Apr-2005, 06:03
glulu76 glulu76 is offline
New Member
 
Join Date: Apr 2005
Posts: 22
glulu76 is on a distinguished road

Everytime I try to add the function I get error.


ogram 4 LG.obj : error LNK2001: unresolved external symbol "bool __cdecl AlreadyThere(int * const,int,int)" (?AlreadyThere@@YA_NQAHHH@Z)
Debug/Program 4 LG.exe : fatal error LNK1120: 1 unresolved externals

Errors like the ones I pasted above.

Sorry to keep bugging you.
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate 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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read/ Write EXCEL files using C/C++ programming confused_pig C++ Forum 4 25-Nov-2005 00:27
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 15:41
which language ? onauc C++ Forum 2 19-Nov-2004 02:53
GUI programming crystalattice C++ Forum 5 14-Sep-2004 12:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:15.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.