Is there a more efficient way to write this chunk of code?

I have a couple classes under my belt and am learning while loops atm. I have an assignment taking input from a .txt and outputting various mins, maxes, averages. I have a system that works for finding all these, but I basically repeat it 5 times with different variables. I am wondering if there is a better way to go about it.

ex: Within my while eof loop.
1
2
3
4
5
6
7
8
9
if(lineCount == 0) {// ensure on first line of file.
     max = x; // set x to max value
}
else {
     if(x > max) {
          max = x;  // new max value 
     }
}
				


repeat 5x for different variables. Any suggestions on consolidation?

thanks.
Well, you could just do the else part every time, and ensure max is initialized to 0 or a number definitely less than all the data you will be reading in.
I want to suggest using a struct but I am not completely sure how this works.

Can you post 2 successive examples?
> repeat 5x for different variables. Any suggestions on consolidation?

Do it with one loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int max_var1 = std::numeric_limits<int>::min() ;
int min_var1 = std::numeric_limits<int>::max() ;
double max_var2 = std::numeric_limits<double>::min() ;
double min_var2 = std::numeric_limits<double>::max() ;
short max_var3 = std::numeric_limits<short>::min() ;
short min_var3 = std::numeric_limits<short>::max() ;

int var1 ;
double var2 ;
short var3 ;
while( file >> var1 >> var2 >> var3 )
{
   max_var1 = std::max( max_var1, var1 ) ;
   min_var1 = std::min( min_var1, var1 ) ;
   max_var2 = std::max( max_var2, var2 ) ;
   min_var1 = std::min( min_var2, var2 ) ;
   max_var1 = std::max( max_var3, var3 ) ;
   min_var1 = std::min( min_var3, var3 ) ;

   // etc
}
The above suggestion is golden but if you are using classes to their full potential you could replace everything in the while loop with your class methods to clean up the variable declarations.

But I am curious as to what the 5 different variables are? Are you counting unsigned types as unique?
scarywoody, when you say different variables, are these of different types as in JLBorges post? Or five different variables of the same type?
Last edited on
I am in a little over my head now. We have not learned about using functions at this point. I haven't seen the min.max used yet.

Th input data file has 5 or 6 columns of data ex:

12-22-11 56 78 45 34 6669696
12-21-11 45 78 34 99 7557599
...
...

I am finding min of one column, max of another, average of another, highest delta between days in another. On some of these I need the dates of highs and lows. I assigned each day a variable, and then I have variable for each value and date that I need.
closed account (zb0S216C)
scarywoody wrote:
1
2
3
4
5
6
7
8
if(lineCount == 0) {// ensure on first line of file.
     max = x; // set x to max value
}
else {
     if(x > max) {
          max = x;  // new max value 
     }
}


scarywoody wrote:
"Is there a more efficient way to write this chunk of code?"

How about...

 
if((!lineCount) && (x > max)) max = x;

No?

Wazzak
Assuming max is an int, if max is init to std::numeric_limits<int>::min(), there is no need to special case for lineCount == 0. So

if(x > max) max = x;

would be enough

Which is pretty much what the std::max solution is doing.
Last edited on
It does seem a bit mean to ask you to parse a file with that many columns without functions.

But I assume you know cin/cout, ifstream/ofstream, and string? (as well as the basics of C++, including loops)
Yeah I may have overstated the functions part. We do know the items listed above and are just getting into the loops:) Thanks for the suggestions. I will start toying around and see if I can get it rolling.

Thanks for all the help.
No better that JLBorges' code when it comes to performance, but if you're into templates you can factor out some of the code into a common class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Range<int>    range1;
Range<double> range2;
Range<short>  range3;

int    var1 = 0;
double var2 = 0;
short  var3 = 0;
while (file >> var1 >> var2 >> var3)
{
	range1.reg_val(var1);
	range2.reg_val(var2);
	range3.reg_val(var3);

	// etc
}


where

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
template<typename T>
class Range
{
private:
	T m_min;
	T m_max;

public:
	Range() :
		m_min(numeric_limits<T>::max()),
		m_max(numeric_limits<T>::min()) {
	}

	void reg_val(const T& t) {
		m_max = max(m_max, t) ;
		m_min = min(m_min, t) ;
	}

	const T& getMin() const {
		return m_min;
	}

	const T& getMax() const {
		return m_max;
	}
};
Last edited on
Topic archived. No new replies allowed.