1

下面是我的代码,它不会排序,它应该排序!我需要从文件中读取数字作为向量。然后将其转换为数组并对数组进行排序。

有谁知道为什么?

using namespace std;

//function prototype
int readInput(vector<int> &);
void sort(vector<int>, int[], int);

int main()
{
  vector<int> values;
  int sum, avg;
  sum = readInput(values);

  const int SIZE = 1300;
  int arr[SIZE];

   // for (int count = 0; count < values.size(); count++)
   //   array[count] = values[count];


  sort(values, arr, SIZE);

  avg = sum / values.size();
  cout << "The average is: " << avg;

  return 0;
}

int readInput(vector<int> &vect)
{

  int count;
  int total = 0;

  ifstream inputFile("TopicFin.txt"); //open file

  if(!inputFile)
  {
    return 0; // if file is not found, return 0
  }

  while(inputFile >> count) //read file
    vect.push_back(count); //add to file

  for (int count = 0; count < vect.size(); count++)
    total+=vect[count]; //sum data in vector

  return total;

}

void sort(vector<int> values, int array[], int size)
{

  int startScan, minIndex, minValue;

  for (int count = 0; count < values.size(); count++)
    array[count] = values[count];

  for(startScan = 0; startScan < (values.size()-1); startScan++)
  {
    minIndex = startScan;
    minValue = array[startScan];
    for(int index = startScan + 1; index < values.size(); index++)
    {
      if (array[index] < minValue)
      {
        minValue = array[index];
        minIndex = index;
      }
    }

    array[minIndex] = array[startScan];
    array[startScan] = minValue;
  }
  for (int index = 0; index < values.size(); index++)
    cout << values[index] << " " << endl;
}
4

0 回答 0