动态数组和用户输入C

Dynamic Arrays and user input c++

本文关键字:输入 用户 数组 动态      更新时间:2023-10-16

我正在为我的C 类编写一个程序,该程序将用户输入以int和char数组的大小为输入,以随机值填充数组(数字0-100,字母A-Z(然后对两个数组进行分类,逆转和显示。

在大多数情况下,程序可以工作,我了解我在这里使用的逻辑,但是...

多次运行并调试代码后。我注意到,当数组被填充值时,即使实际上给出了一个值,它也不会以升序订单给出的分配值,而是按降序降序?我一点都不明白。

注意:我必须使用模板函数进行排序,反向和显示数组。

template <class T> 
void sort(T *arr, int a) {
    T temp;
    for (int i = 0; i < a; i++) {
        for (int j = a; j > 0; j--) {
            if (arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
 }
template <class T>
void reverse(T *arr, int a) {
    T temp;
    for (int i = 0; i < a / 2; i++) {
        temp = arr[i];
        arr[i] = arr[a - i];
        arr[a - i] = temp;
    }
}
template <class T>
void display(T *arr, int a) {
    for (int i = 0; i < a; i++) {
        cout << arr[i] << ", ";
    }
    cout << endl;
}
template<class T>
void save(T *arr, int a) {
    sort(arr, a);
    display(arr, a);
    reverse(arr, a);
    display(arr, a);
}
int main() {
    int x, y;
    cout << "Please enter a number for an array of data type "int"" << endl;
    cin >> x;
    cout << "Please enter a number for an array of data type "char"" << endl;
    cin >> y;
    int *arr1 = new int[x];
    char *arr2 = new char[y];
    for (int i = 0; i < x; i++) 
        cout << (arr1[i] = rand() % 100 + 1);
    srand(time(nullptr));
    for (int i = 0; i < y; i++)
        cout << (arr2[i] = rand() % 26 + 65);

    system("cls");
    save(arr1, x);
    save(arr2, y);
    delete[]arr1;
    delete[]arr2;
    system("pause");
    return 0;
}

您在此处使用完整的长度:

save(arr1, x);
save(arr2, y);

所以在reverse

arr[i] = arr[a - i]; arr[a - i] = temp;

您需要在长度上进行-1,否则您将在i == 0

时获得无效的索引

arr[i] = arr[a - 1 - i]; arr[a - 1 - i] = temp;

就像r sahu所说,在sort

for (int j = a; j > 0; j--) {

您需要-1,因为A是无效索引的长度。

for (int j = a-1; j > 0; j--) {

作为旁注,您可以在reverse中的for环内和sort中的CC_8循环内部声明Temp t,因为它仅在这些范围中使用。

编辑:我也忽略了,在sort中,您需要更改

j>0

to

j >= 0

这样,您也可以访问数组的第一个元素。

您在几个地方有一个错误的错误。

    for (int j = a; j > 0; j--) {

是不正确的。a是数组的无效索引。将该行更改为使用j = a-1

    for (int j = a-1; j > 0; j--) {

您在reverse中有类似的,逐一的错误。而不是

    arr[i] = arr[a - i];
    arr[a - i] = temp;

您需要使用:

    arr[i] = arr[a - i - 1];
    arr[a - i - 1] = temp;

您对sort的实现不正确。我不想在这里介绍算法细节,但是更改j的值的顺序似乎可以解决该问题。

for (int i = 0; i < a; i++) {
    for (int j = i+1 ; j < a ; j++) {
       // The swapping code.
    }
}

您使用的是o(n^2(时间复杂性的气泡排序。考虑使用更快的算法。如果您不想自己实现它,请使用sort((函数。它的复杂性是关于o(n log n(,这非常好。

http://www.cplusplus.com/reference/algorithm/sort/

#include <iostream>
#include <algorithm>
using namespace std;
bool comp(int i1, int i2) { // comp function is to compare two integers
    return i1 < i2;
}
int main() {
    int x[30];
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> x[i];
    }
    sort(x, x + n, comp); // if you don't provide comp function ( sort(x, x+n)     ), sort() function will use '<' operator
    for (int i = 0; i < n; i++) {
        cout << x[i] << " ";
    }
    return 0;
}