分配大小无效

时间:2016-10-15 03:45:27

标签: c++

我正在为选择,插入和交换排序编写代码。

我从输入文本文件创建数组读取然后填充数组,然后对其进行排序。

当我在命令提示符下测试代码时,它工作得很好。但如果我在常规调试模式下测试它,我会收到一条错误消息

  

无效的分配大小:4294967295字节

什么是错的?

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int SWAP_COUNT;
int COMPARE_COUNT;
 bool compareSelectionElement(int i, int j);
 void displayArray (int a[], int s);
void copyArray(int * source, int * dest, int s);
void swapElement(int &i , int &j);
bool compareElement(int i , int j);
void exchangeSort(int *a, int length);
void reverseArray(int *a , int s);
void reverseArraySort(int *a, int s);
void insertionSort(int *a, int s);
void selectionSort(int * a, int s);
bool compareInsertionElement(int i, int j);

int main(int argc, int * argv[])
  {
int swapHolder;
string fileName,text, size;
fstream inText;
int lengthOf, total;
int * myArray;
if(argc < 2)
{
cout << "Please Enter An Input File Name: ";
getline(cin, fileName);
inText.open(fileName.c_str() , fstream::in);
if(!inText)
{
    cout << "Could Not Open File" << endl;
}
inText >> lengthOf;
myArray = new int[lengthOf];
for(int i = 0; i < lengthOf; i++)
{
    inText >> myArray[i];
}
cout << " \nData File Array " << endl;
displayArray(myArray,lengthOf);
int * copyArr = new int[lengthOf];
copyArray(myArray,copyArr, lengthOf);
exchangeSort(copyArr, lengthOf);
cout << " \nExchange Sort Array " << endl;
displayArray(copyArr,lengthOf);

cout << "Number Of Comparisons: " << COMPARE_COUNT << endl;
cout << "Number Of Exchanges: " << SWAP_COUNT << endl;

exchangeSort(copyArr, lengthOf);
cout << " \nThe now-sorted array again using exchange sort " << endl;
displayArray(copyArr,lengthOf);

cout << "Number Of Comparisons: " << COMPARE_COUNT << endl;
cout << "Number Of Exchanges: " << SWAP_COUNT << endl;

cout << "\nReversed Array " << endl;
reverseArray(copyArr,lengthOf);
cout << "\nSorted Reverse Array" << endl;
reverseArraySort(copyArr,lengthOf);
exchangeSort(copyArr, lengthOf);
displayArray(copyArr, lengthOf);
cout << "Number Of Comparisons: " << COMPARE_COUNT << endl;
cout << "Number Of Exchanges: " << SWAP_COUNT << endl;
//Insertion Sort
COMPARE_COUNT = 0;
SWAP_COUNT = 0;
int *copyArray2 = new int[lengthOf];
copyArray(myArray, copyArray2, lengthOf);
insertionSort(copyArray2,lengthOf);
cout << "  Insertion Sort Array " << endl;
displayArray(copyArray2,lengthOf);
cout << "Number Of Comparisons: " << COMPARE_COUNT << endl;
cout << "Number Of Exchanges: " << SWAP_COUNT << endl;
//Selection Sort
COMPARE_COUNT = 0;
SWAP_COUNT = 0;
int *copyArray3= new int[lengthOf];
copyArray(myArray, copyArray3, lengthOf);
selectionSort(copyArray3,lengthOf);
cout << "  Selection Sort Array " << endl;
displayArray(copyArray2,lengthOf);
cout << "Number Of Comparisons: " << COMPARE_COUNT << endl;
cout << "Number Of Exchanges: " << SWAP_COUNT << endl;
inText.close();
}
return 0;
}

void displayArray (int a[], int s)
{
if(s <= 200)
{
    for (int i = 0; i < s; ++i)
    {
        if(i%10 == 0)
        {
            cout << endl;
        }
        cout << setw(7) << a[i];
    }
    cout << endl;
}
else
{
    for(int  i = 0; i < 100; i++)
    {
        if(i%10 == 0)
        {
            cout << endl;
        }
        cout << setw(7) << a[i] << " ";
    }
    cout << endl;
    for (int i = s-100; i < s; ++i)
    {
        if (i%10 == 0)
        {
            cout << endl;
        }
        cout << setw(7) << a[i] << " ";
    }
    cout << endl;
}

}
bool compareElement(int i , int j)
{
++COMPARE_COUNT;
return i <= j;
}
bool compareInsertionElement(int i, int j)
{
++COMPARE_COUNT;
return i > j;
}
bool compareSelectionElement(int i, int j)
{
++COMPARE_COUNT;
return i > j;
  }
void swapElement(int &i, int &j)
{
++SWAP_COUNT;
int t = i;
i = j;
j = t;
}
void exchangeSort(int *a, int length)
{
for(int i = 0; i < length; i++)
{
    for(int index = 0; index < length - 1; index++)
    {
        if(!compareElement(a[index], a[index+1]))
        {
            swapElement(a[index],a[index +1]);
        }
    }
}
}
void copyArray(int *source, int * dest, int s)
{
for(int i = 0; i < s; i++)
{
    dest[i] = source[i];
}
}
void reverseArraySort(int *a, int s)
{
if(s <= 200)
{
    for(int i = s-1; i > 0; i--)
    {
        swapElement(a[i], a[i - 1]);
        //cout << setw(7) << a[i] << " ";

    }
    //cout << endl;
}
else
{
    for(int i = 100; i > 0; --i)
    {
        /*
        if(i%10 == 0)
        {
            cout << endl;
        }
        */
        swapElement(a[i], a[i-1]);
        //cout << setw(7) << a[i] << " ";
    }
    //cout << endl;
    for(int y = s -1; y >  s - 100; --y)
    {
        /*
        if(y%10 == 0)
        {
            cout << endl;
        }
        */
        swapElement(a[y], a[y-1]);
        //cout << setw(7) << a[y] << " ";
    }
    //cout << endl;
}
}

void reverseArray(int *a, int s)
{
if(s <= 200)
{
    for(int i = s; i > 0; i--)
    {
        //swapElement(a[i], a[i - 1]);
        if(i%10 == 0)
        {
            cout << endl;
        }
        cout << setw(7) << a[i - 1] << " ";
    }
    cout << endl;
}
else
{
    for(int i = 100; i > 0; --i)
    {

        if(i%10 == 0)
        {
            cout << endl;
        }

        //swapElement(a[i], a[i-1]);
        cout << setw(7) << a[i - 1] << " ";
    }
    cout << endl;
    for(int y = s; y >  s - 100; --y)
    {
        if(y%10 == 0)
        {
            cout << endl;
        }

        //swapElement(a[y], a[y-1]);
        cout << setw(7) << a[y - 1] << " ";
    }
    cout << endl;
}
}
void insertionSort(int * a, int s)
{
int j;
for(int i = 1; i < s; i++)
{
    j = i;
    while(j > 0 && compareInsertionElement(a[j-1],  a[j]))
    {
        swapElement(a[j], a[j-1]);
        j--;
    }
}
}
void selectionSort(int *a, int s)
{
for(int i = 0 ; i < s - 1; ++i)
{
    int smallest = i;
    for(int j = i + 1; j < s; ++j)
    {
        if(!compareSelectionElement(a[smallest], a[j]))
        {
            smallest = j;
            swapElement(a[smallest],a[i]);
        }
    }
}
}

0 个答案:

没有答案
相关问题