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 PostSince you never initialized the mem[] array, try displaying it just to see what's in it. It could be anything, including 'R'.
Jump to PostRandom Letter 5: R Random Letter 6: V Random Letter 3: M Random Letter 1: B mem[0]=R mem[1]=V mem[2]=M mem[3]=B
That's what mem[] contains
Before you use it :icon_rolleyes:
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.