(1) To get the total of all values stored in an array.
(2) To get the average of all of the array values.
(3) To get the total of all of the arrays values.
(4) To get the total of all of the columns values.
(5) To get the highest number in the row.
(6) To get the lowest value in the array. |
It is hard to say, how 1 and 3 differ. 6 is suspicious too.
How about:
1. Sum of all elements of 2D array.
2. Average of all elements of 2D array. (==sum/count)
3. Sum of elements of one row. (repeat for each row)
4. Sum of elements of one column. (repeat for each column)
(sum of all == sum of row-sums == sum of column-sums)
5. Maximum element of one row. (repeat for each row)
6. Minimum element of one row. (repeat for each row)
Your lines 9-14 (attempt to) declare variables. They are
global variables; in "file scope".
You could do so, but you will (have to) learn more, if you do use
local variables.
A function is much more usable, when it does not depend on globals, but uses parameters and return value.
A function that calculates should (preferably) not print anything.
First a "scary" example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<std::vector<int>> data
{ {4, 24, 62, 91, 101},
{77, 0, 45, 33, 6},
{56, 7, 1, 27, 211},
{1, 4, 0, 22, 50} };
for ( const auto & row : data ) {
auto hilo = std::minmax_element( row.begin(), row.end() );
std::cout << "row max " << *hilo.second << " min " << *hilo.first << '\n';
}
}
|
But, but, We haven't had vectors yet!
No problem. Plain array is just as fine:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <algorithm>
int main()
{
constexpr size_t Cols {5};
int data[][Cols]
{ {4, 24, 62, 91, 101},
{77, 0, 45, 33, 6},
{56, 7, 1, 27, 211},
{1, 4, 0, 22, 50} };
for ( const auto & row : data ) {
auto hilo = std::minmax_element( std::begin(row), std::end(row) );
std::cout << "row max " << *hilo.second << " min " << *hilo.first << '\n';
}
}
|
What an odd loop you have and can we do just max?
How quaint, but why not:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <algorithm>
int main()
{
constexpr size_t Cols {5};
constexpr size_t Rows {4}; // cheat, we should read this from the array
int data[][Cols]
{ {4, 24, 62, 91, 101},
{77, 0, 45, 33, 6},
{56, 7, 1, 27, 211},
{1, 4, 0, 22, 50} };
for ( size_t row=0; row < Rows; ++row ) {
auto hi = std::max_element( data[row], data[row] + Cols );
std::cout << "row " << row << " max " << *hi << '\n';
}
}
|
Our function is still a one-liner from Standard Library. What magic is that?
I presume that you have (not) seen:
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/arrays/
You should (re)read them. There is section "Arrays as parameters". Short, not sweet.
The
data[row]
and
data[row] + Cols
are
pointers to first element and one-past last element of one row of the 2D array.
An alternative is to give a pointer to first element and the count of elements.
We could make the function more complex by giving pointer to first element of the 2D array, the index of row that we are interested in, and the count of elements in one row.
We could make the function yet more complex by giving pointer to first element of the 2D array, the count of rows, the count of elements in one row, and pointer to 1D array (which must have at least rows elements).
Fun!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
int main()
{
constexpr size_t Cols {5};
constexpr size_t Rows {4};
int data[][Cols]
{ {4, 24, 62, 91, 101},
{77, 0, 45, 33, 6},
{56, 7, 1, 27, 211},
{1, 4, 0, 22, 50} };
int rowmax[Rows];
for ( size_t row=0; row < Rows; ++row ) {
auto max1 = foo( data[row], Cols );
auto max2 = bar( data, Cols, row );
// show max1 or max2
}
gaz( data, Rows, Cols, rowmax );
for ( size_t row=0; row < Rows; ++row ) {
// show rowmax[row]
}
}
|
Oh, I forgot to declare and implement the functions foo, bar, and gaz.
What parameter types do they need? When used as parameter
data[row] has type int*
data has type int**
Cols has type size_t
Rows has type size_t
row has type size_t
rowmax has type int*
The foo and bar do return an
int
value.
The gaz does not return a value and should have return type
void
. It stores results into its fourth parameter.
That I could write:
void gaz( const int** matrix, size_t rows, size_t cols, int* rmax );