Arrays, sorted arrays

Hi I was given homework on arrays, but this new stuff is very confusing to follow. I'm asked to create an array with 10 integers from user input and have the values from 0-20. If the array is not sorted then sort it i.e. array [0] < array [1] < array [2] < ...

If anybody can walk me through I would appreciate it thank you.
Hi

this might help
http://www.cplusplus.com/articles/NhA0RXSz/

but if you want to go old school and write your own algorithm to sort then you should start from reading this

http://mathbits.com/MathBits/CompSci/Arrays/Sorting.html
Compiled and tested:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>

int main()
{
        std::cout << "Enter 10 values (0-20). Press enter after each:\n\n";
        constexpr unsigned short int SIZE = 10;
        int array[SIZE] = { };
        for ( auto &i : array )
        {
                while ( !( std::cin >> i ) || ( i < 0 || i > 20 ) )
                {
                        std::cin.clear();
                        std::cin.ignore( 1000000, '\n' );
                        std::cout << "Enter value (0-20): ";
                }
        }
        bool isSorted = true;
        unsigned short int count = 0;
        constexpr unsigned short int END = SIZE - 1;
        for ( auto c  : array )
        {
                if ( count != END )
                {
                        if ( c > array[count + 1] )
                        {
                                isSorted = false;
                                break;
                        }
                }
                else
                {
                        break;
                }
                ++count;
        }
        if ( !isSorted )
        {
                for ( unsigned short int rep = 0; rep != SIZE; ++rep )
                {
                        for ( unsigned short int ind = 0; ind != END; ++ind )
                        {
                                const unsigned short int NEXT = ind + 1;
                                if ( array[ind] > array[NEXT] )
                                {
                                        array[ind] += array[NEXT];
                                        array[NEXT] = array[ind] - array[NEXT];
                                        array[ind] -= array[NEXT];
                                }
                        }
                }
        }
        std::cout << '\n';
        count = 0;
        for ( auto c : array)
        {
                std::cout << c;
                if (count != END)
                {
                        std::cout << ", ";
                }
                std::cout << std::flush;
                ++count;
        }

        return 0;
}
Last edited on
Thanks shadder although I'm not making any algorithms yet I'm just doing simple array problems
Thank you for the coding boost lexical cast, but is there a way it can be done without constexpr, auto, and const? I haven't learned any of those key words yet
Achilles23
There are many sorting algorithms, but the classic one is "bubble sort" - try googling that. It consists of successive passes down an array, swapping pairs of elements that are out of order, so that the "lightest" (i.e smallest) gradually "bubble" to the top.

I think boost lexical cast actually has a version of it above , in his lines 38-50, but it may be easier to flush it out on paper. His lines 45-47 are just a swapping routine (for objects that can be added or subtracted), but you can probably code a swap(a,b) for arbitrary objects, irrespective of whether they can participate in arithmetic operations.

There are tweaks that make bubble sort go faster. The largest element ends up at the end of the array on the first pass, so you only have to check to the penultimate element on the second pass etc. and your swapping passes get shorter each time. Similarly, if you make no swaps on one pass then you know that you are sorted, so you can go home early.

Other routines include QuickSort, but there are many others.
This might help you.. fyi: it's not very efficient but it will work for what you want.


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
#include <iostream>


using namespace std; 


int main()
{
	int a[10] = {1, 3, 20, 5, 2, 7, 4, 0, 17, 9};
	int b[10] = {};
	int temp_loc;


for (int i = 0; i <= 9; i++){
	for (int j = 0; j <= 9; j++)
	{
		if (a[i] < a[j]) {
			temp_loc = a[i];
			a[i] = a[j];
			a[j] = temp_loc;
		}
	}
}

for (int i = 0; i < 10; i++) {
	cout << a[i] << " ";
	b[i] = a[i]; 
}

return 0; 
}
	
Lastchance

That's what we were taught, but I did not know there was actually a name for that thanks I'll be sure to look it up

Tibrado

Thank you for the code however its not asking for an input right away its asking to ask for user input on the monitor so anybody can use it, but it did work though.
Achilles23 wrote:
Thank you for the coding boost lexical cast, but is there a way it can be done without constexpr, auto, and const?

Replace all occurences of auto with unsigned short int.

Remove all keywords constexpr and const.

The code will still work.

P.S. I used auto so the compiler will deduce the type. I used constexpr because SIZE is constant and will be used as a constant expression. I used const because NEXT won't change.
Last edited on
Topic archived. No new replies allowed.