function to read values from file and get the maximum

hello
i want to read data from a file to caculate the variation between them.
and get the maximum.
i have this data in the file:
50 10
50 78
100 170
250 200
100 100
50 90
100 120
65 96
100 160
250 190
10 10
100 150

and here is my trial
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
fstream fin;
int maximum(int , int );
bool open_file( );

int main()
{
int currentavg(0), lastavg(0);
bool exist;
exist=open_file();
{
if (exist=true)
{
while(!fin.eof())
{
fin>>lastavg>>currentavg;
maximum(currentavg, lastavg)<<endl;

}
}
else
cout<<"file does not exist\n";
}

return 0;
}
bool open_file()
{
bool exist;
fin.open("rain.dat");
{
if (fin.fail())
{

cout<<"file does not exist\n";

}
else
{
exist= true;
}

}
return exist;
}
int maximum(int x , int y )
{
int u, max(0), temp;
u=y-x;
if (u>max)
max=u;

return max;
}

how i can get the maxiumu?
Just a tip: [code]int main()[/code] = int main(). This would make your code more readable :)

Could you be more specific about your problems? In what way is your code not working? Does it not compile? Does it crash? Does it behave unexpectedly? The more information you provide, the easier it will be for us to help you!

Regards
-Xander314
hi my dear
i want to read from the file and write a function to detemine the maximum value of the difference.
the problem is that the function does not give me one value. it give me a list of values

so how i can write a condition in the function to do that.
i hope the poblem is clear now
thanks alot
closed account (zwA4jE8b)
return (u>x) ? u : x;

1) read in the first number : consider it the maximum.
2) read in the second number : compare it to the first.
3) if greater then it is the new maximum
4) repeat while not eof
Last edited on
the function does not give me one value. it give me a list of values

Your function returns a list of values? I can't see why it should.

You can just use if else blocks to see which number is large and return it. Or you could do as CreativeMFS suggested, although I would go with the first method for clarity, unless you are happy with the conditional operator.
Hi ,
What is the output you are getting .. are you getting the error or wrong value of the max .
also the code has the modified to work properly .
the question is to include this function
int max(int, int): Each time returns the biggest number among two integers which are sent as parameters. This function can be used to find the largest deviation from the average.
1
2
3
4
5
int maximum(int y, int x)
{
     if (u>max)
   max=u;
   return max


if i write the above function , how i cancall it in main to get the maximum value from a file, how can i do that?
recntly the function read only the last value in the file not the maxima
Last edited on
In that piece of code, you are using a variable u which doesn't seem to be declared anywhere.

As for reading data from a file, see the tutorial on files on this site:
http://cplusplus.com/doc/tutorial/files/
y dont u store all the # in an array/stack/queue than use bubble sort by which u have ur first as the lowest and last index as the highest
by that u can just compare and get the difference and u dont need to worry about rest. :)

K.
this is the full HW
The average monthly rainfall for the last 8 years (historical average) (2001 – 2009) is stored in the first column of a file called rain.dat In addition, last year’s monthly rainfall averages (current average) are stored in the second column of the same file.
Write a C++ program that reads the data file and displays a graph showing the variation between the historical and current monthly averages month by month.
In the graph (see sample below), months are represented by the first 3 letters abbreviation of the months’ name. Every ‘*’ represents 10 units. Moreover, the left part of the graph represents negative variations, while the right part represents positive variations with respect to the average. Your program should also display the largest deviation from the average.
Your program must implement and use the following functions:
1. bool open_file(): To open and validate the existence of the input file. If the file fails to open for any reason then the function should return false and the program terminates.
2. void draw_Line(int, int): To plot a line (corresponding to a month) inside the chart based on the month’s readings
3. string num_to_month(int): To convert integers (1-12) to the 3 letters month abbreviations.
4. int max(int, int): Each time returns the biggest number among two integers which are sent as parameters. This function can be used to find the largest deviation from the average.


and here my answer
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
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>//for output
#include <fstream>// do dclare fin
#include <iomanip>
#include <string>// for strin function
using namespace std;
fstream fin;
int currentavg, lastavg, u, count, Month_Max;// global parameter to avoid repating and some erros
int maximum(int , int );
void draw_Line(int, int);
bool open_file( );
string num_to_month(int m);

int main()
{
	int max(0);
	bool exist;
	exist=open_file();// check if the file open or not
	{
		if (exist=true)
		{
			while(!fin.eof())// read from the file
			{
				fin>>count>>lastavg>>currentavg;
				max=maximum(max, currentavg-lastavg );
				draw_Line(currentavg, lastavg );
			}
		}
		else
			cout<<"file does not exist\n";
	}
	cout<<"the maximum value for the diffrernce is "<<maximum(max, currentavg-lastavg )<<endl;// maximum value
	
	
	return 0;//end main
}
bool open_file()//function to open the file
{
	bool exist;
	fin.open("rain.dat");
	{
		if (fin.fail())
		{
			
			cout<<"file does not exist\n";

		}
		else
		{
			exist= true;
		}
		
	}
		return exist;
}
int maximum(int x , int y )// to get the maximum
{
	if (y>x)
		x=y;
	return x;
}
void draw_Line(int x, int y)//to draw stars
{
	string stars ="" ;
	for (int i=0 ; i<abs(y-x) ; i+=10)
		stars+="*" ;
	if ((y-x)>0)
		cout <<setw(19)<<right<<stars<<" "<<num_to_month(count)<<endl;
	else
		cout <<setw(20)<<"  "<<num_to_month(count)<<setw(10)<<right<<stars<<endl;
}
	

string num_to_month(int mounth)// to convert numbers of month to nams

{
	string convert_to_m ;

	switch (mounth) 
	{
		case 1:
				convert_to_m="Jan" ;
		break ;
		case 2:
				convert_to_m="Feb" ;
		break ;
		case 3:
				convert_to_m="Mar" ;
		break ;
		case 4:
				convert_to_m="Apr" ;
		break ;
		case 5:
				convert_to_m="May" ;
		break ;
		case 6:
				convert_to_m="Jun" ;
		break ;
		case 7:
				convert_to_m="Jul" ;
		break ;
		case 8:
				convert_to_m="Aug" ;
		break ;
		case 9:
				convert_to_m="Sep" ;
		break ;
		case 10:
				convert_to_m="Oct" ;
		break ;
		case 11:
				convert_to_m="Nov" ;
		break ;
		case 12:
				convert_to_m="Dec" ;
		break ;
	}
return convert_to_m ;
}
i already submit the cpp file yesterday
for the future if you have any comment on that code please tell me esspecially on how to convert from string to integer

thanks for you all
best regards
Last edited on
closed account (zwA4jE8b)
does atoi stand for "alpha to int"?
I don't know, but I would guess either "alpha to int" or "ASCII to int".
Topic archived. No new replies allowed.