简体   繁体   中英

I'm not sure about a few things on this problem.

Below is the problem. What I am not understanding is how I would use the rand function and not get any repeated values. I'm also not sure how I would compare the two in the transpose function that my professor is asking. Any explanation or template would be appreciated, thanks! This is in C++ by the way.

-Create a dynamic two dimensional square array of unsigned integers (array_one). Prompt the user to enter the number of rows (maximum of 50) (Use this for columns too since the array will be square.)

-Pass the array to a function that will initialize the two-dimensional array to random numbers between 0 and 4000 using the rand() library function. Here is the kicker: The array cannot have any repeated values!

-Create another dynamic two dimensional array of the same size (array_transpose)

-Pass both arrays to a function that will generate the transpose of array_one returning the values in array_transpose. The transpose swaps the rows and columns of an array. Suppose the square array is a 4 by 4 integer numbers.

  Transpose example: 

                               Array One                   Array One Transpose

                              1    2    3    4                   1   5   9   13

                              5    6    7    8                   2   6  10  14

                              9  10  11  12                   3   7  11  15

                            13  14  15  16                   4  8  12   16

Pass each array to a print_array function that will write to the screen the results of a test case with a 20 by 20 array.

One easy way to generate unique random numbers is to put each number you generate into an unordered_set . When you generate a number, check if it is in the unordered_set. If it is, generate a new one until you get one that isn't.

As far as the transpose goes, what exactly are you confused about? You simply need to take the randomly generated matrix and create a new matrix which is its transpose (ie, swap the column and row ordering).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM