Why is my array not passing properly?

I need to write a function that checks if an array passed through it in order from smallest value to largest. The issue that I'm having is that the function is returning true for all arrays. I put a 'cout' statement within the function an noticed that the numbers did not match the array. For example, when 4 is pressed, since the array would be [30..1], it should return the array and that it is not ordered. Yet the actual return is:
18224
array is sorted
1
which is incorrect. What am I doing wrong?

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
int isOrdered(int arr[], int n) {
    for (int i = 0; i < n; i++) {
        cout << arr[i] << '  ';
        cout << endl;
        if (arr[i] <= arr[i + 1]) {
            cout << "array is sorted " << endl;
            return 1;
        }
        else {
            cout << "array is not sorted " << endl;
            return -1;
        }
    }
}

int main() {
    int input;

    // Prompt for user
    cout << "----------------------------------------------" << endl;
    cout << " Press 1 to exit the program " << endl;
    cout << " Press 2 to select the array that is sorted in increasing order " << endl;
    cout << " Press 3 to select the array that is randomly sorted " << endl;
    cout << " Press 4 to select the array that is sorted in decreasing order " << endl;
    cout << "----------------------------------------------" << endl;
    cin >> input;

    // catch if input not within bounds
    while (input != 1 && input != 2 && input != 3 && input != 4)
    {
        cout << "----------------------------------------------" << endl;
        cout << " Press 1 to exit the program " << endl;
        cout << " Press 2 to select the array that is sorted in increasing order " << endl;
        cout << " Press 3 to select the array that is randomly sorted " << endl;
        cout << " Press 4 to select the array that is sorted in decreasing order " << endl;
        cout << "----------------------------------------------" << endl;
        cin >> input;
    }

    while (input != 1)
    {
        int n = 30;

        int* a = new int[n];
        int* b = new int[n];
        int* c = new int[n];

        int* a_c = new int[n];
        int* b_c = new int[n];
        int* c_c = new int[n];

        if (input == 2) {
            for (int i = 0; i < n; i++) {
                a[i] = i + 1;
            }

            // duplicated array needed to be created per instructions 
            for (int i = 0; i < n; i++) {
                a_c[i] = a[i];
            }

            cout << isOrdered(a_c, n) << endl;
            
        }

        else if (input == 3) {
            /* seed the PRNG (MT19937) using a variable value (in our case, s)*/
            std::mt19937 generator(1); // seed by variable input
            std::uniform_int_distribution<int> distribution(1, n); // random numbers need to be in range between 1, 100000 

            for (int i = 0; i < n; i++) {
                b[i] = distribution(generator);
                //cout << b[i] << ' '; // testing
            }

            // create duplicate 
            for (int i = 0; i < n; i++) {
                b_c[i] = b[i];
                cout << b_c[i] << ' ';
            }

            cout << isOrdered(b, n) << endl;
        }

        else {
            for (int i = n-1; i >= 0; i--) {
                c[i] = i + 1;
            }
            
            // create duplicate 
            for (int i = 0; i < n; i++) {
                c_c[i] = c[i];
            }

            cout << isOrdered(c_c, n) << endl;
        }

        // Prompt user again
        cout << "----------------------------------------------" << endl;
        cout << " Press 1 to exit the program " << endl;
        cout << " Press 2 to select the array that is sorted in increasing order " << endl;
        cout << " Press 3 to select the array that is randomly sorted " << endl;
        cout << " Press 4 to select the array that is sorted in decreasing order " << endl;
        cout << "----------------------------------------------" << endl;
        cin >> input;

        // catch if input not within bounds
        while (input != 1 && input != 2 && input != 3 && input != 4)
        {
            cout << "----------------------------------------------" << endl;
            cout << " Press 1 to exit the program " << endl;
            cout << " Press 2 to select the array that is sorted in increasing order " << endl;
            cout << " Press 3 to select the array that is randomly sorted " << endl;
            cout << " Press 4 to select the array that is sorted in decreasing order " << endl;
            cout << "----------------------------------------------" << endl;
            cin >> input;
        }
    }

    exit(0);
}
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
#include <iostream>

using namespace std;

bool isOrdered(int arr[], int n)
{
    for (int i = 1; i < n; i++)
    {
        if(arr[i-1] >= arr[i])
            return false;
    }
    
    return true;
}

int main()
{
    int array_1[]{ 111,37,22,4,3,2};
    cout << isOrdered(array_1, sizeof(array_1)/sizeof(int) ) << '\n';
    
    int array_2[]{3,4,22,37,111};
    cout << isOrdered(array_2, sizeof(array_2)/sizeof(int) ) << '\n';
    
    return 0;
}
First, use a do while loop instead of rewriting code:

1
2
3
4
5
6
7
8
9
10
do
{
	cout << "----------------------------------------------" << endl;
	cout << " Press 1 to exit the program " << endl;
	cout << " Press 2 to select the array that is sorted in increasing order " << endl;
	cout << " Press 3 to select the array that is randomly sorted " << endl;
	cout << " Press 4 to select the array that is sorted in decreasing order " << endl;
	cout << "----------------------------------------------" << endl;
	cin >> input;
} while (input != 1 && input != 2 && input != 3 && input != 4);


Next, your function doesn't loop through the array. It only takes the first two elements, and if those two happen to be ordered, it'll output ordered.

Also, you're not generating random numbers, it's going to generate the same numbers every time you run the program. Here's how to seed the twister:

1
2
std::random_device rd;
std::mt19937 mersenne(rd());
Was in a hurry so I missed some important info:

Favor std::vector over dynamically allocated arrays.

Moreover, the reason the array when outputted in the function is different then when you assign the values is because of this line:

cout << arr[i] << ' ';

You have 2 spaces inside ' ' which screws things up. You want only 1 space or to use quotes:

cout << arr[i] << ' ';
Last edited on
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
#include <iostream>

using namespace std;

bool isIncreasingOrder(int arr[], int n)
{
    for (int i = 1; i < n; i++)
    {
        if(arr[i-1] >= arr[i])
            return false;
    }
    return true;
}

bool isDecreasingOrder(int arr[], int n)
{
    for (int i = 1; i < n; i++)
    {
        if(arr[i-1] <= arr[i])
            return false;
    }
    return true;
}

bool isUnordered(int arr[], int n)
{
    if( !isDecreasingOrder( arr, n) and !isIncreasingOrder( arr, n) )
        return true;
    else
        return false;
}

int main()
{
    // DECREASING ORDER
    int array_1[]{ 111,37,22,4,3,2};
    cout
    << isIncreasingOrder(array_1, sizeof(array_1)/sizeof(int) ) << ' '
    << isDecreasingOrder(array_1, sizeof(array_1)/sizeof(int) ) << ' '
    << isUnordered(array_1, sizeof(array_1)/sizeof(int) )
    << '\n';
    
    // INCREASING ORDER
    int array_2[]{3,4,22,37,111};
    cout
    << isIncreasingOrder(array_2, sizeof(array_2)/sizeof(int) ) << ' '
    << isDecreasingOrder(array_2, sizeof(array_2)/sizeof(int) ) << ' '
    << isUnordered(array_2, sizeof(array_2)/sizeof(int) )
    << '\n';
    
    // NO ORDER
    int array_3[]{3,22,4,37,111};
    cout
    << isIncreasingOrder(array_3, sizeof(array_3)/sizeof(int) ) << ' '
    << isDecreasingOrder(array_3, sizeof(array_3)/sizeof(int) ) << ' '
    << isUnordered(array_3, sizeof(array_3)/sizeof(int) )
    << '\n';
    
    return 0;
}


0 1 0
1 0 0
0 0 1
Program ended with exit code: 0
I took all answers and updated my isOrdered function and it is working for options '2' and '4' but not '3'. The most recent run I performed gave the output:
1
2
3
4
5
Test after B has been sorted:
2 3 3 7 8 8 9 9 10 10

3 3 array is not sorted
-1


updated code:
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <chrono>
#include <cmath>
#include <math.h>
#include <random>
using namespace std;
// Insertion Sort
void insertionSort(int arr[], int n) {
    auto start = std::chrono::steady_clock::now(); // needed to test runtime of function
    for (int i = 1; i < n; i++) {
        int j, temp = arr[i]; // make a copy of a[i]
        for (j = i - 1; j >= 0; j--) { // starting "moving left"
            if (arr[j] > temp)
                arr[j + 1] = arr[j]; // inversion detected, move a[j] to the right
            else
                break; // index j is one spot to the left of where temp belong
        }
        arr[j + 1] = temp;
    }
    auto end = std::chrono::steady_clock::now();
    double elapsed_time_ns = double(std::chrono::duration_cast <std::chrono::nanoseconds> (end - start).count());
    double elapsed_time_ms = double(std::chrono::duration_cast <std::chrono::milliseconds> (end - start).count());

    cout << "Elapsed time of Insertion Sort: " << elapsed_time_ns << " ns vs " << elapsed_time_ms << " ms" << endl;
}

// Check if array has been ordered
int isOrdered(int arr[], int n) {
    if (n <= 1)   // array with 1 or no elements
        return 1; // will always be sorted

    for (int i = 1; i < n; i++) {
        cout << arr[i] << ' ';
        if (arr[i-1] >= arr[i]) {
            cout << "array is not sorted " << endl;
            return -1;
        }
    }
    return 1;
}

int main() {
    int input;

    // Prompt user with catch if input not within bounds
    do
    {
        cout << "----------------------------------------------------------------" << endl;
        cout << " Press 1 to exit the program " << endl;
        cout << " Press 2 to select the array that is sorted in increasing order " << endl;
        cout << " Press 3 to select the array that is randomly sorted " << endl;
        cout << " Press 4 to select the array that is sorted in decreasing order " << endl;
        cout << "----------------------------------------------------------------" << endl;
        cin >> input;
    } while (input != 1 && input != 2 && input != 3 && input != 4);

    while (input != 1)
    {
        int n = 10;

        int* a = new int[n];
        int* b = new int[n];
        int* c = new int[n];

        int* a_c = new int[n];
        int* b_c = new int[n];
        int* c_c = new int[n];

        if (input == 2) {
            for (int i = 0; i < n; i++) {
                a[i] = i + 1;
            }

            // create duplicate array as specified by instructions
            for (int i = 0; i < n; i++) {
                a_c[i] = a[i];
            }

            insertionSort(b_c, n);

            cout << isOrdered(a_c, n) << endl;
            
        }

        else if (input == 3) {
            /* seed the PRNG (MT19937) using a variable value (in our case, rd)*/
            std::random_device rd;
            std::mt19937 generator(rd()); // seed by variable input
            std::uniform_int_distribution<int> distribution(1, n); // random numbers need to be in range between 1, n 
            
            for (int i = 0; i < n; i++) {
                b[i] = distribution(generator);
            }

            // create duplicate 
            for (int i = 0; i < n; i++) {
                b_c[i] = b[i];
            }
            
            insertionSort(b_c, n);
            // test
            cout << "Test after B has been sorted: " << endl;
            for (int i = 0; i < n; i++) {
                cout << b_c[i] << ' ';
            } // insertion sort worked but isOrdered function still returns false
            cout << endl;
            cout << endl;

            cout << isOrdered(b_c, n) << endl;
        }

        else {
            int temp_1 = n;
            for (int i = 0; i < n; i++) {
                c[i] = temp_1;
                temp_1--;
            }
            
            // create duplicate 
            for (int i = 0; i < n; i++) {
                c_c[i] = c[i];
            }

            insertionSort(c_c, n);

            cout << isOrdered(c_c, n) << endl;
        }

        // Prompt user again with catch if input not within bounds
        do
        {
            cout << "----------------------------------------------------------------" << endl;
            cout << " Press 1 to exit the program " << endl;
            cout << " Press 2 to select the array that is sorted in increasing order " << endl;
            cout << " Press 3 to select the array that is randomly sorted " << endl;
            cout << " Press 4 to select the array that is sorted in decreasing order " << endl;
            cout << "----------------------------------------------------------------" << endl;
            cin >> input;
        } while (input != 1 && input != 2 && input != 3 && input != 4);
    }

    exit(0);
}
Line 34.

This line says that if the previous number is greater than or equal to the current one, the list is not sorted. This means that if you have duplicates, 3 is equal to 3 which would trigger the if statement to say false. What you want is:

if (arr[i-1] > arr[i]) {
Thank you
Topic archived. No new replies allowed.