Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives - Handling two dimensional arrays Create a two-dimensional 6 X 6 arra

ID: 3546346 • Letter: O

Question

Objectives - Handling two dimensional arrays

Create a two-dimensional 6 X 6 array.  Fill it with random numbers from 1-100.  Print the array. Then print the highest and lowest values in the array and the indexes of where those values sit in the array. Transpose the array.  Finally, print the array again.

Must have a function for every method out side out the main function. (voidfillarray, voidprintarray, voidfindhighest, voidfindlowest, voidtranspose).

voidfindhighest and voidfindlowest must also find the indexes of the values in the array.

All code must be in C++ language.

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
const int SIZE = 6;

void fillArray(int arr[6][6])
{
srand((unsigned)time(NULL));
for( int i = 0; i < 6; i++){
for( int j = 0; j < 6; j++){
arr[i][j] = rand()% 100 + 1;
}
}
}

void printArray(int arr[6][6])
{
for( int i = 0; i < 6; i++){
for( int j = 0; j < 6; j++){
//proper spacing
if(arr[i][j] < 10)
cout << " ";
else if(arr[i][j] < 100)
cout << " ";
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
}

void findHighest(int arr[6][6])
{
int high = 0, rowIndex, colIndex;
for( int i = 0; i < 6; i++){
for( int j = 0; j < 6; j++){
if(arr[i][j] > high){
high = arr[i][j];
rowIndex = i;
colIndex = j;
}
}
}
cout << "Highest Value: " << high <<" Located in index: " << rowIndex << " " << colIndex << endl;
}

void findLowest(int arr[6][6])
{
int low = 101, rowIndex, colIndex;
for( int i = 0; i < 6; i++){
for( int j = 0; j < 6; j++){
if(arr[i][j] < low){
low = arr[i][j];
rowIndex = i;
colIndex = j;
}
}
}
cout << "Lowest Value: " << low <<" Located in index: " << rowIndex << " " << colIndex << endl;
}

void transpose(int arr[6][6])
{
/*the idea of transposing is shown here
// 00 01 02 03 04 05 | 00 10 20 30 40 50
// 10 11 12 13 14 15 | 01 11 21 31 41 51
// 20 21 22 23 24 25 | 02 12 22 32 42 52
// 30 31 32 33 34 35 | 03 13 23 33 43 53
// 40 41 42 43 44 45 | 04 14 24 34 44 54
// 50 51 52 53 54 55 | 05 15 25 35 45 55
*/
int temp;
for (int i = 0; i < 6; i++){
//moves diagonially to avoid changing values multiple times
for (int j = i; j < 6; j++){
//diagonals do not change
if(i != j){
temp = arr[i][j]; //saves number
//swaps 2 values
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
}
}

int main()
{
//6x6 array of ints
int arr[SIZE][SIZE];
//fill the array with random numbers
fillArray(arr);
printArray(arr);
findHighest(arr);
findLowest(arr);
transpose(arr);
printArray(arr);

system("pause"); //for DevC++ remove otherwise
return 0;
}