Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // THOMAS MICHAEL
- //
- //
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- #include <vector>
- using namespace std;
- void init_array(unsigned int** array, unsigned int rows, unsigned int cols);
- void print_array(unsigned int** array, unsigned int rows, unsigned int cols);
- void array_transpose(unsigned int** array_one, unsigned int** array_transpose, unsigned int rows, unsigned int cols);
- int main(void)
- {
- //Variable for rows and columns of array
- int rows, cols;
- //Loops through until valid input for the number of rows in the array
- for (;;) {
- cout << "Enter the number of rows for the array. Max 50." << endl;
- if (cin >> rows && rows <= 50 && rows > 0) {
- break;
- }
- else {
- cout << "Please enter a valid integer" << endl;
- }
- }
- cout << endl << endl;
- //sets cols = rows since the array is square
- cols = rows;
- //init pointer and 2D array of pointers for the original array
- unsigned int** array_one;
- array_one = new unsigned int*[rows];
- for (unsigned int k = 0; k < rows; k++)
- array_one[k] = new unsigned int[cols];
- //init pointer and 2D array of pointers for the transpose array
- unsigned int** array_two;
- array_two = new unsigned int*[rows];
- for (unsigned int i = 0; i < rows; i++) {
- array_two[i] = new unsigned int[cols];
- }
- //init original array with given values and then gets the transpose of the array
- init_array(array_one, rows, cols);
- array_transpose(array_one, array_two, rows, cols);
- //Calculates the width for set input to center the text depending on the size of the array
- unsigned int width = (cols * 7) / 2;
- //Prints out the original array
- cout << setw(width) << "Array One" << endl << endl;
- print_array(array_one, rows, cols);
- //Prints out the transpose of the original array
- cout << endl << endl;
- cout << setw(width + 3) << "Array Transpose" << endl << endl;
- print_array(array_two, rows, cols);
- cout << endl << endl;
- system("pause");
- return 0;
- }
- //Function that init an array with nonrepeating random values from the rand() function
- void init_array(unsigned int** array, unsigned int rows, unsigned int cols) {
- //int to calculate total number of elements needed to fill the entire array
- unsigned int elements = rows * cols;
- //Creates vector to hold all of the nonrepeating numbers then passes one number since
- //the first value will not have a duplicate already in the vector
- vector<unsigned int> good_values;
- unsigned int first_value = rand() % 4001;
- good_values.push_back(first_value);
- //Checks to see if random number is in the vector and if not adds to the end of the vector
- unsigned int count_nums = 0;
- while (count_nums <= elements) {
- unsigned int random_number = rand() % 4001;
- for (unsigned int k = 0; k < good_values.size(); k++) {
- if (good_values[k] == random_number) {
- break;
- }
- else if (k = good_values.size()) {
- good_values.push_back(random_number);
- count_nums++;
- }
- }
- }
- //Assigns all of the values from the good_values vector into the 2d array
- unsigned int j = 0;
- for (unsigned int s = 0; s < rows; s++) {
- for (unsigned int t = 0; t < cols; t++) {
- array[s][t] = good_values[j++];
- }
- }
- }
- //Function to print out given array
- void print_array(unsigned int** array, unsigned int rows, unsigned int cols) {
- for (unsigned int j = 0; j < rows; j++) {
- for (unsigned int k = 0; k < cols; k++) {
- cout << setw(6) << array[j][k];
- //Adds an end line after it reaches the desired number of columns
- if (k == cols - 1) {
- cout << endl;
- }
- }
- }
- }
- //Takes the transpose of given array and stores the array into a second given array
- void array_transpose(unsigned int** array_one, unsigned int** array_transpose, unsigned int rows, unsigned int cols) {
- //Loops through and flips the values of row and column for transpose array
- for (unsigned int j = 0; j < rows; j++) {
- for (unsigned int k = 0; k < cols; k++) {
- array_transpose[k][j] = array_one[j][k];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement