random letters array


Each letter in the letter matrix will only be used once.


For example

J x U f B u F y D z
S g N n Y b H e X d
Q P A t V v T l E w
W i G c C r M h R j
Z m K q P k ı a O o


My code repeats letters.What can I do ?

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
27
28
29
#include <iostream>
 #include <iomanip>
 #include<cstdlib>
 using namespace std;
int main() {
	srand(time(NULL));

	
	char arrays[5][10] = {};

	char letters[52] =   {'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','a','b','c','d',
	                      'e','f','g','h','ı','j','k','l','m','n',
	                      'o','p','q','r','s','t','u','v','w','x','y','z'};

	for(int i=1;i<=5;i++){

		for (int j = 1; j <= 10; j++) {

			arrays[i][j] = letters[rand() % 52];

			cout << arrays[i][j];
		}
		cout << endl;
	}
	
	system("pause");
}
Last edited on
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
27
28
29
#include <iostream>
#include <algorithm>
#include <string>
#include <chrono>
#include <random>
using namespace std;

int main()
{
   const int ROWS = 5, COLS = 10;
   char arrays[ROWS][COLS];

   unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

   string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

   // Shuffle the alphabet and then take the first desired number of letters
   shuffle( alphabet.begin(), alphabet.end(), default_random_engine( seed ) );

   for ( int i = 0, n = 0; i < ROWS; i++ )
   {
      for ( int j = 0; j < COLS; j++ )
      {
         arrays[i][j] = alphabet[n++];
         cout << arrays[i][j] << " ";
      }
      cout << '\n';
   }
}


u C x N k e I H K p 
G E b g t c F V z D 
y o U X P s L n f Y 
l J T S d m r B W q 
w A h v j a i Z Q R 




If you had to do it with rand() then simply erase the characters from your selection string immediately after using them and adjust the modulus to the available size, rather than leaving it at 52.
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
27
28
#include <iostream>
#include <algorithm>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
   const int ROWS = 5, COLS = 10;
   char arrays[ROWS][COLS];

   srand( time( 0 ) );

   string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

   for ( int i = 0; i < ROWS; i++ )
   {
      for ( int j = 0; j < COLS; j++ )
      {
         int n = rand() % alphabet.size();
         arrays[i][j] = alphabet[n];
         alphabet.erase( n, 1 );
         cout << arrays[i][j] << " ";
      }
      cout << '\n';
   }
}
Last edited on
Thank you so much.I started learning new in c ++.The second solution is easier for me.I have one questions.how do we write the matrix again in alphabetical order.We have 50 letters in the matrix, but we have 52 letters in total.2 letters are empty


For example;

A B C D E F G H I J
K M N O P Q R S T U
V W X Y Z a b c d e
f g h i j k l m n o
p q r t u v w x y z
how do we write the matrix again in alphabetical order.

If you were going to do that it would probably be best to choose the two random letters that weren't going to be used first and erase them before starting the loops. Keep a copy of that so that you can write it out after.

I think you should try that.
Topic archived. No new replies allowed.