Quantcast

Maximum PC

It is currently Wed Dec 24, 2014 8:41 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: passing random pointer elements in C++
PostPosted: Tue Apr 26, 2005 2:03 pm 
Little Foot
Little Foot

Joined: Sun Aug 08, 2004 5:30 am
Posts: 147
Location: Qo'nos
Does anyone know how to pass random elements from one pointer array to another?

Let's say that I have the following two pointer arrays:

Code:
const char *sample1[] = {"samplea", "sampleb", "samplec");

char *sample2[] = {""};


I want to select one of the elements in *sample1 - at random - and pass it to *sample2, but have no idea how to code something like that.

Any suggestions? Thank you!


Top
  Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 3:32 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
How many elements do you want to have? Two-dimensional arrays on the heap can get very very messy (there is no native support for two-dimesnional arrays on the heap, you have to hack it up yourself).

If you just want to have one item in there, then like this:
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

const unsigned int SIZE = 50;

int main()
{
    const char sample1[3][50] = {"samplea", "sampleb", "samplec"};
    char *sample2;
   
    srand(time(NULL));//seed random generator
   
    int elem = std::rand() % (sizeof(sample1)  /  SIZE);//num of elements

    sample2 = new char[SIZE];//allocate
    strcpy(sample2, sample1[elem]);

    std::cout << sample2 << std::endl;
    delete [] sample2;//deallocate
   
    std::cin.get();
}

This puts one of the c-strings in sample1 into sample2.

Or if you are using C++, just use std::strings.[/code]


Top
  Profile  
 
 Post subject: Re: passing random pointer elements in C++
PostPosted: Tue Apr 26, 2005 6:37 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Treklink256 wrote:
Does anyone know how to pass random elements from one pointer array to another?

Any suggestions? Thank you!

You need to learn how to break an assignment or project into smaller parts and implement each of those....

0) How do you create the arrays (which you did).
1) How do you pass things from one array to the other.
2) How do you select something at random.

... then combine them into the program.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Apr 27, 2005 9:16 am 
Little Foot
Little Foot

Joined: Sun Aug 08, 2004 5:30 am
Posts: 147
Location: Qo'nos
Well, now that I've calmed down a bit... :roll:

Kybo_Ren, I've been tinkering with that code you provided (thank you by the way!), and I've gotten it to work with my program - mostly...

Here the full program:

Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

const unsigned int SIZE = 50;

int main()
{

for (int counter=1; counter<20; counter++)
{
   const char article[5][50] = {"The", "A", "One", "Some" "Any"};
   const char noun[5][50] = {"boy", "girl", "dog", "town", "car"};
   const char verb[5][50] = {"drove", "jumped", "ran", "walked", "skipped"};
   const char preposition[5][50] = {"to", "from", "over", "under", "on"};
   const char articlex[5][50] = { "any" "the", "a", "one", "some",};
   const char nounx[5][50] = {"girl", "dog", "town", "car", "boy",};
   char *sentance;

    srand(time(NULL));//seed random generator
   
   int random_selector = std::rand() % (sizeof(article)  /  SIZE);//num of elements

   sentance = new char[SIZE];//allocate

   //displays article
   strcpy(sentance, article[random_selector]);
   cout<<sentance<<" ";
   
   //displays noun
   strcpy(sentance, noun[random_selector]);
   cout<<sentance<<" ";
   
   //displays verb
   strcpy(sentance, verb[random_selector]);
   cout<<sentance<<" ";

   //displays preposition
   strcpy(sentance, preposition[random_selector]);
   cout<<sentance<<" ";

   //displays second article
   strcpy(sentance, articlex[random_selector]);
   cout << sentance<<" ";
   
   //displays second noun
   strcpy(sentance, nounx[random_selector]);
   cout<<sentance<<"."<<endl;

   delete [] sentance;//deallocate
}
system("PAUSE");
return 0;
}


As you can see, the program basically just creates random sentances, like this:

Code:
A girl jumped from a dog.
Press any key to continue . . .


Sometimes, though, it seems to print two elements from any given array, one right after another, or it may skip elements from one of the arrays altogether, like this:

Code:
SomeAny town walked under some car.
Press any key to continue . . .


Or this:

Code:
car skipped on  boy.
Press any key to continue . . .


From looking at the code, I can't figure out why or how to fix it.

Another problem is that the program is supposed to print out 20 *different* random sentances. If the random function, like the one you provided, determines what sentance is created based on what time it is, then it just prints the same psuedo-random sentance 20 times. It doesn't generate 20 *different* psuedo-random sentances, which is what I want it to do.

If anyone has any more suggestions, that'd be fantastic, cuz' I just don't know how to code something like that. Thanks!


Top
  Profile  
 
 Post subject:
PostPosted: Thu Apr 28, 2005 8:04 am 
Little Foot
Little Foot

Joined: Sun Aug 08, 2004 5:30 am
Posts: 147
Location: Qo'nos
Hah! Never mind, I got it!

Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

const unsigned int SIZE = 50;

int main()
{
for (int counter=1; counter<21; counter++)
{
   const char article[5][50] = {"The", "A", "One", "Some", "Any"};
   const char noun[5][50] = {"boy", "girl", "dog", "town", "car"};
   const char verb[5][50] = {"drove", "jumped", "ran", "walked", "skipped"};
   const char preposition[5][50] = {"to", "from", "over", "under", "on"};
   const char articlex[5][50] = { "any", "the", "a", "one", "some",};
   const char nounx[5][50] = {"girl", "dog", "town", "car", "boy",};
   char *sentance;
   cout<<"Displaying sentance #"<<counter<<endl;

    srand(time(NULL));//seed random generator
   
   int random_selector = std::rand() % (sizeof(article)  /  SIZE);//num of elements

   sentance = new char[SIZE];//allocate

   //displays article
   strcpy(sentance, article[random_selector]);
   cout<<sentance<<" ";
   
   //displays noun
   strcpy(sentance, noun[random_selector]);
   cout<<sentance<<" ";
   
   //displays verb
   strcpy(sentance, verb[random_selector]);
   cout<<sentance<<" ";

   //displays preposition
   strcpy(sentance, preposition[random_selector]);
   cout<<sentance<<" ";

   //displays second article
   strcpy(sentance, articlex[random_selector]);
   cout << sentance<<" ";
   
   //displays second noun
   strcpy(sentance, nounx[random_selector]);
   cout<<sentance<<"."<<endl;
   system("PAUSE");
   system("CLS");

   delete [] sentance;//deallocate
}
system("PAUSE");
return 0;

It's not quite what I want, but thank you anyways!


Top
  Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 2:54 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Well, the srand() and const definitions should be outside of the for loop.

Also, use std::strings if you can.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.