How to generate a random string from many strings, pls help? c programming

chewy91

Junior Member
Joined
Feb 1, 2012
Messages
9
Reaction score
0
i have 108 facts, how do i randomly read and show a string of fact when the person select a number?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
i have 108 facts, how do i randomly read and show a string of fact when the person select a number?

I believe your facts as you have mentioned is each a C string. First you mentioned each is randomly selected, next you mentioned a person will select a number.

So how is this "number" affecting the selection ? It is contradicting, isn't it ? Please clarify.

Should I interpret your terminology "random" as random file access ?
 

chewy91

Junior Member
Joined
Feb 1, 2012
Messages
9
Reaction score
0
for example, when the person selects number 5, the 5th string out of the 108 string will show, but that should be something like a search function right? i dont know if is easier to do this way or just allow a random string to show when the the person selects any number. All the strings are stored in a txt file.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
for example, when the person selects number 5, the 5th string out of the 108 string will show, but that should be something like a search function right? i dont know if is easier to do this way or just allow a random string to show when the the person selects any number. All the strings are stored in a txt file.

I will believe each of your facts are delimited by a set of character(s). The most common for a simple text file is obviously a combination of either "\r\n", or "\r", or "\n".

You can choose to read(or slurp) all into an array, and then select based on the chosen number.
If your exercise requires you to exercise random file access nature, then you will need to find out EXACTLY where is the start of each fact(a c string).

You can either parse the file from the start till to the end for the first time, index the offset of the occurrence of the character right after the "\r\n" or "\r" or "\n". Assuming there is always a fact starting right after them except the last fact. Store the index and use it for random access when there is a chosen number.

Another naive approach every time skim thru the whole text and find the right string of each string, but this will ne sequential access, not exercising random file access.

If you really want a good way to exercise smart approach, it is how you store your text file. I would have store with a index at the top of each file as such

Code:
0:104:200:.....:1023
This is fact one
This is fact two
...
This is fact N

The first line is the offset of each C string calculated before you wrote the text file.

Another interesting way is the following

Code:
18:This is fact one
18:This is fact two
...
16:This is fact N

The beginning of each fact(C string) is the length of the following fact. You can go figure out how to traverse. :)
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
I need to tell you storing an index at the beginning of the file, the construction will need some good thoughts. I will let you exercise some thoughts on why it is not as easy as I have mentioned :)

Think about it and you can tell me about it here :)
 

chewy91

Junior Member
Joined
Feb 1, 2012
Messages
9
Reaction score
0
sorry, i understand the code that u wrote, u mean i need to write in that format on my txt file?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
sorry, i understand the code that u wrote, u mean i need to write in that format on my txt file?

It depends on how you want. Is there a need for you to random access the file, or all you need is produce the fact based on the input ? I believe what you need seems to be a practical assignment of your course right ? Is it mandatory for you to demonstrate random access in this exercise ? Is the file format already explicitly determined in the assignment ?
 

chewy91

Junior Member
Joined
Feb 1, 2012
Messages
9
Reaction score
0
It depends on how you want. Is there a need for you to random access the file, or all you need is produce the fact based on the input ? I believe what you need seems to be a practical assignment of your course right ? Is it mandatory for you to demonstrate random access in this exercise ? Is the file format already explicitly determined in the assignment ?

It is not neccessary to use the random access. Producing the fact based on input seems to be easier. Is there a need to change my file format or my data? My data only contains sentences.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
It is not neccessary to use the random access. Producing the fact based on input seems to be easier. Is there a need to change my file format or my data? My data only contains sentences.

In that case, just read each line into an array of char pointers. Remember to allocate space from the heap for storing into the array.

Surely u t taught how to do this right?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Do i need to use malloc?

Yes u will need to. After u read each fact into a string, u will need to Malloc sufficient memory to copy the new string over. Remember to free the memory at the end of the program. Good practice, but not essential for ur case since the OS will reclaim all memory when a process is destroyed
 

chewy91

Junior Member
Joined
Feb 1, 2012
Messages
9
Reaction score
0
Yes u will need to. After u read each fact into a string, u will need to Malloc sufficient memory to copy the new string over. Remember to free the memory at the end of the program. Good practice, but not essential for ur case since the OS will reclaim all memory when a process is destroyed

ok, then do i need to use struct function?
 

chewy91

Junior Member
Joined
Feb 1, 2012
Messages
9
Reaction score
0
Tell me why do u want to use struct? I am not saying no need, I am asking u why, since u suggested


i dont know why too, im actually confused and im still a beginner.sigh. Can i dont use malloc if i know the amount of space needed?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
i dont know why too, im actually confused and im still a beginner.sigh. Can i dont use malloc if i know the amount of space needed?

Okay, struct is not necessary for this exercise. All you need is an array of strings. If you have been thought how to dynamically allocate memory from the heap, you should use it. No C programmer comes complete with the knowledge of how to handle dynamic memory.

However, you can also solve this assignment with just a 2 dimensional array of characters.
 

hawthorne

Arch-Supremacy Member
Joined
May 9, 2006
Messages
11,714
Reaction score
210
Easiest create a char* array with 108 elements. Then use file i/o open with text mode to populate the char* array

The user choose a number and u just directly access the array by index. I think less than 20 lines of code only
 
Last edited:

Nerrazzuri

Junior Member
Joined
Oct 15, 2012
Messages
9
Reaction score
0
You may store them in a STL container, specifically map/multimap. There you can assign the value(numbers) to retrieve key(string) from the container itself.

example:

Code:
std::map<string, int> mFacts;

//you can either use a txt file to store the facts or hardcoded in your program, and set the facts and the numbers into map.

//use a loop instead to loop all the facts into std::map
mFacts.insert(std::pair<string, int>("first fact", 1));
mFacts.insert(std::pair<string, int>("2nd fact", 2));

//Then when you want to retrieve the facts base on numbers
int i;
cout << "Please insert your number: ";
cin >> i;
cout << "The fact is: " << mFacts[i];

Above are some codes written by raw hand, no compiler, so it might have some syntax mistakes. Hope you get the concept.

PS: Multimap works in the same way, just change map to multimap since map doesn't allow multiple key to be added to it. So if you have more than 1 same string/int/char or whatsoever, use multimap.
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
You may store them in a STL container, specifically map/multimap. There you can assign the value(numbers) to retrieve key(string) from the container itself.

example:

Code:
...

Above are some codes written by raw hand, no compiler, so it might have some syntax mistakes. Hope you get the concept.

PS: Multimap works in the same way, just change map to multimap since map doesn't allow multiple key to be added to it. So if you have more than 1 same string/int/char or whatsoever, use multimap.

It is C...
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top