Thread: Algorithm Help Please!

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    166

    Algorithm Help Please!

    Hello,
    I'm having trouble understanding what one of my assignments requires me to do. This is a data structures ADT program. I tried emailing the teacher, but she could only manage to clarify a few things for me... Maybe there's just something that I'm not seeing. If someone could make sense of this, please help me out! I would really appreciate it!

    Write a program that does the following:
    1. Prompts the user to enter the name of the input file.
    2. Reads the data in the file into a stack. Each time an S is encountered process data if possible and display the results(see example below), otherwise display an error message.
    3. If the stack is not empty, save it to a file (get the name of the output file from the user). You have to be able to build the stack back from the file exactly as it is.

    Example:

    02/15/2008 B 100 3.00
    02/15/2009 B 200 5.00
    02/15/2010 B 300 10.00
    02/15/2011 S 400 100.00

    Display the following:
    Feb. 15, 2011
    400 * 100 = 40000
    300 * 10 = 3000
    100 * 5 = 500
    Gain: 40000 -3500 = 36500
    100 shares left out of 200 shares at $5.00 each.

    Here is the file that I'm supposed to use:
    02/15/1995 S 10000 3.71
    02/15/1995 B 10000 3.71
    02/15/2009 B 1000 16.15
    01/27/2011 S 5000 28.87
    09/02/2011 B 100 25.80
    09/06/2011 B 500 25.51
    09/08/2011 S 700 26.52
    09/09/2011 B 2000 25.74
    09/15/2011 S 1000 27.00
    09/22/2011 B 300 25.00
    09/22/2011 s 30000 25.00
    09/26/2011 B 100 25.50

    -First, the file starts with an S... so do I just ignore the first term since there is nothing above it? And since there are multiple S's, what exactly do I process? The whole file or just what is above the S up to the last previous S? Would I need multiple stacks or is one enough?
    -Lastly, were supposed to use #include "stacks.ADT", however when I have this in my visual studio, it doesn't recognize this. What can I do?
    -Thank you so much for your time! I know this is a lot of information, but I am very confused and worried that if I can't even figure out the algorithm, then I can't write the code. I hope what I'm asking makes sense.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I find it very odd that your teacher won't explain this in a way you can understand. At any rate I'm not sure how useful I can be. Anything I say is basically my interpretation of what you've written meanwhile it's the teacher's assignment and the teacher is the best source for clarifacation you have.
    First, the file starts with an S... so do I just ignore the first term since there is nothing above it?
    My opinion is that there is enough information there to do at least 1 round of calculations, even if the line is an S line. In other words it's a stack of one thing.
    2/15/1995
    10,000 shares * 3.71 = 37,100
    And since there are multiple S's, what exactly do I process?
    The instructions say:
    2. Reads the data in the file into a stack. Each time an S is encountered process data if possible and display the results(see example below), otherwise display an error message.
    A stack is a data structure. B lines will just become part of the stack. So if you encounter an S line, you have to add it to the stack and then display the output. To display the output, first you have to pop data off of the stack in turn. When you have popped off everything, you can continue reading the file.
    Would I need multiple stacks or is one enough?
    I think it's up to you if you use the same instance or destroy the stack after dealing with each S line.
    -Lastly, were supposed to use #include "stacks.ADT"
    Please include files with the .h extension. Your compiler knows what to do with them.
    Last edited by whiteflags; 10-09-2012 at 05:19 AM.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    And put the files in a directory included (pardon the pun) in the include file path. How you will do this depends on the IDE you're using.

    First, the file starts with an S... so do I just ignore the first term since there is nothing above it?
    I would certainly say the example infers you need to process it. Figuring this sort of thing out -- as well as the algorithm -- is an important part of software development, and you need to put more thought into it. Let's look at the example.

    Example:

    02/15/2008 B 100 3.00
    02/15/2009 B 200 5.00
    02/15/2010 B 300 10.00
    02/15/2011 S 400 100.00

    Display the following:
    Feb. 15, 2011
    400 * 100 = 40000
    300 * 10 = 3000
    100 * 5 = 500
    Gain: 40000 -3500 = 36500
    100 shares left out of 200 shares at $5.00 each.
    Notice how the data in the S line is reflected in the first non-header line of the output? I would say that's a pretty big indication you don't just ignore it. You can ever figure out how a stack works from the example output.

    I'd say this is a pretty nice assignment.

    100 shares left out of 200 shares at $5.00 each.
    I must admit this sort of baffles me a bit. Not sure where that's coming from, but I may be a stock-selling moron. I'd sell the shares with the biggest difference first, which would make the ending result that you had sold the $3 and $5 stock altogether, and 100 of the $10, leaving 200 of the $10 after the transaction.

    /me shrugs
    Last edited by rags_to_riches; 10-09-2012 at 07:42 AM.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    That was just a typo. I do have the header written as #include "stackADT.h". It still doesn't recognize it. But I need that header to write the program.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Do you know how to add header files you download to your project? If you write code in stackADT.h you need to make sure the compiler can find it by either storing the h files where the c files are stored, or the path to the file should be listed as one of the include directories.

    I was able to find detailed instructions for many IDEs by searching the web.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DFS algorithm
    By Eman in forum C++ Programming
    Replies: 8
    Last Post: 03-18-2011, 08:47 AM
  2. Algorithm HELP!!!
    By alexdavid in forum C Programming
    Replies: 43
    Last Post: 02-14-2011, 04:49 PM
  3. Replies: 16
    Last Post: 01-20-2011, 10:58 PM
  4. algorithm help
    By the Wookie in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 07:12 PM
  5. my grandfather's chess algorithm can beat your grandfather's chess algorithm...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 08-17-2001, 06:52 PM