Answer to Question #76583 in C++ for JT

Question #76583
Question 1
Write a C++ program that contains the following functions
A function that takes in a 2D array with 5 rows and 6 columns, and has the user read in values to fill up the array
A function that takes in the 2D array and a parallel array and finds the largest element using row-major order and stores these elements in the corresponding location of the parallel array, as explained in class
A function that does the same as above using column-major order
A function to print the 2D array using row-major order
A function to print the 2D array using column-major order
A function that takes in a 1D array and it's size and prints the entire array.
Use appropriate function calls in the main to display the 2D array in column and row major order, and the content of the 1D arrays
1
Expert's answer
2018-04-27T12:09:08-0400
#include <iostream>

using namespace std;

const int ROWS = 5;
const int COLS = 6;

void fillArray(int array[ROWS][COLS])
{
for (int row = 0; row < ROWS; ++row)
{
for (int col = 0; col < COLS; ++col)
{
cin >> array[row][col];
}
}
}

void largestInRow(int array[ROWS][COLS], int largest[])
{
for (int row = 0; row < ROWS; ++row)
{
int max = array[row][0];
for (int col = 1; col < COLS; ++col)
{
if (array[row][col] > max)
max = array[row][col];
}
largest[row] = max;
}
}

void largestInCol(int array[ROWS][COLS], int largest[])
{
for (int col = 0; col < COLS; ++col)
{
int max = array[0][col];
for (int row = 0; row < ROWS; ++row)
{
if (array[row][col] > max)
max = array[row][col];
}
largest[col] = max;
}
}

void printRowMajor(int array[ROWS][COLS])
{
for (int row = 0; row < ROWS; ++row)
{
for (int col = 0; col < COLS; ++col)
{
cout << array[row][col] << " ";
}
cout << endl;
}
}

void printColMajor(int array[ROWS][COLS])
{
for (int col = 0; col < COLS; ++col)
{
for (int row = 0; row < ROWS; ++row)
{
cout << array[row][col] << " ";
}
cout << endl;
}
}

void printArray(int array[], int arrSize)
{
for (int i = 0; i < arrSize; ++i)
{
cout << array[i] << " ";
}
cout << endl;
}

int main()
{
int array[ROWS][COLS];

cout << "Fill Array:\n";
fillArray(array);

cout << "Row-major order:\n";
printRowMajor(array);

cout << "Col-major order:\n";
printColMajor(array);

int rows[ROWS];

largestInRow(array, rows);

cout << "Largest row values:\n";
printArray(rows, ROWS);

int cols[COLS];

largestInCol(array, cols);

cout << "Largest column values:\n";
printArray(cols, COLS);

return 0;
}

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
APPROVED BY CLIENTS