C++ 二进制搜索数组溢出c++;
我是计算机科学专业的学生。这是我为我的数据结构和算法类完成的一些代码。它编译精细,运行正确,但有一个错误,我纠正了创可贴。我希望得到一个答案,如何以正确的方式修复它,以便在未来,我知道如何正确地做这件事 分配的目的是创建一个二进制搜索。我使用了我创建的一个使用堆排序的程序,并添加了一个二进制搜索。我使用Visual Studio作为编译器 我的问题是,我选择将值从文本文件读入数组。文本文件中的每个整数由一个选项卡式空格分隔。在第98行中,文件读取正确,但当我到达文件中的最后一项时,计数器(n)计数过多,并为数组中的索引分配一个大的负数(因为数组溢出),这会导致堆排序从一个我不需要的非常大的负数开始。我通过将数组中的最后一个点指定为数组中的第一个点来放置一个创可贴。我将读取的数字与我的文件进行了比较,每个数字都在那里,但是大的数字消失了,所以我知道它是有效的。这不是一个适合我的修复,即使程序运行正确。我想知道是否有人知道一个正确的解决方案,它可以遍历我的文件,将每个整数分配给数组中的一个点,但不会使数组溢出 以下是整个计划:C++ 二进制搜索数组溢出c++;,c++,arrays,binary,C++,Arrays,Binary,我是计算机科学专业的学生。这是我为我的数据结构和算法类完成的一些代码。它编译精细,运行正确,但有一个错误,我纠正了创可贴。我希望得到一个答案,如何以正确的方式修复它,以便在未来,我知道如何正确地做这件事 分配的目的是创建一个二进制搜索。我使用了我创建的一个使用堆排序的程序,并添加了一个二进制搜索。我使用Visual Studio作为编译器 我的问题是,我选择将值从文本文件读入数组。文本文件中的每个整数由一个选项卡式空格分隔。在第98行中,文件读取正确,但当我到达文件中的最后一项时,计数器(n)计
#include "stdafx.h"
#include <iostream>
#include <fstream>
using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
#define MAXSIZE 100
void heapify(int heapList[], int i, int n) //i shows the index of array and n is the counter
{
int listSize;
listSize=n;
int j, temp;//j is a temporary index for array
temp = heapList[i];//temporary storage for an element of the array
j = 2 * i;//end of list
while (j <= listSize)
{
if (j < listSize && heapList[j + 1] > heapList[j])//if the value in the next spot is greater than the value in the current spot
j = j + 1;//moves value if greater than value beneath it
if (temp > heapList[j])//if the value in i in greater than the value in j
break;
else if (temp <= heapList[j])//if the value in i is less than the value in j
{
heapList[j / 2] = heapList[j];//assigns the value in j/2 to the current value in j--creates parent node
j = 2 * j;//recreates end of list
}
}
heapList[j / 2] = temp;//assigns to value in j/2 to i
return;
}
//This method is simply to iterate through the list of elements to heapify each one
void buildHeap(int heapList[], int n) {//n is the counter--total list size
int listSize;
listSize = n;
for (int i = listSize / 2; i >= 1; i--)//for loop to create heap
{
heapify(heapList, i, n);
}
}
//This sort function will take the values that have been made into a heap and arrange them in order so that they are least to greatest
void sort(int heapList[], int n)//heapsort
{
buildHeap(heapList, n);
for (int i = n; i >= 2; i--)//for loop to sort heap--i is >= 2 because the last two nodes will not have anything less than them
{
int temp = heapList[i];
heapList[i] = heapList[1];
heapList[1] = temp;
heapify(heapList, 1, i - 1);
}
}
//Binary search
void binarySearch(int heapList[], int first, int last) {//first=the beginning of the list, last=end of the list
int mid = first + last / 2;//to find middle for search
int searchKey;//number to search
cout << "Enter a number to search for: ";
cin >> searchKey;
while ((heapList[mid] != searchKey) && (first <= last)) {//while we still have a list to search through
if (searchKey < heapList[mid]) {
last = mid - 1;//shorten list by half
}
else {
first = mid + 1;//shorten list by half
}
mid = (first + last) / 2;//find new middle
}
if (first <= last) {//found number
cout << "Your number is " << mid << "th in line."<< endl;
}
else {//no number in list
cout << "Could not find the number.";
}
}
int main()
{
int j = 0;
int n = 0;//counter
int first = 0;
int key;//to prevent the program from closing
int heapList[MAXSIZE];//initialized heapList to the maximum size, currently 100
ifstream fin;
fin.open("Heapsort.txt");//in the same directory as the program
while (fin >> heapList[n]) {//read in
n++;
}
heapList[n] = heapList[0];
int last = n;
sort(heapList, n);
cout << "Sorted heapList" << endl;
for (int i = 1; i <= n; i++)//for loop for printing sorted heap
{
cout << heapList[i] << endl;
}
binarySearch(heapList, first, last);
cout << "Press Ctrl-N to exit." << endl;
cin >> key;
}
#包括“stdafx.h”
#包括
#包括
使用std::cout;
使用std::cin;
使用std::endl;
使用std::ifstream;
#定义最大尺寸100
void heapify(int heapList[],int i,int n)//i表示数组的索引,n表示计数器
{
int列表大小;
listSize=n;
int j,temp;//j是数组的临时索引
temp=heapList[i];//数组元素的临时存储
j=2*i;//列表结束
while(j heapList[j])//如果下一个点的值大于当前点的值
j=j+1;//如果值大于其下方的值,则移动该值
if(temp>heapList[j])//如果i中的值大于j中的值
打破
else if(temp=1;i--)//for循环创建堆
{
heapify(heapList,i,n);
}
}
//此排序函数将把已生成的值放入堆中,并按顺序排列,使它们从最小到最大
void排序(int-heapList[],int-n)//heapsort
{
buildHeap(heapList,n);
for(int i=n;i>=2;i--)//for对堆进行排序的循环--i为>=2,因为最后两个节点的值不会小于它们
{
int temp=heapList[i];
heapList[i]=heapList[1];
heapList[1]=温度;
heapify(heapList,1,i-1);
}
}
//二进制搜索
void binarySearch(int-heapList[],int-first,int-last){//first=列表的开头,last=列表的结尾
int mid=first+last/2;//查找中间位置进行搜索
int searchKey;//要搜索的编号
cout>搜索键;
while((heapList[mid]!=searchKey)&&(first)
此注释错误-heapList
数组被声明为未初始化,因此当您从文件中读取所有数据时,索引变量n
将指向未初始化的单元格。任何使用它的尝试都将调用未定义的行为。您可以选择:在使用数组之前初始化它,减小n
值,因为它大于读取值1,或者最好使用std::vector
而不是数组
此注释错误-heapList
数组被声明为未初始化,因此当您从文件中读取所有数据时,索引变量n
将指向未初始化的单元格。任何使用它的尝试都将调用未定义的行为。您可以选择:在使用数组之前初始化它,减小n
值,因为它比读取值大一倍,或者最好使用std::vector
而不是数组。仅为索引0
到n-1
填充heapsort
的值
然后从1
到n
访问heaplist
,这是超出范围的,因为heapsort[n]
中没有任何值
使用
for(int i=0;i
您只为索引0
到n-1
填充heapsort
的值
然后从1
到n
访问heaplist
,这是超出范围的,因为heapsort[n]
中没有任何值
使用
for(int i=0;i
您应该添加单元测试来验证您的程序。您应该访问heaplist[0]
到heaplist[n-1]
仅在何处设置该访问权限?一旦我们进入我的排序功能,就为时已晚,因为大量的数字被移动到了另一个位置。您应该添加单元测试来验证您的程序。您应该访问heaplist[0]
到heaplist[n-1]
仅建议将该访问权限放在何处?一旦我们进入我的排序函数,就为时已晚,因为大量的数字会被移动到另一个位置。虽然从技术上讲是正确的,但这忽略了情况的重点-好吧,heapList开始时未初始化,但在循环装备中填充了相关值ht之后。重点是,与他实际读取的内容相比,他在最后访问了一个额外的元素。如果数组被初始化(比如说)为零,他仍然会有一个伪值,只有零而不是随机垃圾。没错。我试过了,但我仍然得到了一个大的数字,因为无论发生什么错误,输入中都会发生错误数组开始时为。虽然技术上正确,但这忽略了情况的要点-好的,heapList
开始取消初始化
int heapList[MAXSIZE];//initialized heapList to the maximum size, currently 100
for (int i = 0; i < n; i++) //instead of i=1 to n