I can not find on the Internet how to sort the first line of a two-dimensional array in ascending order, using the function sort ()
.
# include & lt; iostream & gt;
#Include & lt; algorithm & gt;
#Include & lt; Time.h & gt;
#Include & lt; stdlib.h & gt;
Using Namespace STD;
INT MAIN ()
{
long n = 10;
int A [n] [n];
int i, j, sum = 0, k = 0;
COUT & LT; & LT; "Enter the matrix \ n";
for (i = 0; i & lt; n; i ++) {
for (j = 0; j & lt; n; j ++) {
A [i] [j] = rand ()% 10;
COUT & LT; & LT; A [i] [j] & lt; & lt; "";}
COUT & LT; & LT; Endl;
}
for (i = 0; i & lt; n; i ++)
for (j = 0; j & lt; n; j ++) {
if (i == j)
{
sum + = A [i] [i];
}
}
cout & lt; & lt; Endl & lt; & lt; "Sum =" & lt; & lt; sum;
Return 0;
}
Answer 1
# include & lt; iostream & gt;
#Include & lt; algorithm & gt;
#Include & lt; Time.h & gt;
#Include & lt; stdlib.h & gt;
Using Namespace STD;
INT MAIN ()
{
long n = 10;
int A [n] [n];
int i, j, sum = 0, k = 0;
COUT & LT; & LT; "Enter the matrix \ n";
for (i = 0; i & lt; n; i ++) {
for (j = 0; j & lt; n; j ++) {
A [i] [j] = rand ()% 10;
COUT & LT; & LT; A [i] [j] & lt; & lt; "";}
COUT & LT; & LT; Endl;
}
sort (A [0], A [0] + n);
cout & lt; & lt; "Sorted first line of a two-dimensional array: \ n";
for (i = 0; i & lt; n; i ++)
cout & lt; & lt; A [0] [i] & lt; & lt; ' ';
Return 0;
}
Here we are in as a parameter to the function sort
is a pointer to the first element of the first row, and at the end of the first line
Answer 2
sort (A [0], A [0] + n);
in C++ can not specify the size of non-constant array variable. n must be const or constexpr