Can't get my program to run,
Essentially, im trying to create a program that can
open a read a file (txt) with values in it then
take those values an put them into an array.
Then sort the array and give the min, max, median, mean values.
and output those values into a txt file.
#include <fstream>
#include <iostream>
#include <string>
#include <cmath>
usingnamespace std;
int main ()
{
ifstream fin("EGR126_data4.txt");
ofstream fout("results.txt");
int temp, count(0);
do
{
fin>>temp;
count++;
} while (!fin.eof());
fin.close();
fin.open("EGR126_data4.txt");
int *dArray;
dArray = newint[count];
for(int i = 0; i <= count; i++)
{
fin >> dArray[i];
}
/**** ARRAY SORTING ****/
int swap;
for (int i=0; i<=count; i++)
{
for(int j=0; j <=count; j++)
{
if (dArray[j] > dArray[j+1])
{
swap = dArray[j];
dArray[j] = dArray [j+1];
dArray[j+1] = swap;
}
}
/***** MATH CALCULATIONS *****/
int mean, median, stdv, total;
for (int k = 0; k<count; k++)
{
total = dArray[i]+ dArray [i+1];
}
mean = total/count;
median = dArray[count/2];
fout << dArray[i] << endl;
fout << "this is a test of an output"<< endl;
fout << "The minimum is " << dArray[1]<< endl;
fout << "The maximum is " << dArray[count] << endl;
fout << "The mean is "<< mean << endl;
fin.close();
return 0;
}
}
} while (!fin.eof());Instant fail. count will always be off by one. median = dArray[count/2]; Will not work as it should iff count is even
mean have heavy precision problems (unless you want it to be truncated to whole) dArray[count] Out of bound access: array of count elements have valid indexes [0; count-1]
And you are sorting your array, calculatian mean and median count times.
Use vector instead of dynamic arrays. All C++ books will say so. Also it is a good idea to learn C++ algorithms.
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
int main ()
{
std::ifstream fin("EGR126_data4.txt");
if(!fin.is_open())
exit(-1);
std::vector<int> values;
std::copy(std::istream_iterator<int>(fin),
std::istream_iterator<int>(),
std::back_inserter(values));
std::sort(values.begin(), values.end());
double mean(std::accumulate(values.begin(), values.end(), 0.0)/values.size());
double median;
if(values.size() % 2 == 0) {
int upper = values.size()/2;
median = (values[upper] + values[upper - 1]) / 2.0;
} else
median = values[values.size()/2];
std::ofstream fout("results.txt");
std::copy(values.begin(), values.end(),
std::ostream_iterator<int>(fout, "\n"));
fout << "This is a test of an output\n" <<
"The minimum is " << values.front() << '\n' <<
"The maximum is " << values.back() << '\n' <<
"The mean is " << mean << '\n' <<
"The median is " << median << std::endl;
}
Your outer for-loop will run once because you are returning a value inside the scope of the loop (line 71).
line 56 total = dArray[i]+ dArray [i+1];
should be total += dArray[i]+ dArray [i+1];
lines 62-66; How can you know the min and max values when you have not yet fully sorted the array?
Thanks fixed line 56, tried using std::sort but intellisense gives me an error, im probably putting it in the wrong spot. Im essentially using std::sort to replace the whole swap portion of the code for sorting.
Does this sound right?
} while (!fin.eof());Instant fail. count will always be off by one.
median = dArray[count/2]; Will not work as it should iff count is even
mean have heavy precision problems (unless you want it to be truncated to whole)
dArray[count] Out of bound access: array of count elements have valid indexes [0; count-1]
And you are sorting your array, calculatian mean and median count times.
Use vector instead of dynamic arrays. All C++ books will say so. Also it is a good idea to learn C++ algorithms.
Vectors seem like the best way to go,
unfortunately i have to use dynamic arrays for this specific program.
How do I compensate for the (!fin.eof()) being off one?
So should I put sort, mean, median all in a for loop to run count times??
int total;
for (int k = 0; k<count; k++)
{
total += dArray[k]+ dArray [k+1];
}
int mean, median, stdv;
mean = total/count;
median = dArray[count/2];
std::cout << min (dArray);
std::cout << max (dArray);
int mean what you think would be mean of 2 and 3 in your case? Answer: it would be 2 because of rounding. Use double. double mean = std::accumualte(dArray, dArray + count, 0.0) / count;
median = dArray[count/2]; Find median of sequence {0, 5, 7, 7} using your code. it gives you 7, but it should be 6. Fix that.
do
{
fin>>temp;
count++;
} while (!fin.eof());
count--;
fin.close();
This, counts the values all the way down to the end.
its off by 1 so
count -- will fix that (in this case)
then closes file to reset back to the top when its opened again
fin.open("EGR126_data4.txt");
int *dArray;
dArray = new int[count];
for(int i = 0; i <= count; i++)
{
fin >> dArray[i];
}
This creates a dynamic array,
with the count as the number of values/size of the array.
This simply opens the file,
and sets an ouput file
Yes. But it is better to declare variables where they will be used.
Other parts: yes.
1 2 3 4
for(int i = 0; i <= count; i++)
{
fin >> dArray[i];
}
i <= count; i < count
A) dArray does not have count's element
B) Even if it does you trying to read past end of file in your last reading. Another off-by-one error. Remember if you want to do something n times loop shold look asfor(int i = 0; i < n; ++i)