Handling two dimensional arrays Finding highest and lowest positions and transpo
ID: 3688527 • Letter: H
Question
Handling two dimensional arrays Finding highest and lowest positions and transpose the array Manipulating data in the array with functions Instructions: You will create a two dimensional 6 times 6 array in main or globally. Next, you will call a number of different functions from main, using the two dimensional the array. Remember to make output attractive and informative. You will make use of five functions for this program. You should complete them in the following order. 1. Fill the 2D array with random values between 1 and 100 (do not seed the random generator this time so all random numbers will the same.) 2. Print the array so the user can see what is in the array. 3. Find the highest value in the array, print it, and print the location (row and column) where it was found. 4. Find the lowest value in the array, print it, and print the location (row and column) where it was found. 5. Transpose the data in the matrix (2D array.) 6. Lastly print the array one more time.Explanation / Answer
#include <iostream> //header file mandatory
#include <cstdlib> // library for random number generator - rand()
using namespace std;
int array[6][6]; // declaring array globally
void print(); // function declarations
void highest();
void lowest();
void load();
void transpose();
int main() //main function program starts here
{
load(); // calling load function to load array values
print(); // calling function to print array values
lowest(); // function to find lowest value and its positions in matrix
highest(); //function to find highest value and its positions in matrix
transpose(); // function to find transpose of matrix
print(); // printing after transpose
return 0;
}
void print() // print function code here
{
for(int i=0;i<6;i++) // loop through array elements - rows
{
for(int j=0;j<6;j++) // loop through array elements -colomuns
{
cout<<array[i][j]<<" "; //printing array value
}
cout<<" "; // entering into next row
}
}
void load() // loading matrix with values from 1 to 100
{
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
array[i][j]=(rand()%100)+1; //rand() function%100 +1 will give numbers bw 1 and 100
}
}
}
void highest() // finding highest value in matrix
{
int max=0,colom,row; //max for storing maximum value, colom and row for positions
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
if(max<array[i][j]) // if max is less the element swap
{
max=array[i][j]; // store max value
row=i; // store row
colom=j; // store colomn
}
}
}
cout<<"position coloumn:"<<colom<<" row:"<<row<<" value"<<max; //print value and positions
}
void lowest() // same as above
{
int min=0,colom,row;
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
if(min>array[i][j])
{
min=array[i][j];
row=i;
colom=j;
}
}
}
cout<<"position coloumn:"<<colom<<" row:"<<row<<" value"<<min;
}
void transpose() // function for tranxpose matrix
{
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
array[j][i]=array[i][j]; // swapping the element
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.