简体   繁体   English

按升序对次对角线的元素进行排序 C++

[英]Sort elements of secondary diagonal in ascending order C++

As I think, I need to transform function printSecondaryDiagonal (that actually print elements of secondary diagonal) into one-dimensional array and then sort its elements in ascending order, right?正如我所想,我需要将 function printSecondaryDiagonal (实际上打印辅助对角线的元素)转换为一维数组,然后按升序对其元素进行排序,对吗?

PS Two-dimensional array in the beginning must be necessarily a dynamic one. PS 一开始的二维数组一定是动态的。 Also, cannot do it using vector .此外,不能使用vector来做到这一点。 Only malloc , calloc and new只有malloc , callocnew

#include <iostream>
#include <iomanip>
using namespace std;

void getManual(int** arr, int rows, int columns);
void getRandom(int** arr, int rows, int columns);
void printSecondaryDiagonal(int** arr, int rows, int columns);

void main() {
    int rowCount = 5;
    int colCount = 6;

    cout << "Enter quantity of rows: ";
    cin >> rowCount;
    cout << "Enter quantity of columns: ";
    cin >> colCount;

    int** arr = new int* [rowCount];

    for (int i = 0; i < rowCount; i++) {
        arr[i] = new int[colCount];
    }

    cout << " Array formation algorithm\n";
        start:
    cout << "Input number : \n1 for manual\n2 for random\n";
    int k;
    cin >> k;
    switch (k) {
    case 1: getManual(arr, rowCount, colCount);
        break;
    case 2: getRandom(arr, rowCount, colCount);
        break;
    default:cout << "Input 1 or 2, please.";
        cout << endl << endl;
        goto start;
    }
    cout << endl;

    printSecondaryDiagonal(arr, rowCount, colCount);
        
    for (int i = 0; i < rowCount; i++) { //очищуємо память для кожного рядка
        delete[] arr[i];
    }

    delete[] arr;
}

void getManual(int** arr, int rows, int columns) { //введення з клавіатури
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            cout << "a[" << i << "][" << j << "]=";
            cin >> arr[i][j];
            //cin >> *(*(arr + i) + j); //вказівникова форма
        }
    }
}

void getRandom(int** arr, int rows, int columns) { //випадкова генерація чисел
    int lowest = -21, highest = 34;
    int i, j;
    srand(time(NULL));
    // ініціалізація масива
    for (i = 0; i < rows; i++) {
        for (j = 0; j < columns; j++) {
            arr[i][j] = lowest + rand() % (highest - lowest + 1);
            cout << setw(7) << arr[i][j];
        }
        cout << endl;
    }
}

Function that I need to transform into one-dimensional array and which is the main problem for me: Function 我需要转换为一维数组,这对我来说是主要问题:

void printSecondaryDiagonal(int** arr, int rows, int columns) {
    
    cout << "Secondary Diagonal: ";
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {

            // Condition for secondary diagonal
            if ((i + j) == (columns - 1)) {
                cout << arr[i][j] << setw(7);
            }
        }
    }
    
}

The elements of the secondary diagonal can be extract with one for loop as I will show.正如我将展示的那样,可以使用一个 for 循环提取次对角线的元素。 The secondary diagonal will be save in an 1-d array `secDiag[i]`.次对角线将保存在一维数组 `secDiag[i]` 中。 Then, using `std::sort` in `algorithm` head file to sort this array in ascending order.然后,使用 `algorithm` 头文件中的 `std::sort` 对这个数组进行升序排序。

void printSecondaryDiagonal(int** arr, int rows, int columns) {

    cout << "Secondary Diagonal: ";
    int *secDiag = new int [rows];
    int r, c;
    for (r = 0; r < rows ; r++) {
              c = columns - r -1;
             if (c < 0) break;
             secDiag[r] = arr[r][c];
        }
    for (int i =0; i<r; i++) std::cout << setw(7) << secDiag[i];
    std::cout << std::endl;
    std::cout << "After sorted: ";
    std::sort(secDiag, secDiag+r);
    for (int i =0; i<r; i++) std::cout << setw(7) << secDiag[i];
    std::cout << std::endl;
    delete [] secDiag;
}

A test run:试运行:

Enter quantity of rows: 3
Enter quantity of columns: 3
 Array formation algorithm
Input number :
1 for manual
2 for random
2
     33    -13     29
     -7     -2     10
     -8     18      6

Secondary Diagonal:      29     -2     -8
After sorted:      -8     -2     29
  void printSecondaryDiagonal(int** arr, int rows, int columns) {
        
    cout << "Secondary Diagonal: ";
    int i = 0;
    int j = columns - 1;
    int k = 0;
    int size =0;
    if (rows>columns)
    {
      size = columns;
    }
    else 
    {
      size = rows;
    }
    int *diagonal = new int[size];
    while (i < rows && j >= 0)
    {
      diagonal[k] = arr[i][j];

      cout << arr[i][j] << setw (7);
      i++;
      j--;
      k++;
    }
    for (int i = 0; i < size - 1; i++)
    {
      for (int j = 0; j < size - i - 1; j++)
        {
          if (diagonal[j] > diagonal[j + 1])
           {
            // swap arr[j+1] and arr[j] 
            int temp = diagonal[j];
            diagonal[j] = diagonal[j + 1];
            diagonal[j + 1] = temp;
           }
         }
      }
    for (int r = 0; r < size; r++)
    {
      cout << diagonal[r] << endl;
    }
    delete [] diagonal;        
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM