Topic: Basics of C++ Programming Language "ARRAY IN C++"
Codes discussed will be posted here. Write your conclusion on the ARRAY discussion.
Filename: Example_8-11.cpp
Code:
--->> https://drive.google.com/file/d/1JSVMELJzTJuf--Zr2h2ubxpXsLVTrr9Q/view?usp=sharing
*Note need proper codes and conclusion on how it's done and about the results. And please give the proper solution and explanation.
Note: Place your conclusion on why did it turn out like that and give your reasons why the results turned like that, one paragraph will do for the explanation and conclusion.
Note: Please place the proper conclusion and explanations and results of the codes which were given.
// Passing Two-dimensional arrays as parameters to functions.
#include <iostream>
#include <iomanip>
using namespace std;
const int NUMBER_OF_ROWS = 6;
const int NUMBER_OF_COLUMNS = 5;
void printMatrix(int matrix[][NUMBER_OF_COLUMNS],
int NUMBER_OF_ROWS);
void sumRows(int matrix[][NUMBER_OF_COLUMNS],
int NUMBER_OF_ROWS);
void largestInRows(int matrix[][NUMBER_OF_COLUMNS],
int NUMBER_OF_ROWS);
int main()
{
int board[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]
= {{17, 8, 24, 10, 28},
{9, 20, 16, 55, 90},
{25, 45, 35, 8, 78},
{5, 0, 96, 45, 38},
{76, 30, 8, 14, 28},
{9, 60, 55, 62, 10}};
printMatrix(board, NUMBER_OF_ROWS);
cout << endl;
sumRows(board, NUMBER_OF_ROWS);
cout << endl;
largestInRows(board, NUMBER_OF_ROWS);
return 0;
}
void printMatrix(int matrix[][NUMBER_OF_COLUMNS],
int numOfRows)
{
int row, col;
for (row = 0; row < numOfRows; row++)
{
for (col = 0; col < NUMBER_OF_COLUMNS; col++)
cout << setw(5) << matrix[row][col] << " ";
cout << endl;
}
}
void sumRows(int matrix[][NUMBER_OF_COLUMNS], int numOfRows)
{
int row, col;
int sum;
for (row = 0; row < numOfRows; row++)
{
sum = 0;
for (col = 0; col < NUMBER_OF_COLUMNS; col++)
sum = sum + matrix[row][col];
cout << "Sum of row " << (row + 1) << " = " << sum
<< endl;
}
}
void largestInRows(int matrix[][NUMBER_OF_COLUMNS],
int numOfRows)
{
int row, col;
int largest;
for (row = 0; row < numOfRows; row++)
{
largest = matrix[row][0];
for (col = 1; col < NUMBER_OF_COLUMNS; col++)
if (largest < matrix[row][col])
largest = matrix[row][col];
cout << "The largest element of row " << (row + 1)
<< " = " << largest << endl;
}
}
Two-dimensional array as arguments to a method (function).
Bypassing an array to a function, it is possible to get the maximum in each row, and also find the sum and the average of all the elements in the array. Since functions are reusable, different arrays can be passed to the functions and get the average and the maximum.
Comments
Leave a comment