ASCENDING ORDER ARRAY OF NUMBERS

EVERYONE KINDLY HELP WITH AN ASSIGNMENT THAT IS 15MARKS AND URGENT AS WE ARE TO SUBMIT TOMORROW.
WE ARE TO WRITE A C++ PROGRAM TO SORT A 2*6 DIMENSIONAL ARRAY WITH 5,8,-1,2,4,5 ON THE FIRST ROW AND -3,8,-4,-5,6,8 ON THE SECOND ROW.
sorting a multiple dimensional array is not defined. You can sort them by columns, by rows, or by the whole thing (treated as if 1-d, lowest is 0,0, next lowest is 0,1, next is 0,2... 1,5) or the same thing column wise.

Which of those (or something else?) did you need?

It would also help to see what you have done and where it is giving you trouble. Its hard to guess from a blank page where your error is.

Last edited on
The point exactly i don't know how to do anything,can anyone help with the codes?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <algorithm> // std::sort
#include <iomanip> // std::setw

int main()
{
    // http://en.cppreference.com/w/cpp/types/size_t
    const std::size_t NROWS = 3 ; // adjust as required
    const std::size_t NCOLS = 5 ; // adjust as required

    // define the initialised NROWSxNCOLS array (3x5 in this example)
    int array[NROWS][NCOLS] { {5,8,-1,2,4}, {5,3,8,-4,-5}, {6,8,-6,9,3} } ;

    // print it
    // http://www.stroustrup.com/C++11FAQ.html#for
    // http://www.stroustrup.com/C++11FAQ.html#auto
    for( const auto& row : array ) // for each row in the array
    {
        // for each int v in the row, print its value
        // http://en.cppreference.com/w/cpp/io/manip/setw
        for( int v : row ) std::cout << std::setw(4) << v ;

        std::cout << '\n' ;
    }

    // sort it as a single sequence of NROWS*NCOLS integers
    // http://en.cppreference.com/w/cpp/algorithm/sort
    // note: the array is stored as a sequence of NROWS*NCOLS contiguous integers in memory
    // array[0] is the first row of the array; when decayed, it yields a pointer to the first int in the row
    // see Array-to-pointer decay: http://en.cppreference.com/w/cpp/language/array
    std::sort( array[0], array[0] + NROWS*NCOLS ) ;

    // and print it once again after the sort
    std::cout << "\n-------- sorted --------\n\n" ;

    for( const auto& row : array )
    {
        for( int v : row ) std::cout << std::setw(4) << v ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/b2b92636e32412a2
Thank you very much.
much appreciated!
Topic archived. No new replies allowed.