Home c++ the first line of a two-dimensional array sorting ascending

# the first line of a two-dimensional array sorting ascending

Author

Date

Category

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;
}
``````

``````# 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 , A  + n);
cout & lt; & lt; "Sorted first line of a two-dimensional array: \ n";
for (i = 0; i & lt; n; i ++)
cout & lt; & lt; A  [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

`sort (A , A  + n); `in C++ can not specify the size of non-constant array variable. n must be const or constexpr

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.