### Dynamic Arrays

Hey guys,

Need some much appreciated help.

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.

thanks

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` `````` #include #include #include #include using namespace 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 = new int[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
Use `std::sort()` from `#include algorithm `

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?
` } 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.
Rewrite of your program using heavy (and sometimes unnecessary) use of standard library:
 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include #include #include #include #include #include int main () { std::ifstream fin("EGR126_data4.txt"); if(!fin.is_open()) exit(-1); std::vector values; std::copy(std::istream_iterator(fin), std::istream_iterator(), 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(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; }``````
 Use std::sort() from #include algorithm 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??
 So should I put sort, mean, median all in a for loop to run count times?
They already doing so, but they shouldn't. Move them out of the loop.
 How do I compensate for the (!fin.eof()) being off one?
Use
 ``123`` ``````while(fin >> variable) { //use variable here }``````

And do not forget that sort and othe algorithm works on arrays too.
I tried doing that but it got what you showed but got some errors.
Im not familiar with it so im sure im typing it wrong.

Would this work??
 while (!fin.eof()); count--;

or how can i do it.

THANKS for all the help btw.
And this is my std algorithyms

 ``` int total; for (int k = 0; k
`min (dArray)` Did you made min as a function?

`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.

 Would this work??
Not always. It depends.
Last edited on

intellisense says that namespace std doesnt have a member accumualte.

Let me try to break it the program down so i understand what each part does.

 ``` #include #include #include #include #include using namespace std; int main () { ifstream fin("EGR126_data4.txt"); ofstream fout("results.txt"); int temp, count(0); ```

This simply opens the file,
and sets an ouput file

Correct?
 ``` 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.

right?
 namespace std doesnt have a member accumualte
`#include <numeric> `
 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.
 ``1234`` `````` 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 as` for(int i = 0; i < n; ++i)`
Last edited on
Check values on your first pass :
 ``123456`` ``````ifstream fin("EGR126_data4.txt"); int temp, count(0); while(fin >> temp){ if(temp > 0) ++count; }``````
Topic archived. No new replies allowed.