中止陷阱:C 中的6个错误
Abort trap :6 Error in C++
我是编程的新手,所以如果问题听起来很琐碎,请原谅我。您的任何建议将对我的学习有很大的帮助。我正在编写大小10的阵列上的选择_sorting程序信息:排序的数组如下: 中止陷阱:6
我的问题是在程序中在哪里寻找问题,该问题发生了什么原因?
是什么原因?作为参考,我正在附上我的代码。
# include <iostream>
void fill_array(int sample_array[10], int size);
void sort(int sample_array[10], int size);
void swap(int &a1, int &a2);
int index_of_smallest(int array[10], int start_index, int size);
int main()
{
using namespace std;
int array[10];
fill_array(array, 10);
sort(array, 10);
cout << " The sorted array as follows : n";
for (int i = 0; i < 10; i++)
{
cout << array[i] << " ";
}
return 0;
}
void fill_array(int sample_array[10], int size)
{
using namespace std;
for (int index = 0; index< 10; index++)
{
cin >> sample_array[index];
}
}
void swap(int &a1, int &a2)
{
int temp;
temp = a1;
a1 = a2;
a2 = temp;
}
int index_of_smallest(int array[10], int start_index, int size)
{
int min = array[start_index];
int min_index = start_index;
for (int i = start_index + 1; i< size - 1; i++)
{
if (array[i]< min)
{
min = array[i];
min_index = i;
}
}
return min_index;
}
void sort(int sample_array[10], int size)
{
int next_min_index;
int j;
for (j = 0; j < size; j++)
{
next_min_index = index_of_smallest(sample_array, j, 10);
}
swap(sample_array[j], sample_array[next_min_index]);
}
我决定破译最有可能故障的一个函数。我在这里重新格式化它是可读的:
void sort( int sample_array[10], int size )
{
int next_min_index;
int j;
for( j = 0; j < size; j++ )
{
next_min_index = index_of_smallest( sample_array, j, 10 );
}
swap( sample_array[j], sample_array[next_min_index] );
}
现在,希望您能看到问题。
互换不会发生在循环内。您将j
的定义移出了循环范围(大概是为了修复您不了解的编译错误,这将使您指出问题)。
当j == 10
时,互换正在发生。那在您的阵列界限和程序包的范围之外。如果将功能更改为此:
void sort( int sample_array[10], int size )
{
for( int j = 0; j < size; j++ )
{
int next_min_index = index_of_smallest( sample_array, j, 10 );
swap( sample_array[j], sample_array[next_min_index] );
}
}
这可能不是唯一的问题,但是我不会破译您的其余代码。希望此修复程序加上一些强烈的鼓励使用人类可读的代码布局将帮助您沿途。
相关文章:
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 如何在C++中初始化嵌套类中的2个memeber
- 如果中的多个语句是否与多个 if 相同?
- 嵌套在循环中的两个循环的 big-O 表示法
- 在 c++ 中对单个排序数组中的 2 个未排序数组进行排序
- 我们是否需要为 C++ 中的多个函数初始化多个模板?
- 即使我没有包含多个文件,C++中的多个定义错误
- C++递归来决定数组中的两个值
- 删除功能不适用于串行通信后多个循环中的多个实例
- 我们如何在文本文件中找到C++中的几个单词?
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- 使用XOR查找O(n)-解决方案中的两个字符串是否为变位符
- VTK 图像数据,访问数组中的 1 个图像
- 我应该如何在没有变量的情况下将相同的参数传递给 CMAKE 中的多个目标?
- 有没有更好的方法对C++中的三个整数进行排序?
- 如何使用循环逐个读取C++中的多个文件
- 使用类 ?C++ 和 SFML 以及标题中的 15 个字符
- 为什么我不能在同一 MS VS 解决方案中的两个控制台应用中使用C++特征(仅标头库)?
- 如何为多个目录中的多个文件创建生成文件?
- 如何忽略输入中的 N 个整数