Need help with isSorted

I would like to create a isSorted function that displays a message if the array elements are sorted or not.

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//This program is used to test the options used in Assignment 4 before inputting them in

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <bits/stdc++.h>

using namespace std;

void enterValues(int[], int);
void displayNumbers(int[], int);
void readNumbers(/*inout*/ifstream &, /*out*/int [], /*in*/int);
void initNumbers(/*out*/ int [], /*in*/int);
void findMedian(int [],const int);
void selectionSort(int [], int);
void swap(double&, double&);
void findMode(int [], int);
void isSorted(int [], int );

int main()
{

    const int ARRAY_SIZE = 10;
    int integers[ARRAY_SIZE], tracker, choice, median;
    bool check;

    do
    {


    cout<<fixed<<showpoint<<setprecision(2); //manipulators set for output format

	// File stream object
	ifstream inFile;

    cout << "---------------------------------------------------------------------" << endl;
    cout << "1-D ARRAY PROCESSING MENU OPTIONS - ARRAY BASED LIST WITH 10 ELEMENTS" << endl;
    cout << "---------------------------------------------------------------------" << endl;
    cout << "1. New data set: Keyboard input(10 numbers from user)" << endl;
    cout << "2. New data set: File data read(10 numbers from the file, numbers.txt.)" << endl;
    cout << "3. Data set: Display current data set." << endl;
    cout << "4. Data set: Display median." << endl;
    cout << "5. Data set: Display mode." << endl;
    cout << "6. Data set: Check array elements - sorted or unsorted." << endl;
    cout << "7. Data set: Ascending sort - Selection Sort." << endl;
    cout << "8. New data set: Random numbers(10 numbers in range of values 1 - 200)." << endl;
    cout << "9. Data set: Binary Search for data item - found, not found and position" << endl;
    cout << "10.Exit program" << endl;

    cout << endl;

    cout << "Enter your choice: ";
    cin >> choice;

    switch(choice)
    {
        case 1: enterValues(integers, tracker);
                cout << endl;
                displayNumbers(integers, tracker);
            break;
        case 2: readNumbers(inFile, integers, tracker);
                cout << endl;
                displayNumbers(integers, tracker);
            break;
        case 3:
            break;
        case 4: selectionSort(integers, ARRAY_SIZE);
                findMedian(integers, ARRAY_SIZE);
            break;
        case 5: selectionSort(integers, ARRAY_SIZE);
                findMode(integers, ARRAY_SIZE);
            break;
        case 6: displayNumbers(integers, tracker);
                isSorted(integers, ARRAY_SIZE);
            break;
        case 7: selectionSort(integers, ARRAY_SIZE);
                displayNumbers(integers, tracker);
            break;
        case 8:
            break;
        case 9:
            break;
        case 10:cout<<endl;
                cout<<"Array processing test now concluded. Exiting program ....."<<endl;
            break;
        default:  cout<<"INVALID CHOICE ...please retype"<<endl;
            break;
    }
    }while(choice != 10);


    return 0;
}

void enterValues(int integers[], int numCount)
{
    const int amount  = 10;

    cout << "Please enter ten values!" << endl;
    cout << endl;

    for(int numCount = 0; numCount < amount; numCount++)
    {
        cout << "Value #" << (numCount + 1) << ": ";
        cin >> integers[numCount];
    }
}

void displayNumbers(int integers[], int numCount)
{
    const int amount = 10;

   cout << " *** Current elements in the array based list *** \n";
   cout << "-------------------------------------------------------\n";

   for (numCount = 0; numCount < amount; numCount++)
    {
      cout<<"Element#"<< (numCount + 1) <<":";
      if(numCount==amount-1)
        cout<<setw (4) <<integers[numCount]<<endl;
      else
        cout<<setw (5) <<integers[numCount]<<endl;
   }
}

void readNumbers(/*inout*/ifstream &inFile, /*out*/int integers[], /*in*/int numCount)
{

    string fileName;
    const int amount = 10;

    cout<<"Please enter filename: ";
    cin>>fileName;
    inFile.open(fileName.c_str() );// Open the file

    //File not found validation routine
	while (!inFile) {
		cin.clear();
		cin.ignore(200, '\n');
		cout << "Error opening the file.\n";
        cout<<"Please enter filename: ";
        cin>>fileName;

        inFile.open(fileName.c_str());    // Open the file.
	}

        for (numCount = 0; numCount < amount; numCount++)
        inFile >> integers[numCount]; //file data read...note no validation for correct data items

        cout<<"File has successfully opened and "<< numCount <<" values have been read and stored."<<endl;
    inFile.close();
    inFile.clear();
}

void initNumbers(/*out*/ int integers[], /*in*/int numCount)
{
    const int amount = 10;

    for(numCount = 0; numCount < amount ; numCount++)
    {
        integers[numCount] = 0;
    }
}

void findMedian(int array[], const int size)
{

    double median;

    if (size % 2 == 0)
        {
            median = (array[size/2] + array[size/2-1])/2.0;
        }
    else
        median = array[size/2];

   cout << "The median is: " << median << endl;

}

void selectionSort(int array[], int size)
{
   int minIndex, minValue;

   for(int start = 0; start < (size -1); start++)
   {
       minIndex = start;
       minValue = array[start];
       for(int index = start + 1; index < size; index++)
       {
           if(array[index] < minValue)
           {
               minValue = array[index];
               minIndex = index;
           }
       }
       swap(array[minIndex], array[start]);
   }
}

void swap(double &a, double &b)
{
    double temp = a;
    a = b;
    b = temp;
}

void findMode(int array[], int size)
{
    int number = array[0];
    int mode = number;
    int count = 1;
    int countMode = 1;

    for (int i=1; i<size; i++)
    {
        if (array[i] == number)
        {
            count++;
        }
        else
        {
            if (count > countMode)
            {
                countMode = count;
                mode = number;
            }
            count = 1;
            number = array[i];
        }
    }

cout << "mode : " << mode << endl;
}

void isSorted(int array[], int size)
{
    
}
Can you start from:
1
2
3
4
5
6
#include <algorithm>

void isSorted(int array[], int size)
{
    std::cout << std::is_sorted( array, array+size ) << '\n';
}
I am a bit unfamiliar with using std:: since we use another type of format when doing cout and such
From beginning to end, think about what must hold true for an array to be deemed as 'sorted.' Assuming we are using increasing order from 0 to n, element array[0] must be less than array[1], array[1] less than array[2], and so on, up to n where array[n-1] must be less than array[n-2].

I would suggest using two variables to keep track of the previous and current array elements and compare each one in a loop. If the condition that the previous is not less than the current, use an error flag and break out of the loop. The opposite is also true for an array in decreasing order.
Would a bool be viable to use in this situation?
Yes. Inside your loop check whether element n-1 (prev) is less than n (current) and if so, set your error flag, in this case, set your bool variable to false indicating that the array is not sorted, and then break out of the loop and return your flag. Before entering the loop make the best-case assumption that your array is already sorted and set your flag to true.

Edit: Just don't forget to change your isSorted function appropriately since you'll want to return a bool.
Last edited on
Topic archived. No new replies allowed.