【问题标题】:Dynamic Arrays and user input c++动态数组和用户输入 c++
【发布时间】:2018-02-08 17:30:16
【问题描述】:

我正在为我的 C++ 类编写一个程序,它接受用户输入的 int 和 char 数组的大小,用随机值(数字 0-100,字母 AZ)填充数组,然后排序、反转和显示两个数组。

该程序在大多数情况下都可以正常工作,并且我理解我在这里使用的逻辑,但是...

多次运行和调试代码后。我注意到,当数组填充值时,第一个元素,即使它实际上被赋予了一个值,它也不会以升序打印分配的值,而是以降序打印?我完全不明白。

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

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;
}

【问题讨论】:

  • 因为这只是一个简短的作业,所以不会太激烈,但对于未来,请避免using namespace std;system("cls")system("pause")
  • @Folling My Prof 让我们使用“using namespace std;”为什么这是个坏主意?
  • 如果你想要一个动态数组,使用std::vector
  • 为什么在调用rand 之后有srand(time(null)) 而之前没有?另外,那种(应该是那种,对吧?)有点奇怪,你不是说for(int j = i + 1 ; j &lt; a ; ++j)吗?

标签: c++ function templates printing dynamic-arrays


【解决方案1】:

你在这里使用完整的长度:

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 &gt; 0; j--) {

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

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

作为旁注,您可以在 reversefor 循环内和 sortif 内声明 Temp t,因为它仅用于这些范围。

编辑: 我也忽略了,sort 你需要改变

j&gt;0

j &gt;= 0

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

【讨论】:

  • 谢谢我现在明白我的逻辑哪里错了。
【解决方案2】:

你在几个地方有一个错误。

    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.
    }
}

【讨论】:

  • @eM3e,我不明白那个评论。
  • @ R Sahu 我的原始问题是第一个元素不会按升序打印,而是按降序打印。现在它不会按降序打印第一个元素,而是按升序打印...
【解决方案3】:

您正在使用 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 2022-01-21
    • 1970-01-01
    • 2018-03-19
    相关资源
    最近更新 更多