C++ 主引擎从不运行?气泡排序&;选择排序
我似乎没有任何产出,不幸的是,我的老师不能帮助我。我使用的是xcode,之前为了让程序正常运行,我们不得不改变很多事情,这次我什么都做不了。起初,我只有一个带有六个数字的冒泡排序,并让它运行,但我们随后添加了一个选择排序,而不是数字,它们是从txt文件中读取的C++ 主引擎从不运行?气泡排序&;选择排序,c++,xcode,C++,Xcode,我似乎没有任何产出,不幸的是,我的老师不能帮助我。我使用的是xcode,之前为了让程序正常运行,我们不得不改变很多事情,这次我什么都做不了。起初,我只有一个带有六个数字的冒泡排序,并让它运行,但我们随后添加了一个选择排序,而不是数字,它们是从txt文件中读取的 #include <iostream> #include <string> #include <fstream> #include <cstdlib> using namespac
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
void bubble(string filename);
void selectionSort(string filename);
const int SIZE = 100000;
int values[SIZE];
// Main
int main()
{
cout << "Beginning set of our bubble sorts. Please stand by... " << endl;
bubble("inOrder.txt");
bubble("revOrder.txt");
bubble("ranOrder1.txt");
bubble("ranOrder2.txt");
cout << "Finished with four bubble sorts. " << endl;
system("PAUSE");
return 0;
}
// Functions
void bubble(string filename)
{
unsigned long int passCount = 0, compCount = 0, swapCount = 0;
int temp;
bool swap;
ifstream fin(filename.c_str());
for ( int j = 0; j < SIZE; j++)
fin >> values[j];
do
{ passCount++;
swap = false;
for (int count = 0; count < (SIZE -1); count++)
{
compCount++;
if(values[count] > values[count + 1])
{
swapCount++;
temp = values[count];
values[count] = values[count + 1];
values[count + 1] = temp;
swap = true;
}
}
} while (swap);
string outputName = "Bubble" + filename;
ofstream fout(outputName.c_str());
for ( int j = 0; j < SIZE; j++)
fout << values[j] << endl;
cout << "The number of passes is: " << passCount << endl;
cout << "The number of comparious is:" << compCount << endl;
cout << "The number of swaps is: " << swapCount << endl;
}
// Selection
void selectionSort(int array[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (SIZE - 1); startScan++)
{
minIndex = startScan;
minValue = values[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (values[index] < minValue)
{
minValue = values[index];
minIndex = index;
}
}
values[minIndex = array[startScan]];
values[startScan] = minValue;
}
}
#包括
#包括
#包括
#包括
使用名称空间std;
无效气泡(字符串文件名);
void selectionSort(字符串文件名);
常数int SIZE=100000;
int值[大小];
//主要
int main()
{
cout我以前在使用Xcode打开文件时也遇到过这个问题。我发现,如果您没有为尝试打开的txt文件指定完整的文件路径,Xcode将按预期检查项目文件夹。问题是,您必须将txt文件添加到Xcode项目中,而不只是将其放在Finder中的项目文件夹中
在Xcode中,转到“文件>添加文件”并将您试图打开的txt文件添加到项目中。我首先要确保您的文件已正确打开。从目录运行的Xcode并不像看上去那么简单。它通常在某个临时生成文件夹中处于关闭状态。如果您确定这是问题所在,可以对其进行更改(除非你知道这一点,并且已经在你的模式配置中解决了它)。你确定什么都没有发生吗?我本来希望得到一些编译器错误,告诉我关于伪名称数组
(你说你已经修复了),缺少]
,可能还有系统
,因为你没有包含
。请注意系统(“暂停”)
在非Microsoft平台上可能不会执行任何操作,但只有在使用邪恶的IDE时才需要它,该IDE会在程序退出后立即关闭程序的输出窗口。为什么不简单地通过1.添加一些正确的错误处理和2.单步执行代码来调试问题?环境是什么?const int SIZE=100000;int v值[SIZE];太多了?您确定它可以编译吗?您提供的列表中有错误(在array[minIndex=array[startScan];
)和未声明的标识符(bubble
函数中的数组)。
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
const int maxSize = 100000;
int main()
{
ofstream fout1("inOrder.txt");
ofstream fout2("revOrder.txt");
ofstream fout3("ranOrder.txt");
ofstream fout4("ranOrder.txt");
cout << "Starting to create 4 output files. . ." << endl;
for (int i = 1; i <= maxSize; i++)
{
fout1 << i << endl;
fout2 << maxSize - i + 1 << endl;
fout3 << rand() << endl;
fout4 << rand() << endl;
}
cout << "Finished creating 4 output files . . ." << endl;
system( "PAUSE");
return 0;
}