Hi, I have an assignment where I have to read in data from a file and then output the original order of the data (which I have done), and then I have to sort through the dataset and then put the data in order in descending order. I am having trouble outputting the sorted order. I think I have the right code?! Then, I need help finding the median as well. Please help ASAP!!!
NOTE: Please use the most basic code you know, I'm new to C++ and I don't get all of the advanced stuff. Thank you in advance!!
Here is my code (sorry, I don't know how to into the right form yet, I'm new to cplusplus.com):
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_ARRAY_SIZE = 100;
void exchange_sort(int array[], int array_size);
float median(int array[], int array_size);
int main()
{
ifstream infile;
int count = 0, next, start;
infile.open("numbers.dat"); //Open the dataset file
if (infile.fail())
{
cout << "Failed to open file." << endl;
exit(1);
}
int array[MAX_ARRAY_SIZE];
cout << "The original array: " << endl;
while (infile >> next) // Read the array and then output the array to the user
{
cout << next << " ";
cout << " ";
count += 1;
}
cout << endl << "The sorted array (from high to low): " << endl;
exchange_sort(array, count);
cout << endl << "The median of the dataset is: " << endl;
//float median(array, count);
cin >> start;
return 0;
}
void exchange_sort(int array[], int array_size)
{
int i, j;
int temp; // temporary swap variable
for (int i = 0; i < array_size; ++i)
{
cout << array[i] << " ";
for (int j = 0; j < i; ++j)
{
if (i <= j)
{
temp = i;
i = j;
j = temp;
}
}
}
}
float median(int array[], int array_size)
{
#include <iostream>
#include <fstream>
usingnamespace std;
constint MAX_ARRAY_SIZE = 100;
void exchange_sort(int array[], int array_size);
float median(int array[], int array_size);
int main()
{
ifstream infile;
int count = 0, next, start;
infile.open("numbers.dat"); //Open the dataset file
if (infile.fail())
{
cout << "Failed to open file." << endl;
exit(1);
}
int array[MAX_ARRAY_SIZE];
cout << "The original array: " << endl;
while (infile >> next) // Read the array and then output the array to the user
{
cout << next << " ";
cout << " ";
count += 1;
}
cout << endl << "The sorted array (from high to low): " << endl;
exchange_sort(array, count);
cout << endl << "The median of the dataset is: " << endl;
//float median(array, count);
cin >> start;
return 0;
}
void exchange_sort(int array[], int array_size)
{
int i, j;
int temp; // temporary swap variable
for (int i = 0; i < array_size; ++i)
{
cout << array[i] << " ";
for (int j = 0; j < i; ++j)
{
if (i <= j)
{
temp = i;
i = j;
j = temp;
}
}
}
}
float median(int array[], int array_size)
{
return 0;
}
while reading put this in array, code taken from line 21:
1 2 3 4 5 6 7 8 9 10 11
int array[MAX_ARRAY_SIZE];
cout << "The original array: " << endl;
count = 0;
while (infile >> next) // Read the array and then output the array to the user
{
array[count] = next; //check that count should not be greater than max array size.
count++;
}
//you have the array now, print it.
for(int i = 0; i < count;i++)
cout << array[i] << endl;
hope you are successful in fetching data from the file.. correct?
the problem which remains is of sorting??
your sort function is doing nothing!!!! you need to sort the array, instead you are just swapping numbers.
it should be something like this, though it the worst algo but good for a beginner to understand:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void sort(int arr[], int size)
{
int temp;
for(int i = 0; i < size; i++)
{
for(int j = 0;j < size - 1; j++)
{
if(arr[j] > arr[j+1])
{
//we need to swap
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}