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


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 <fstream>
#include <iostream>
#include <string>
#include <cmath>


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<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;
}
}
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:
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
#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;
}

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
1
2
3
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<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);

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 <fstream>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>


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.
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 as for(int i = 0; i < n; ++i)
Last edited on
Check values on your first pass :
1
2
3
4
5
6
ifstream fin("EGR126_data4.txt");
int temp, count(0);
while(fin >> temp){
    if(temp > 0)
        ++count;
}
Topic archived. No new replies allowed.