Random Number Generator with array

Pages: 12
Hi I do not understand the question stated below. Does anyone know what does it mean so I can kick start with it?

The program has two array of character. One of them has size 26 and is initialized with the alphabet a to z. A random number generator generates two numbers and these two numbers are used to index into this array of alphabets. The second array is used to hold the indexed alphabets. For example, the first row is the value held in the second array and the second row are the two generated numbers.

b g c z k e <- letter index by generate number
n1=1 n2=6 n1=2 n2=25 n1=10 … … … … n2=4 <- generated n1 & n2


Please see illustration below for clarification on the above question.

Element a b c ……… x y z
Index 0 1 2 ……… 23 24 25

Array2
Element a b c ……… x y z
Index 0 1 2 ……… 23 24 25

Final state after program execution
Array1
Element a b c ……… x y z
Index 0 1 2 ……… 23 24 25

Array2*
Element c e m ……… z f g
Index 0 1 2 ……… 23 24 25
Random No generated 2 4 12 ……… 25 5 6
* Actual char element allocated to each index location will depend on corresponding random number generated. Program completes execution after filling all 26 locations in Array2.
I would be inclined to help, but since you deleted your last question as soon as you got an answer, I don't see the point.
Sorry, I am new to the forum. I was told to change it to case solved but I didn't know he mentioned about the button instead. I could not retrieve back the question I had which I posted. I will try to post back the question. Would appreciate your help Disch.
I have posted back the question. :) Cheers.
hey can some1 post the solution here. I have tried but cant get it.
hey can some1 post the solution here. I have tried but cant get it.

No. This is not a free homework site. If there's something you're having difficulty with, post your code, and we'll try and help you understand where you're going wrong and how to fix it. But we won't just write it for you.
This is not my homework. I have seen similar qs in my past year ppr. Tried it but don really get the qs. I dono hw to start also. So im nt asking for ans. Mayb to use this solution n apply to my past yr qs.
Does anyone know what does it mean so I can kick start with it?


It's pretty self explanatory.

1) Make an array that is filled with characters 'a' through 'z' in order
2) Make another array that is filled with characters randomly

Tried it but don really get the qs. I dono hw to start also. So im nt asking for ans. Mayb to use this solution n apply to my past yr qs.


I dt udnstd wt u r syg
Last edited on
Array 1 holds two dimensional array with Element of a to z and Index of 0 to 25.
Array 2 is display out the ray with Element of random alphabets, Index of 0 to 25 and Random No generated which display the number in index holds the alphabets
in Array 1.

How to I hold a to z and 0 to 25 in an array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iomanip>
using namespace std;

const int NUMROWS = 1;
const int NUMCOLS = 26;
void display(int [NUMROWS][NUMCOLS]); // function prototype
int main()
{

int val[NUMROWS][NUMCOLS] = {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26}};
string words[NUMROWS][NUMCOLS] = {{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}};
 display(words);
 return 0;
}
void display(int nums[NUMROWS][NUMCOLS])
{
 int rowNum, colNum;
 for (rowNum = 0; rowNum < NUMROWS; rowNum++)
 {
 for(colNum = 0; colNum < NUMCOLS; colNum++)
 cout << setw(4) << nums[rowNum][colNum];
 cout << endl;
 }
 return;
}
I dont think this will work even if stored array is created. As the random generator is not programmed in. This programme will output 1-26.
Array 1 holds two dimensional array with Element of a to z and Index of 0 to 25.


No no. There are no 2 dimensional arrays. There are only 1 dimensional arrays.

The index is just the number you put inside the brackets when accessing an element

How to I hold a to z and 0 to 25 in an array?


You want an array of chars, and 'a' to 'z' should be put in single quotes to indicate they are characters and not variable names.

The 0-25 should not be stored inside the array, but are just numbers you can use to access individual characters in the array:

1
2
3
char alphabet[26] = {'a','b','c', ...etc };

cout << alphabet[2];  // <- 2 is our index, this will print 'c' 
int val[NUMROWS][NUMCOLS] = {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26}}; --->this line not necessary correct
Try this. could not display anything.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

int value[26] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
char alphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
cout << value[2] << alphabet[2];
 
 return 0;
}
#include<iostream>
#include<ctime>
#include <iomanip>
using namespace std;



const int NUM = 26;

int main()
{

double randval,randval2;

int i,j;

srand(time(NULL));
for (i,j=0; i,j<26 ; i,j++)
{
randval = 1 + rand() %26;
randval2 = 1 + rand() %26;
cout<<randval<<endl<< randval2 << endl;


char alpha[26] = {'a','b','c','d','e','f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't','u','v', 'w', 'x', 'y','z'};

//display(digits);

cin.ignore();
cin.ignore();
}



}
how to edit from here?
Hi garena,

Why would there be two random number.

It have to display

For Array 2,
26 random alphabets in the first row which is with array 1
0 1 2 3... to 25 in the 2nd row
26 random numbers in the 3 row which is with array 1


As for Array 1,
It will have 'a' which is stick to the number 0. 'b' to the number 1, 'c' to the number 2 and etc... this two will then be in display in Array 2 which is not in sequence,
Last edited on
@ evgeric:

int value[26] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};

This does nothing. You do not need/want an array that just contains numbers.

You will have 2 arrays here:

- One which has the alphabet ordered from 'a' to 'z'
- One which has randomized characters

Both will be char arrays.

The 0-25 thing seems to be tripping you up. This is just a natural effect of arrays. You can think of it as being automatic. [0] will access the first element in the array, and [25] will access the last element. For your ordered array, this means that [0] will automatically get you the 'a' character, and [25] will automatically get you the 'z' character. You do not need to create a second array for that -- that's just how arrays work.


@garena:

how to edit from here?


You're closer. You are correctly generating 2 random numbers, but you are adding 1 to them for some reason (don't do that, indexes are zero based, not 1 based).

You will need a second array, and you will need to randomly fill that array with characters from 'a' to 'z'. You do this by generating a random number from 0-25, and using that random number as an index to access the ordered array.

IE, your ordered array is alpha. So...
1
2
3
4
5
cout << alpha[3]; // <- prints 'd'
cout << alpha[0]; // <- prints 'a'

int foo = 2;
cout << alpha[foo]; // <- prints 'c' 


Notice how using a number as the index for the alpha array, you can get a different letter of the alphabet. If the index is random, then you can a random letter.
how do i create a 2nd array and randomly fill that array with char a-z? hw to use tht random number as index to access the ordered array? I am feeling confused.
Disch,

Sorry. I couldn't understand the second array portion.

with this
char alpha[26] = {'a','b','c','d','e','f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't','u','v', 'w', 'x', 'y','z'};

How do I generate that random number as an index to access the ordered array?

Is it like

Double randval = char [N];
Cout<< alpha[N];
Pages: 12