为什么我的程序在使用不同的编辑器和编译器时会有所不同?
问题描述:
该程序应该询问用户设置的数值,然后再次询问用户的值,对它们进行排序,然后用户将输入一个值。程序应该搜索这个值,并返回值的位置或告诉用户它不是他给出的列表的一部分。为什么我的程序在使用不同的编辑器和编译器时会有所不同?
我用记事本++和pocketC++来运行和编译整个程序,然后我调用了这个UNIX编辑器,putty和g ++,这是我遇到问题的地方。当我使用putty和g ++时,程序编译得很好,但只会返回中间排序的值。例如,我想要3个数字,1 2 3.然后,我询问2是哪个程序,它返回2在第二个位置,但其他任何数字都会给我一个垃圾值。这只适用于putty和C++,而pocketC++可以处理所有的值。
#include <iostream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
char answer;
int list,target, first, last;
int *array;
int search(int , int , int , int);
int sort(int a[], int);
int index_of_smallest(const int array[], int first, int target);
void swap(int& v1, int& v2);
int search(int data[], int target, int first, int last);
cout << "How many integers does your list have?\n";
cin >> list;
array = new int[list];
cout << "Please enter your integers: ";
for (int i = 0; i < list; i++)
cin >> array[i];
do {
cout << "\nWhat is the target value?\n";
cin >> target;
sort(array,list);
first = 0;
last = list-1;
int k= search(array, target, first, last);
if (k == -1) cout << "That number is not a part of your list of integers.\n";
else cout << "The location of " << target << " is spot " << k+1 << endl;
cout << "Would you like to search a different number?" << endl;
cin >> answer;
} while (answer !='n' && answer !='N');
return 0;
}
int index_of_smallest(const int array[], int first, int target){
int min= array[first];
int index_min=first;
for (int i = first + 1; i < target; i++)
if (array[i] < min) {min = array[i];
index_min = i;}
return index_min;
}
void swap(int& v1, int& v2){
int temp;
temp = v1;
v1=v2;
v2=temp;
}
void sort(int a[], int num){
int next_smallest;
for (int i= 0; i < num - 1; i++)
{
next_smallest =index_of_smallest(a, i, num);
swap (a[i],a[next_smallest]);
}
}
int search(int data[], int target, int first, int last) {
int middle;
if (first > last)
return -1;
else {
middle=(first + last)/2;
if (target == data[middle])
return middle;
else if (target < data[middle])
search(data, target, first, middle-1);
else if (target > data[middle])
search(data, target, middle +1, last);}
}
答
在简单地查看代码时,没有什么比这更奇怪的了。话虽这么说,如果我通过编译器运行代码,以下警告出来:
[11:04pm][[email protected] /tmp] g++ -Wall foo.cc
foo.cc: In function ‘int search(int*, int, int, int)’:
foo.cc:76: warning: control reaches end of non-void function
所以我们看一下搜索功能。它看起来像你预期的功能是递归的,所以我相信你应该有更多的回报语句写它:
int search(int data[], int target, int first, int last) {
int middle;
if (first > last)
return -1;
else {
middle=(first + last)/2;
if (target == data[middle])
return middle;
else if (target < data[middle])
return search(data, target, first, middle-1);
else if (target > data[middle])
return search(data, target, middle +1, last);
}
}
+0
这是我的问题!我忘了我的回报!谢谢!!感谢所有帮助过的人! – 2013-05-09 04:12:43
如果不同的编译器提供不同的结果,它可能是你调用未定义行为(例如,在你没有分配的内存中取消引用指针,在缓冲区溢出或溢出等),但是编译器可以根据需要编码,但实际上结果将取决于他们如何决定将堆栈中的内存打包在一起和堆 - 这将从编译到编译有所不同。 – Patashu 2013-05-09 03:49:26
是否应该在主函数中放置函数声明? – olevegard 2013-05-09 03:55:56
您需要使用更传统且一致的缩进样式来改进代码的格式,供其他人阅读。同样,在C++中,没有必要或没有理由在块的开头声明每个变量。避免使用'namespace std',特别是当你使用名为'array'的变量时''std''中的一个类型的名字。当你没有编写低级内存管理类时,调用'new'和'delete'是内存损坏的常见原因。函数前向声明通常放在现代C++的函数之外。请使用http://sscce.org/。 – Yakk 2013-05-09 03:56:21