Click here to Skip to main content
15,942,847 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a program in c++. The last column of output does not wrap. Allows users to input 15 integers (including positive and negative integers) through the keyboard. The program sorts the input numbers (from small to large) and prints the results. However, the sorting rules are slightly adjusted as follows: (1) All even numbers must be sorted before all odd numbers (2) All even numbers and all odd numbers are sorted from small to large (3) If the input contains 0, 0 will be sorted last The non-zero numbers in front and after are sorted according to the aforementioned rules. For example, the input numbers are in order: 51 -4 0 24 -13 -6 0 38 -27 -48 3 17 8 0 21. Then the sorted result is: 0 0 0 -48 -6 -4 8 24 38 -27 -13 3 17 21 51 For parts that require user input, please print out the text message prompt before allowing the user to input. The program execution output screen is as follows ( means blank) Integer1:51 Integer2:-4 Integer3:0 Integer4:24 Integer5:-13 Integer6:-6 Integer7:0 Integer8:38 Integer9:-27 Integer10:-48 Integer11:3 Integer12: 17 Integer13:8 Integer14:0 Integer15:21 Result: 0□0□0□-48□-6□-4□8□24□38□-27□-13□3□17□21□51

What I have tried:

C++
#include <iostream>
using namespace std;

void sortArray(int arr[], int n) {
    int temp;
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

int main() {
    int nums[15];
    int sortedNums[15];
    int n = 0;

 
    for (int i = 0; i < 15; ++i) {
        cout << "Integer" << i + 1 << ": ";
        cin >> nums[i];
    }

    for (int i = 0; i < 15; ++i) {
        if (nums[i] == 0) {
            sortedNums[n++] = nums[i];
        }
    }

    for (int i = 0; i < 15; ++i) {
        if (nums[i] != 0 && nums[i] % 2 == 0) {
            sortedNums[n++] = nums[i];
        }
    }


    for (int i = 0; i < 15; ++i) {
        if (nums[i] % 2 != 0) {
            sortedNums[n++] = nums[i];
        }
    }

    // ?整?排序???行排序
    sortArray(sortedNums, n);

    // ?出?果
    cout << "Result: ";
    for (int i = 0; i < n; i++) {
        cout << sortedNums[i] << " ";
    }

    return 0;
}
Posted
Updated 29-Dec-23 5:46am
v3
Comments
Dave Kreskowiak 29-Dec-23 11:28am    
You seem to have forgotten to ask a question or describe a problem you're having.

If you just use a simple bubble sort on the input values first, then you will have all the numbers in order. So in your sample the sorted list is:
0 0 0 -48 -6 -4 8 24 38 -27 -13 3 17 21 51

So from that list it is a simple matter to split into two new lists. One containing all the even numbers, with the zeroes at the end, and one containing all the odd numbers.

Alternatively you could use the rules in your sort routine to do what the question says.
 
Share this answer
 
In C++, it would be advisable to use the STL function std::sort to sort a vector of integers with user-defined sorting criteria. This works best with a lambda function.
The order of conditions is important to ensure that the sorting rules are fulfilled according to the requirements of the task. In ascending sorting, the value considered "smaller" is placed at the front. Returning false means that the relative order is maintained.

C++
std::vector<int> numbers = { 51, -4, 0, 24, -13, -6, 0, 38, -27, -48, 3, 17, 8, 0, 21 };

// Sorting according to the special rules
std::sort(numbers.begin(), numbers.end(), [](int a, int b) {

  // If both a and b are equal, maintain the relative order.
  if (a == b) 
     return false;

  // 2. If only a is 0, ensure that a comes before other numbers.

  // 3. If only b is 0, ensure that b comes before other numbers.

  // If both numbers are even or both are odd, maintain the order between a and b.
  // 4. If a is even and b is odd, place a before b (true).

  // 5. If a is odd and b is even, place a after b (false).

  // If none of the special cases apply, use normal ascending sorting.
  return a < b;
}); 
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900