array

Pages: 12
I am having a problem with an assignment, I have got a file and then once saved I need to work out the Sum of the data, the biggest number, smallest and average.
Any ideas?
Liam.
Last edited on
Can you post some code?
Okay I will do.
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
// CO1401 Reassessment 2008/9
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;

double max(const double *data, int N);
double min(const double *data, int N);
double sum(const double *data, int N);
double mean(const double *data, int N);

int main ()

{
  char N, filein[256];
  ifstream myfilein;
  double sum=0;


	const int NMAX = 10;	// Maximum number of items to be processed




  do
  	{
		cout << "Enter the name of an existing text file: ";
  		cin.get (filein,256);

		myfilein.open (filein);

  if (!myfilein.is_open())



  {
	 	cout << "Unable to open file, sorry!" << endl;
	  	cout <<  "Please enter the file name that you which to be processed : ";
		cin.get (filein, 256);
	}
}


	while (!myfilein.is_open());

	cout << "These are the contents of the file: " << endl;

 	while (myfilein.good())   // loop while extraction from file is possible


  {

    N = myfilein.get();       // get character from file

    if (myfilein.good())

      cout << N;

  }

while (myfilein >> N)
	{
		sum=sum+N;
	}

	cout << endl << endl;
	cout << "The sum of the data is: " << sum << endl;

	myfilein.close();           // close file

  return 0;
}

Hope that helps, you will also need a text file to go with it, but thats just a 5 numbers
After loop which reads all the characters from the file, you would have reached the end of it so you won't be able of reading it the second time. You can close and reopen the file before the second loop to solve this
Sorry, do you mean after i've got the contents of the file, the file is closed, so I can't process the data in the file untill it is opened again?

P.S. Also does everything else look ok?
Just thought I may ask, you don't have to
Last edited on
After you've read it at all the file would be still open but no more in a good state and you would have reached the end.
You can close and re-open it or set the state to good and restart from the beginning
For the latter solution:
1
2
f.clear(); // set state to good
f.seekg(0); // go to the beginning of the file 


The rest of your code looks fine ( excluding indentation you should line up the blocks of code in a better way http://en.wikipedia.org/wiki/Indent_style ) and sum=sum+N; could be written as sum += N;
Thanks, that worked a treat, apart from the sum is coming up as like 629, when it should be 337.6.
Also if I had say 6 numbers in a text file is there a way to just process the first 5 numbers?
The numbers I was processing are
100
89
79.6
4
65
Last edited on
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// CO1401 Reassessment 2008/9
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;

double max(const double *data, int N);
double min(const double *data, int N);
double sum(const double *data, int N);
double mean(const double *data, int N);

int main ()

{
	char N, filein[10];
	ifstream myfilein;
	double sum=0;
	double mean =0;
	double max =0;
	double min=0;

	const int NMAX = 10;	// Maximum number of items to be processed

	do
		{
			cout << "Enter the name of an existing text file: ";
  			cin.get (filein,10);

			myfilein.open (filein);

		if (!myfilein.is_open())

			{
	 			cout << "Unable to open file, sorry!" << endl;
	  			cout <<  "Please enter the file name that you which to be processed : ";
				cin.get (filein, 10);
			}
		}

	while (!myfilein.is_open());

		cout << "These are the contents of the file: " << endl;

 	while (myfilein.good())   // loop while extraction from file is possible

		{
			N = myfilein.get();       // get character from file

  	if (myfilein.good())

      		cout << N;

		}

			myfilein.clear(); // set state to good
			myfilein.seekg(0); // go to the beginning of the file

	while (myfilein >> N)

		{
			sum += N;
		}

			myfilein.clear(); // set state to good
			myfilein.seekg(0); // go to the beginning of the file

	while (myfilein >> N)

		{
			mean = sum/5;
		}

			myfilein.clear(); // set state to good
			myfilein.seekg(0); // go to the beginning of the file

	while (myfilein >> N)

		{
			if (filein[10] > N)
				max = filein[10];

		}

			myfilein.clear(); // set state to good
			myfilein.seekg(0); // go to the beginning of the file

	while (myfilein >> N)

		{
			if (filein[10] < N)
				min = filein[10];
		}

	cout << endl << endl;
	cout << "The sum of the data is: " << sum << endl;
	cout << "The average value of the data is: " << mean << endl;
	cout << "The biggest number is: " << max << endl;
	cout << "The smallest number is: " << min << endl;

		myfilein.close();           // close file

  return 0;
}


This is what I've got now, and all the answers I got from the processing the data were wrong, I'm not sure why?
Any ideas?
Thanks Liam
1
2
3
char N;
//...
while (myfilein >> N)
You are reading characters, not numbers. Declare 'N' as a double
That worked, but now it won't show the contents of the file as it's just coming as a line of random numbers, but it's finding the correct sum and average.
I think it's because it's still finding characters, in the get function.
If you want to read a char, use a char. istream::get returns the ASCII value of the next character in the stream. You are seeing those values as doubles instead of chars
I'm not sure what you mean.
Sorry.
Last edited on
N = myfilein.get();
If you have '1' in your file, you would get 49, the ASCII value for '1'
To get the actual character, you should get a char:
1
2
char mychar;
myfilein.get ( mychar );
I did that, and it's showing the correct contents of the file again, but now all the sum and average and max and min are wrong. I'm getting the 629 again as the sum of the contents.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// CO1401 Reassessment 2008/9
//Liam Moncur BSc Computer Networking
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

double max(const double *data, int N);
double min(const double *data, int N);
double sum(const double *data, int N);
double mean(const double *data, int N);

int main ()

{
	char N;    //This is the change
	char filein[10];
	ifstream myfilein;
	double sum=0;
	double mean=0;
	double max=0;
	double min=0;

	const int NMAX = 10;	// Maximum number of items to be processed

	do
		{
			cout << "Enter the name of an existing text file: ";
  			cin.get (filein,10);

			myfilein.open (filein);

		if (!myfilein.is_open())

			{
	 			cout << "Unable to open file, sorry!" << endl;
	  			cout <<  "Please enter the file name that you which to be processed : ";
				cin.get (filein, 10);
			}
		}

	while (!myfilein.is_open());

		cout << "These are the contents of the file: " << endl;

 	while (myfilein.good())   // loop while extraction from file is possible

		{
			myfilein.get ( N );       // get character from file   This is the change

  	if (myfilein.good())

      		cout << N;

		}

			myfilein.clear(); // set state to good
			myfilein.seekg(0); // go to the beginning of the file

	while (myfilein >> N)

		{
			sum += N;
		}

			myfilein.clear(); // set state to good
			myfilein.seekg(0); // go to the beginning of the file

	while (myfilein >> N)

		{
			mean = sum/5;
		}

			myfilein.clear(); // set state to good
			myfilein.seekg(0); // go to the beginning of the file

	while (myfilein >> N)

		{
			if (filein[10] > N)
				max = filein[10];

		}

			myfilein.clear(); // set state to good
			myfilein.seekg(0); // go to the beginning of the file

	while (myfilein >> N)

		{
			if (filein[10] < N)
				min = filein[10];
		}

	cout << endl << endl;
	cout << "The sum of the data is: " << sum << endl;
	cout << "The average value of the data is: " << mean << endl;
	cout << "The biggest number is: " << max << endl;
	cout << "The smallest number is: " << min << endl;

		myfilein.close();           // close file

  return 0;
}

I think maybe that my functions for doing the sums, don't understand that chars and get it wring, because when it was a double, (apart from the max and min) it came up with the correct answers
Last edited on
You need two different variables, a char to get the text, a double to get the values and calculate sum etc.
That worked, thank you.
I also using my new
double i=0;
I changed the max and min to
1
2
3
4
5
while (myfilein >> i)
{
if (N > i)
max = N;
}

1
2
3
4
5
while (myfilein >> i)
{
if (N < i)
min = N;
}

but it comes up with the same answer for both.
Last edited on
You are reading 'i' from the file and then assigning 'N' to 'max' and 'min'
Ok I did that, it finds the second biggest number, it doesn't seem to reconise sayt 100 or 300, and it's not reading negative numbers. but apart from missing negative numbers, it's finding the smallest fine.

Sorry I don't think thats the problem, i think it's just going through it all and then it's not looking logically, it just thinks that it's finding the last biggest number, and saying thats the biggest compared to the smallest.
Last edited on
How is your cone now on that part?
cone? sorry don't understand.
Pages: 12