What is intriguing is reversing return values of sortcheck() .
This create no "before.dat" file after the execution. It would be interesting finding out why |
To get a return value of 'true', it would require only two consecutive values in the entire file to be in ascending order or to be identical. The only way it would give 'false' would be:
it is already sorted, but in descending order AND all the values are different.
On the other hand, in the 'correct' version, if the file supplied by the user is already sorted in ascending order, there would be no output in the "before.dat".
This dependence on the data may not be what is needed, if all you want is before and after, there may be other ways.
I created a modified version of the program, where I passed values by reference rather than by value or pointer where appropriate. References are useful either when the object is to be modified, or it is large, and creating a copy (which pass by value does) would be wasteful.
Vettore.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#ifndef __Vettore_h__
#define __Vettore_h__
struct Vettore {
unsigned int size;
double* v;
bool sorted;
};
void init(Vettore& vec);
Vettore read(unsigned int size, const char* filename);
void scambia(double &, double &);
void print(const Vettore& vec);
void sort(Vettore& vec);
#endif
|
Vettore.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include "Vettore.h"
#include <fstream>
#include <iostream>
using namespace std;
void init(Vettore& vec)
{
vec.size = 0;
vec.v = nullptr;
vec.sorted = false;
}
Vettore read(unsigned int size, const char* filename)
{
Vettore vec;
init(vec);
vec.size = size;
vec.v = new double [size];
ifstream input(filename);
unsigned int i;
for (i = 0; i<size && input>>vec.v[i] ; )
{
i++;
}
// not ideal, loses track of capacity
if (i < vec.size)
vec.size = i;
return vec;
}
void print(const Vettore& vec)
{
const char * fname = vec.sorted ? "after.dat" : "before.dat";
std::ofstream fout(fname);
for (unsigned int j=0; j<vec.size; j++)
{
fout << vec.v[j] << endl;
std::cout <<vec.v[j] << endl;
}
}
void sort(Vettore& vec)
{
for (unsigned int k=0; k<vec.size; k++)
{
for (unsigned int h=k+1; h<vec.size; h++)
{
if (vec.v[k] > vec.v[h])
scambia(vec.v[k], vec.v[h]);
}
}
vec.sorted = true;
}
void scambia(double & a, double & b)
{
double c = a;
a = b;
b = c;
}
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
#include <fstream>
#include "Vettore.h"
using namespace std;
int main()
{
char nomefile[80];
cout << "Inserisci il nome del file da leggere: \n";
cin >> nomefile;
ifstream in(nomefile);
if (!in)
{
cout << "Il file non è stato caricato correttamente.. \n";
return 1;
}
else
{
cout << "Il file è stato caricato correttamente! \n\n";
}
cout << "Vi sono 10E6 valori. Quanti se ne vogliono analizzare? \n";
unsigned int a;
cin >> a;
Vettore carica = read(a, nomefile);
cout << "I dati prima dell'odinamento sono: \n"; // data stream before sorting
print(carica);
sort(carica);
cout << "I dati dopo l'ordinamento sono: \n"; // data stream after sorting.
print(carica);
}
|
This may at least give you some ideas to think about.