动态数组和用户输入C
Dynamic Arrays and user input c++
我正在为我的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;
}
- C++如何通过用户输入删除列表元素
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 用户定义函数中的指针和输入
- 如何只允许用户输入正整数
- C++如何计算用户输入的数字中的偶数位数
- 通过for循环使用用户输入填充列表
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 在C++中,我如何接受不同于同一行的用户输入
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 当用户键入分隔符时,停止getline()输入
- 通过错误处理,在C++中可靠地获得用户十六进制输入
- 当用户在qtablewidget中输入单元格时,如何获得信号?C++
- 显示基于用户输入的整数的字符
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 如果用户没有输入任何内容或输入错误,如何重新输入用户的输入?
- 为什么在代码块中执行时会跳过输入用户输入的行?
- 使用 for 循环在数组中输入用户