Problem with sorting datasets ASAP!

I have a problem with my code...
First of all, is there a way (in very basic fstream) to store values in a dataset directly to an array that I can display in multiple places in the file? I don't know if I have already done that or not...help here please
Second of all, My function calls are bringing up errors...I don't know why...could someone help me out and offer suggestions?
Lastly, the code is supposed to sort through a dataset and then display the values in descending order. Am I on the right track? Please offer suggestions to make it better please. Thank you for those who respond in advance!!!!!

Here is my code so far:

#include <iostream>
#include <fstream>
#include <cstdlib>
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, number[MAX_ARRAY_SIZE], next, array;

infile.open("numbers.dat");
if (infile.fail())
{
cout << "Failed to open file." << endl;
exit(1);
}

cout << "The original array: " << endl;
while (infile >> next)
{
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);
return 0;
}

void exchange_sort(int array[], int array_size)
{
ifstream infile;
int i, j, next, num;
int temp;

for (num = 0; num <= MAX_ARRAY_SIZE; num++)
{
while (infile >> next)
{
if (i >= j)
return;
else
{
temp = i;
i = j;
j = temp;
}
}
}
}

float median(int array[], int array_size)
{
return 0;
}
your array is just an int. number is an array of 100 ints, but it's not used
it should look more like this
1
2
3
int array[MAX_ARRAY_SIZE]
ifistream infile("numbers.dat", ios::binary) // I'm assuming it's binary
infile.read((char*) array, sizeof(array); // putting the file into array (and assuming it too is an array of 100 ints 
Last edited on
What does line 3 of your text do for me that I haven't already done earlier?!
read fills an array with the file specified up to the length specified. It only take a char pointer as the first argument, so you have to convert it.

I'm sure this is a much better explanation http://cplusplus.com/reference/iostream/istream/read.html
Last edited on
how is my sorting function?! will it work?
the simplest way would be to use STL's sort, include <algorithm>, and use:
 
sort(array, array + MAX_ARRAY_SIZE, greater)
these are the instructions:

// Write a code which goes through each element of the array, compares it with all the elements after it, and swaps them if necessary, use the temporary variable to swap any two array elements
// TO DO: you will need two nested for loops
// The outer loop will go through all elements except the last one
// The inner loop will go through all the elements after the current element of the outer loop.

Can you help with this?!
This sounds like "Bubble sort", which should be something like this

1
2
3
4
5
6
7
8
for (int *i = array, i != array + MAX_ARRAY_SIZE, ++i)
    for (int *j = array, j < i, ++j)
        if (*i > *j) {
             int temp = *i;
             *i = *j;
              *j = temp;
        }
            

I apologize, but I am not familiar with pointers, at least I think those asterisks symbolize. Can you make the loop without pointers perhaps? I appreciate all of your help again. You're helping out substantially!!!!
yes, it can be done with an index instead. The index is just another way to do pointer arithmetic.
1
2
3
4
5
6
7
for (int i = 0; i !=  MAX_ARRAY_SIZE; ++i) // doh, I realized in my last code I used commas instead of semicolons. I do that all the time
    for (int j = 0; j < i; ++j)
        if (array[i] > array[j]) {
             int temp = array[i];
             array[i] = array[j];
              array[j] = temp;
        }

Last edited on
how do i do the code style that you use?
so how i do output the dataset in correct sorted order?!? i keep getting an infinite loop.
the sorting orders the array, so just
1
2
for (int i = 0; i !=  MAX_ARRAY_SIZE; ++i)
      cout << array[i] << endl;
This is my program, and I'm getting crazy numbers when I comile...

#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)
{


return 0;
}
Topic archived. No new replies allowed.