Hi, i have this letters, A,R,V,G,M,N,B. And from those i need to get 4 of them randomly, bit i dont know how to do it, please help! :D
rsg31
6
Newbie Poster
Recommended Answers
Jump to Postfirst consider:
char letters[7] = {"ARVGMNB"}; for (i=0; i<7; i++) printf("letter # %d = %c\n", letters[i]);
then consider:
int number = rand() % 7; print ("random number from 0-6 is : %d\n", number);
now somehow you need put em together in your program.
…
Jump to Post
#include <time.h>
and you should already have stdlib because of rand(). At the top at some point before line 18 callsrand((unsigned)time(0));
to seed the random number generator using the current time (and converts that value to one acceptable to srand).A couple of little things: main() should …
Jump to PostIt always generates R as the first letter, or what is the situation?
Jump to Postchar letters[6]; int mem[3]; int i, j; letters[0]=65;//A letters[1]=66;//B letters[2]=71;//G letters[3]=77;//M letters[4]=78;//N letters[5]=82;//R letters[6]=86;//V
You are accessing the array boundary here. Change 6 to 7 in the array declaration.
Jump to PostOh yeah, didn't notice that.
char letters[6];
can only access [0] to [5] - 6 valuesint mem[3];
can only access [0] to [2] - 3 values
All 25 Replies
jephthah
1,888
Posting Maven
rsg31
6
Newbie Poster
jonsca
1,059
Quantitative Phrenologist
Team Colleague
Featured Poster
rsg31
6
Newbie Poster
rsg31
6
Newbie Poster
jonsca
1,059
Quantitative Phrenologist
Team Colleague
Featured Poster
rsg31
6
Newbie Poster
jonsca
1,059
Quantitative Phrenologist
Team Colleague
Featured Poster
rsg31
6
Newbie Poster
jonsca
1,059
Quantitative Phrenologist
Team Colleague
Featured Poster
rsg31
6
Newbie Poster
jonsca
1,059
Quantitative Phrenologist
Team Colleague
Featured Poster
rsg31
6
Newbie Poster
jonsca
1,059
Quantitative Phrenologist
Team Colleague
Featured Poster
WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague
rsg31
6
Newbie Poster
jephthah
1,888
Posting Maven
WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague
thomas_naveen
105
Junior Poster
jephthah
commented:
doh! look at how many "veteran posters" missed that! good catch :)
+6
jonsca
commented:
Yes, excellent job
+3
WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague
thomas_naveen
commented:
didn't notice mem[3] access.
+1
jonsca
commented:
Excellent job too, I guess ;)
+0
rsg31
6
Newbie Poster
jephthah
1,888
Posting Maven
rsg31
6
Newbie Poster
jephthah
commented:
sometimes you just gotta tell a "veteran poster" that he's wrong ;)
+6
jephthah
1,888
Posting Maven
thomas_naveen
105
Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.