I need to write a program that reads an unknown number of numerical data from a text file into an array and then uses a sorting parameter to sort the numbers. I've been trying for hours and I can't get the program to work
void sort(double &, double arr[], int s);
int main()
{
fstream myfile;
int size, i = 0;
const int n = 1000;
double x[n];
//reading data
myfile.open("data.txt");
if (myfile.fail())
{
cout << "Error Opening File" << endl;
exit(1);
}
while (!myfile.eof())
{
myfile >> x[i];
cout << x[i] << endl;
i++;
}
size = i - 1;
cout << size << " number of values in file" << endl;
myfile.close();
i = 0;
while (size > i)
{
cout << x[i] << endl;
i++;
}
//sorting
sort(x[n], x[n], size);
i = 0;
while (size > i)
{
cout << x[i] << endl;
i++;
}
return 0;
}
void sort(double &, double arr[], int s)
{
bool swapped = true;
int j = 0;
int tmp;
while (swapped)
{
swapped = false;
j++;
for (int i = 0; i < s - j; i++)
{
if (arr[i] > arr[i + 1])
{
tmp = arr[i];
arr[i] = arr[i + 1];
&[i + 1] = &tmp;
swapped = true;
}
}
}
}