错误我得到了:“列表未在此范围内定义”[在main()内部] 我尝试了几件事但结束了失败。我已经阅读了一些范围教程并且没有找到任何内容:\,所以我来到这里,希望有人可以提供帮助。
[CODE:]
#include <iostream>
using namespace std;
const int MAX_LIST_SIZE = 100;
typedef int element;
typedef element list_type[MAX_LIST_SIZE];
void bubble_sort();
int main() {
cout << list[0] << list[1] << list[2] << list[3] << list[4] << endl;
return 0;
}
void bubble_sort(list_type list, int n) {
list[0] = 43;
list[1] = 20;
list[2] = 24;
list[3] = 31;
list[4] = 36;
n = 5;
int j, k;
bool exchange_made = true;
element temp;
k = 0;
// make up to n- 1 passes through array, exit
// early if no exchange are made on previous pass
while((k < n -1 ) && exchange_made) {
exchange_made = false;
++k;
// number of comparisons on kth pass
for (j = 0; j < n - k; ++j)
if(list[j] > list[j + 1]) {
// exchange must be made!
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
exchange_made = true;
}
}
}
答案 0 :(得分:1)
int main() {
cout << list[0] << list[1] << list[2] << list[3] << list[4] << endl;
return 0;
}
好list
仅在函数bubble_sort
中定义,因此知道main
可以访问它的方式。这是一个问题。这是让你入门的东西。
#include <iostream>
using namespace std;
const int MAX_LIST_SIZE = 100;
typedef int element;
typedef element list_type[MAX_LIST_SIZE];
void bubble_sort();
int main() {
bubble_sort(); //you need to actually CALL bubble_sort for it to do anything
return 0;
}
void bubble_sort() {
list_type list; //no need to have the variables as a parameter
int n = 5; //just declare them in the scope (inside the function) that you need them in
list[0] = 43;
list[1] = 20;
list[2] = 24;
list[3] = 31;
list[4] = 36;
int j, k;
bool exchange_made = true;
element temp;
k = 0;
// make up to n- 1 passes through array, exit
// early if no exchange are made on previous pass
while((k < n -1 ) && exchange_made) {
exchange_made = false;
++k;
// number of comparisons on kth pass
for (j = 0; j < n - k; ++j)
if(list[j] > list[j + 1]) {
// exchange must be made!
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
exchange_made = true;
}
}
cout << list[0] << list[1] << list[2] << list[3] << list[4] << endl;
}
答案 1 :(得分:0)
在您的代码中,您从未向主要块声明list
。错误消息清楚地告诉您问题。
答案 2 :(得分:0)
您应该定义list
。它没有在任何地方定义。
int arr[MAX_LIST_SIZE];