Answer to Question #100801 in C++ for sabra

Question #100801
11/ Write a program that will find the inverse of any two dimensional array without the usage of the built-in function.
1
Expert's answer
2019-12-27T05:56:31-0500

#include <iostream>

#include <time.h>

//Use time.h library to generate random numbers and fill the array

using namespace std; //do not write this if you use printf() instead cout <<

void main()

{

srand(time(NULL));

//srand() function seting start value, seed for rand() function

//time(NULL) returns time that passed since some moment int milliseconds, so we can call this value random


const int x = 3;

const int y = 3;

//Constants as sizes of static array

//They can have any value


int arr[x][y];


cout << "Original array: " << "\n\n"; //"\n\n" newline twice;


for (int i = 0; i < x; i++)

{

for (int j = 0; j < y; j++)

{

arr[i][j] = rand() % 10; //Filling array with random numbers from 0 to 9

cout << arr[i][j] << " "; //We show array.

}

cout << "\n"; //newline

}


int inverse[x][y];

for (int i = 0; i < x; i++)

{

for (int j = 0; j < y; j++)

{

inverse[i][j] = arr[x - 1 - i][y - 1 - j]; //We write the array backwards.

//For example, if i = 0 and j = 0

// inverse[0][0] - very first element of new array

// is arr[x - 1][y - 1] - very last elememt of original array,

}

}



//We show new array

cout << "\n" << "Inversed array: " << "\n\n";


for (int i = 0; i < x; i++)

{

for (int j = 0; j < y; j++)

{

cout << inverse[i][j] << " ";

}

cout << "\n"; //newline

}


cout << "\n\n";


return;

}



WITHOUT COMMENTS

USING COUT <<


#include <iostream>

#include <time.h>

using namespace std;

void main()

{

srand(time(NULL));


const int x = 3;

const int y = 3;

//Constants as sizes of static array

//They can have any value


int arr[x][y];


cout << "Original array: " << "\n\n";


for (int i = 0; i < x; i++)

{

for (int j = 0; j < y; j++)

{

arr[i][j] = rand() % 10;

cout << arr[i][j] << " ";

}

cout << "\n";

}


int inverse[x][y];


for (int i = 0; i < x; i++)

for (int j = 0; j < y; j++)

inverse[i][j] = arr[x - 1 - i][y - 1 - j];


cout << "\n" << "Inversed array: " << "\n\n";


for (int i = 0; i < x; i++)

{

for (int j = 0; j < y; j++)

{

cout << inverse[i][j] << " ";

}

cout << "\n";

}


cout << "\n\n";


return;

}


USING PRINTF()


#include <iostream>

#include <time.h>

void main()

{

srand(time(NULL));


const int x = 3;

const int y = 3;

//Constants as sizes of static array

//They can have any value


int arr[x][y];


printf("%s: \n\n", "Original array");


for (int i = 0; i < x; i++)

{

for (int j = 0; j < y; j++)

{

arr[i][j] = rand() % 10;

printf("%d ", arr[i][j]);

}

printf("\n");

}


int inverse[x][y];


for (int i = 0; i < x; i++)

for (int j = 0; j < y; j++)

inverse[i][j] = arr[x - 1 - i][y - 1 - j];


printf("\n%s: \n\n", "Inversed array");


for (int i = 0; i < x; i++)

{

for (int j = 0; j < y; j++)

{

printf("%d ", inverse[i][j]);

}

printf("\n");

}


printf("\n\n");


return;

}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS