Advertisement
Guest User

problem2

a guest
Nov 20th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1.  
  2. //
  3. // THOMAS MICHAEL
  4. //
  5. //
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <iomanip>
  10. #include <string>
  11. #include <vector>
  12.  
  13. using namespace std;
  14.  
  15. void init_array(unsigned int** array, unsigned int rows, unsigned int cols);
  16. void print_array(unsigned int** array, unsigned int rows, unsigned int cols);
  17. void array_transpose(unsigned int** array_one, unsigned int** array_transpose, unsigned int rows, unsigned int cols);
  18.  
  19. int main(void)
  20. {
  21.     //Variable for rows and columns of array
  22.     int rows, cols;
  23.  
  24.     //Loops through until valid input for the number of rows in the array
  25.     for (;;) {
  26.         cout << "Enter the number of rows for the array. Max 50." << endl;
  27.         if (cin >> rows && rows <= 50 && rows > 0) {
  28.             break;
  29.         }
  30.         else {
  31.             cout << "Please enter a valid integer" << endl;
  32.         }
  33.     }
  34.  
  35.     cout << endl << endl;
  36.    
  37.     //sets cols = rows since the array is square
  38.     cols = rows;
  39.  
  40.     //init pointer and 2D array of pointers for the original array
  41.     unsigned int** array_one;
  42.     array_one = new unsigned int*[rows];
  43.     for (unsigned int k = 0; k < rows; k++)
  44.         array_one[k] = new unsigned int[cols];
  45.  
  46.     //init pointer and 2D array of pointers for the transpose array
  47.     unsigned int** array_two;
  48.     array_two = new unsigned int*[rows];
  49.     for (unsigned int i = 0; i < rows; i++) {
  50.         array_two[i] = new unsigned int[cols];
  51.     }
  52.    
  53.     //init original array with given values and then gets the transpose of the array
  54.     init_array(array_one, rows, cols);
  55.     array_transpose(array_one, array_two, rows, cols);
  56.    
  57.     //Calculates the width for set input to center the text depending on the size of the array
  58.     unsigned int width = (cols * 7) / 2;
  59.  
  60.     //Prints out the original array
  61.     cout << setw(width) << "Array One" << endl << endl;
  62.     print_array(array_one, rows, cols);
  63.    
  64.     //Prints out the transpose of the original array
  65.     cout << endl << endl;
  66.     cout << setw(width + 3) << "Array Transpose" << endl << endl;
  67.     print_array(array_two, rows, cols);
  68.    
  69.  
  70.     cout << endl << endl;
  71.     system("pause");
  72.  
  73.     return 0;
  74. }
  75.  
  76. //Function that init an array with nonrepeating random values from the rand() function
  77. void init_array(unsigned int** array, unsigned int rows, unsigned int cols) {
  78.     //int to calculate total number of elements needed to fill the entire array
  79.     unsigned int elements = rows * cols;
  80.  
  81.     //Creates vector to hold all of the nonrepeating numbers then passes one number since
  82.     //the first value will not have a duplicate already in the vector
  83.     vector<unsigned int> good_values;
  84.     unsigned int first_value = rand() % 4001;
  85.     good_values.push_back(first_value);
  86.  
  87.     //Checks to see if random number is in the vector and if not adds to the end of the vector
  88.     unsigned int count_nums = 0;
  89.     while (count_nums <= elements) {
  90.         unsigned int random_number = rand() % 4001;
  91.         for (unsigned int k = 0; k < good_values.size(); k++) {
  92.             if (good_values[k] == random_number) {
  93.                 break;
  94.             }
  95.             else if (k = good_values.size()) {
  96.                 good_values.push_back(random_number);
  97.                 count_nums++;
  98.             }
  99.         }
  100.     }
  101.     //Assigns all of the values from the good_values vector into the 2d array
  102.     unsigned int j = 0;
  103.     for (unsigned int s = 0; s < rows; s++) {
  104.         for (unsigned int t = 0; t < cols; t++) {
  105.             array[s][t] = good_values[j++];
  106.         }
  107.     }
  108. }
  109.  
  110. //Function to print out given array
  111. void print_array(unsigned int** array, unsigned int rows, unsigned int cols) {
  112.     for (unsigned int j = 0; j < rows; j++) {
  113.         for (unsigned int k = 0; k < cols; k++) {
  114.             cout << setw(6) << array[j][k];
  115.            
  116.             //Adds an end line after it reaches the desired number of columns
  117.             if (k == cols - 1) {
  118.                 cout << endl;
  119.             }
  120.         }
  121.     }
  122. }
  123.  
  124. //Takes the transpose of given array and stores the array into a second given array
  125. void array_transpose(unsigned int** array_one, unsigned int** array_transpose, unsigned int rows, unsigned int cols) {
  126.     //Loops through and flips the values of row and column for transpose array
  127.     for (unsigned int j = 0; j < rows; j++) {
  128.         for (unsigned int k = 0; k < cols; k++) {
  129.             array_transpose[k][j] = array_one[j][k];
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement