Thread: Finding lowest value

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    10

    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. #2
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    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
    google is your friend.
    Last edited by xviddivxoggmp3; 11-09-2004 at 12:43 AM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    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. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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);
    }
    Quote 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. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    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. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote 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. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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 subscribe to a feed

Similar Threads

  1. Extracting lowest and highest score from array
    By sjalesho in forum C Programming
    Replies: 6
    Last Post: 03-01-2011, 06:24 PM
  2. did i do this right?
    By Joldil in forum C++ Programming
    Replies: 8
    Last Post: 07-25-2007, 02:43 AM
  3. Finding the lowest number of a certain input
    By theorbb in forum C Programming
    Replies: 26
    Last Post: 04-17-2006, 02:36 AM
  4. Finding lowest value
    By sloopy in forum C Programming
    Replies: 5
    Last Post: 11-26-2005, 10:48 AM
  5. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 01:21 PM