Search code examples
c++arrayssortingmergedynamic-memory-allocation

How to check whether array elements entered by user are sorted or not in c++


So it's a simple c++ program which takes 2 sorted arrays from user dynamically using new operator and sum ups their size to create a third array which is equal to the length of the both array sum.. Like final_arr = arr1_size +arr2_size; Everything is working fine but the problem is that is that I am taking array values from user so I need to check up that array entered by user is sorted or not, if array is not sorted then program must take that array values from user until he entered in correct order. I am using label with goto statement for checking the array, if some element smaller than previous element is found, then we will go at the top of the for loop through goto statement..as shown in code below.

            //receiving array 1 input from user dynamically...
        cout << "How many numbers you want to enter for array 1" << endl;
        cin >> arr1_size;
        arr1 = new int [arr1_size]; //creating array dynamically
        arr1_label: //goto label
        cout << "Enter numbers " << endl; //just to display 
        for (int i = 0; i < arr1_size; i++) { //loop for taking value from the user for arr1
            cin >> arr1[0];
        }
        for (int i = 1; i < arr1_size; ) {//loop for checking for unordered array, if detects restart the input for loop
            if (arr1[i--] > arr1[i]) {
                cout << "Array 1 is not sorted" << endl;
                goto arr1_label;
                break;
            }
            i++;
        }

It's a task in which I can't use vector ... I only have to use arrays dynamically.....Complete code of project is given below..

int main()
{

        int arr1_size, arr2_size, final_arr_size;
        int* arr1; int* arr2; int* final_array;

        //receiving array 1 input from user dynamically...
        cout << "How many numbers you want to enter for array 1" << endl;
        cin >> arr1_size;
        arr1 = new int [arr1_size]; //creating array dynamically
        arr1_label: //goto label
        cout << "Enter numbers " << endl; //just to display 
        for (int i = 0; i < arr1_size; i++) { //loop for taking value from the user for arr1
            cin >> arr1[0];
        }
        for (int i = 1; i < arr1_size; ) {//loop for checking for unordered array, if detects restart the input for loop
            if (arr1[i--] > arr1[i]) {
                cout << "Array 1 is not sorted" << endl;
                goto arr1_label;
                break;
            }
            i++;
        }
        
            
        
        //receiving array 2 input from user dynamically...
        cout << "How many numbers you want to enter for array 2" << endl;
        cin >> arr2_size;
        arr2 = new int[arr2_size];
        cout << "Enter numbers " << endl;
        for (int i = 0; i < arr2_size; i++) {
            cin >> arr2[i];
        }



        //Merged array code here...
        final_arr_size = arr1_size + arr2_size;
        final_array = new int[final_arr_size];
        int i = 0, j = 0, k = 0;
        while (i < arr1_size && j < arr2_size) {
            if (arr1[i] < arr2[j]) {
                final_array[k++] = arr1[i++];
            }
            else {
                final_array[k++] = arr2[j++];
            }
        }

        while (i < arr1_size) {
            final_array[k++] = arr1[i++];
        }

        while (j < arr2_size) {
            final_array[k++] = arr2[j++];
        }


        //displaying final array
        for (int i = 0; i < final_arr_size; i++) {
            cout << final_array[i] << " ";
        }

        //deleting dynamically allocated memory.
        delete[] arr1;
        delete[] arr2;
        delete[] final_array;

    
    return 0;
}

Solution

  • #include <algorithm>
    // ...
    // check if arr1 is sorted
    bool input_is_sorted = std::is_sorted(arr1, arr1 + arr1_size); 
    

    See https://en.cppreference.com/w/cpp/algorithm/is_sorted

    Of course you would better replace bare pointers with std::vector etc.