我是计算机科学专业的学生。这是我为数据结构和算法类完成的一些代码。它编译得很好,运行正常,但是我用一个创可贴修正了它的错误。我希望得到一个如何以正确的方式解决问题的答案,以便将来我知道如何做到这一点。
作业的目的是创建二进制搜索。我创建了一个我创建的程序,它使用堆排序并添加了二进制搜索。我使用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;
}
答案 0 :(得分:1)
int heapList[MAXSIZE];//initialized heapList to the maximum size, currently 100
此评论错误 - heapList
数组宣布 ,因此当您从文件中读取所有数据时,索引变量{{1}将指向未初始化的单元格。任何使用它的尝试都将调用未定义的行为。您可以:在使用数组之前初始化数组,递减n
值,因为它比读数值大1,或者更好地使用n
而不是数组。
答案 1 :(得分:1)
您只为heapsort
索引0
填充值n-1
。
然后,您从heaplist
访问1
n
,因为heapsort[n]
没有输入任何值,因此超出范围。
使用
for (int i = 0; i < n; i++) //instead of i=1 to n