Avoid printig same letters in matrix

Hey guys I'm trying to print a matrix with random letters wiht non to repeat.I can print the random matrix but ı can't avoid letters to repeat. Can anyone help me ? Thanks for your helps.

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
30
31
32
33
34
#include "iostream"
#include "time.h"

using namespace std;

int main() {
	
	srand(time(NULL));
	
	int a;
	int i, j;
	char  ch_c;

	const int row = 5;
	const int col = 10;

	char matrix[row][col];

	for ( i = 0; i < row; i++)
	{
		for ( j = 0; j < col; j++)
		{
			a = rand() % 26;
			if (j % 2 == 1) {
				ch_c = 'a' + a;	
			}
			
			else {
				ch_c = 'A' + a;	
			}
			matrix[i][j] = { ch_c };

		}
	}
You can try maybe to first add all diifrent letters to matrix by yourself,and then to print matrix where elements were sorted randomly.If you can't sort the matrix you can get them in array and then sort array randomly.
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
30
31
32
33
34
35
36
37
#include <chrono>
#include <iostream>
#include <random>

int main()
{
    std::mt19937 eng {
        static_cast<unsigned>(
            std::chrono::high_resolution_clock::now().time_since_epoch().count()
        )
    };
    char letters[] { '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', 'i', 'j', 'k', 'l', 'm', 'n',
                     'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
                     'y', 'z' };
    constexpr int ROW { 5 };
    constexpr int COL { 5 };
    char matrix[ROW][COL];
    for(int i {}; i < ROW; ++i) {
        for(int j {}; j < COL; ++j) {
            std::uniform_int_distribution<> dst(1, 52);
            int index { dst(eng) };
            for(/**/; letters[index] == '\0'; index = dst(eng)) {}
            matrix[i][j] = letters[index];
            letters[index] = '\0';
        }
    }
    for(int i {}; i < ROW; ++i) {
        for(int j {}; j < COL; ++j) {
            std::cout << matrix[i][j];
        }
        std::cout << '\n';
    }
    std::cout << '\n';
}


Example output:
sgXUm
cnDLw
VIiyJ
EGYOC
dlPBx


Thanks for your reply but the code you wrote is too complicate for me.Is there any way that using for an if too avoid repeating ?
too complicate

Sorry for that, killingforlive; I meant to help, not to make your life harder - even if your nickname creeps me out a little bit :-)

Although the more I read my code, the more I think it’s very close to yours, if you tell me what’s the complicated part I’ll try to enhance it.

Happy coding!
Here's a simplified version of Enoizat's code:
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
30
31
32
33
34
35
#include <chrono>
#include <iostream>
#include <ctime>

int main()
{ char letters[]{ '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', 'i', 'j', 'k', 'l', 'm', 'n',
                   'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
                   'y', 'z' };
  
  const int ROW = 5;
  const int COL = 5;
  char matrix[ROW][COL];

  srand((unsigned)time(NULL));

  for (int i{}; i < ROW; ++i) {
    for (int j{}; j < COL; ++j) {
      int index = rand() % 52;
      for (/**/; letters[index] == '\0'; index = rand() % 52) 
      {}
      matrix[i][j] = letters[index];
      letters[index] = '\0';
    }
  }
  for (int i{}; i < ROW; ++i) {
    for (int j{}; j < COL; ++j) {
      std::cout << matrix[i][j];
    }
    std::cout << '\n';
  }
  std::cout << '\n';
}
Thanks a lot, Thomas1965, that’s great!
May I ask a very naive question? Why does it work even without <cstdlib>?
Probably because it got included by some other header file.
I missed it, so better add the include <cstdlib>
Yes like Thomas said, headerfiles can include other headerfiles. In this case iostream is including cstdlib because it's dependent on it. So if you include iostream you can use cstdlib functions.

But you should not rely on that. You should include the headerfile anyway.
Thank you, Thomas1965.
Thanks for your replies.They are very usefull for me.But there is a one last thing ı can't resolve.
The letters must be one capital one lowercase letter(if column % 2 == 0 needs to be capital).I tried on both of the codes but ı can't do it.Can you solve it please ?

Example:

A e V h C w R i
O p G z X b M l
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 <chrono>
#include <random>
using namespace std;

int main()
{
   const int ROWS = 2, COLS = 8;
   char arrays[ROWS][COLS];

   unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
   auto rng = default_random_engine( seed );
   string alphabet = "abcdefghijklmnopqrstuvwxyz";
   string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   shuffle( alphabet.begin(), alphabet.end(), rng );
   shuffle( ALPHABET.begin(), ALPHABET.end(), rng );

   char *p = &arrays[0][0];
   for ( int n = 0; n < ROWS * COLS; n++, p++ ) *p = n % 2 ? alphabet[n/2] : ALPHABET[n/2];

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


Q v C d Z b S g 
L z B p O h A o 
Topic archived. No new replies allowed.