Assignment希望我们创建一个5x10的矩阵,该矩阵随机且非重复地包含英文字母。但是由于矩阵中有52个字母和50个房间,我不得不将它们放开。但是,如果我可以随机生成它们,我仍然会丢失两个字母,但不是相同的。
到目前为止,我的代码是这样的
#include <iostream>
#include <ctime> //for srand (number randomize)
using namespace std;
int main()
{
srand(time(0)); // generates random number
const int ROWS = 5; //declaration of rows
const int COLUMNS = 10; //declaration of columns
//writing content of the matrix
//I took out two letter (v and V) because matrix limit was 50 but all letters were 52
char harf[ROWS][COLUMNS] = {
{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'},
{'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'},
{'U', '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', 'w', 'x', 'y', 'z'}
};
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; ++j)
{
//generates a random index number
int index1 = rand() % 5; //random numbers 0 to 5
int index2 = rand() % 10; //random numbers 0 to 10
//swaps harf [i][j] with harf [index1][index2] for it won't be repating itself
char temp = harf[i][j];
harf[i][j] = harf[index1][index2];
harf[index1][index2] = temp;
}
}
//printing header and random order matrix
cout << "Random and nonrecurring matrix" << endl << endl;
for (int i = 0; i < ROWS; ++i)
{
for (int j = 0; j < COLUMNS; ++j)
cout << harf[i][j] << " ";
cout << endl << endl;
}
我尝试过这种方式;
char harf [ROWS] [COLUMNS];
for(int i = 0; i
{for(int j = 0; j
{
harf[i][j] = rand() % 25 + 65 && rand() % 25 + 97; } }
我也尝试过使用“ ||”进行相同的操作,但似乎不起作用。
由于这是一项作业,因此我无法使用比这种表达更高级的内容。有人可以告诉我如何将英语字母放入该矩阵吗?
答案 0 :(得分:0)
[abcde.....XYZ]
[dPYka....slqEr]
[dPYka...slq]
[dPYka...slq]
-> [dPYka...sl]
(读'q')如果采用这种方式,则删除的随机字母将纯粹是随机的(ha *),最后的5x10矩阵也将纯粹是随机的(ha *)。
*不要进入真正的随机数生成器的属性。