# Thread: Finding lowest value

1. ## Finding lowest value

I'm am creating a program that uses stream and creates two separate file that tracts the balances in a savings account and a checking account. At the end I need to print the lowest value that occured in each of the accounts. How do I go about doing that?

2. what do you have so far?
i would say you would need to start out with
some knowledge of file processing (fstream).
http://www.cprogramming.com/tutorial/lesson10.html

3. Find the value:

#define MIN(a,b) (if (a<b)?a:b)
#define MAX(a,b) (if (a>b)?a:b)

int testvalue=MAX(10,20);

Note that these macros are not typesafe in that a and b can represent any object or data type. Do not use this on objects as it does not make sense to say that object a is less than object b. However for the sake of not using templates in my example I used the macro solution. But this should work for all numerical data types although the definition gets a little fuzzy for boolean data types. While in boolean if a is true and b is false, a is technically greater than b - if TRUE is 1 and FALSE is 0, thinking of booleans in that way is not advised because some define TRUE and FALSE as different values depending on implementation. For MSVC I believe that true evaluates to 1 and false evaluates to 0 which fits with how C evaluates expressions.

4. I have the program running just fine. But at the end of the statement I need to print the lowest value of the variable checkingBalance on the checking statement, and savingsBalance on the savings account statement. Those variables start with a certain value, then depending on the type of transaction, either are subtracted from or added to.

5. So how about keeping track of the lowest <variable, entrie record, whatever>, and then using that to <find the record, display the field, whatever> at the end?

Quzah.

6. By the way, the templated min and max functions are already defined in <algorithm>.
Code:
```#include <algorithm>

int main()
{
int testmax = std::max(10, 20);
int testmin = std::min(10, 20);
}```
Originally Posted by Bubba
#define MIN(a,b) (if (a<b)?a:b)
#define MAX(a,b) (if (a>b)?a:b)

int testvalue=MAX(10,20);
The macro version should not have the ifs:
Code:
```#define MIN(a,b) ((a<b)?a:b)
#define MAX(a,b) ((a>b)?a:b)

int testvalue=MAX(10,20);```

7. Say the variable starts out at 3000. The program will read the input file and separate it into transaction in the savings account and transactions in the checking account. Depending on the transaction the variable will either be added to or taken away from. I need to find the lowest point that variable gets to.

8. Originally Posted by jlou
Code:
```#define MIN(a,b) ((a<b)?a:b)
#define MAX(a,b) ((a>b)?a:b)```
Wrong...
e.g. this would simply fail
MIN(15 & 2 , 3 & 1)
< has greater precedence than &...

Correction
Code:
```#define MIN(a,b) ( (a)<(b) ? (a) : (b) )
#define MAX(a,b) ( (a)>(b) ) ? (a) : (b) )```
Rules when writing macros
1# wrap variables with parenthesis
2# wrap the whole macro with parenthesis
3# (optional) use capital letters to you remember that it is a macro

9. xErath: Ok. I wouldn't use that macro anyway, the functions in <algorithm> should work fine, but you are right, that version is better than mine or Bubba's. Here is one last correction:
Code:
```#define MIN(a,b) ( (a)<(b) ? (a) : (b) )
#define MAX(a,b) ( (a)>(b) ? (a) : (b) )```

titleist_03: Did you try what quzah said? Keep track of the lowest amount in a separate variable as you go.

Popular pages Recent additions