Thread: Noob question to hlep getting started with priority queue

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    86

    Noob question to hlep getting started with priority queue

    So, I'll start by posting the assignment details, say what I think it is, and then you guys can flame me so I know what to do.



    Goal : Implement a priority queue in which you may update the priority of an object in the
    queue.

    Description : You are to implement a min-priority queue using an array based heap. Them elements of the queue are objects (or structures) having an id which is a string and a value
    which is the priority of the object. An input command text le is used to specify the operations on the heap. An output text le is used to store any output results. You will need to create your own heap implementation. The elements of the heap array should be
    indirect references to the objects and not actual copies of the objects (this depends on the programming language | C++ should use smart pointers, in Java and Python most objects are all ready indirect references). The priority of an element all ready in the heap may
    change, so the program will need to be able to find an object based on its id. You may use your programming language's standard libraries to provide an efficient association between
    an id and the object (i.e., associative array, set, unordered set, dict, map, hash-map, etc.).The program must to do something better than a linear search when search for an element in the heap.

    Details:
    The program should be named pa1 and take one command line parameter:
    pa1 input- filename
    where input- lename is the name of the text le containing the commands for the heap operations. The le format is defined later in the assignment.

     The output of the program should be stored in a le called pa1out.txt. Only store the output specified by the command le.

     Use good programming style. Your priority queue code should be separated from the driver program pa1 which is used to test the code and from the class of the object that it is storing.

    Input File Format : the input file will be a line based command file. The first letter of each line specifies a command. The commands will be separated from the rest of the line by a whitespace. The rest of the line holds additional information that the command needs. Ignore blank lines.

    Then it proceeds to define the commands, like B: build a heap, the rest of the line is id value pairs, A: add news objects to the queue.

    Alright, so let me give this a whack. I am going to save the .txt files with the commands from the subversion location. Read them into my program. Use functions to completed tasks into an array of pointers, and then send the array's contents to an output file?

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Quote Originally Posted by carpeltunnel View Post
    Details:
    The program should be named pa1 and take one command line parameter:
    pa1 input-filename
    where input-filename is the name of the text file containing the commands for the heap operations. The le format is defined later in the assignment
    Does that mean prompt the user to type in the values from a command prompt? And just use the file for reference?

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It means that when you invoke the program, the file will be passed as an argument, for example:
    Code:
    C:\Users\Josh2>pa1 test.txt
    Since the commands will be in the file, there is no user interaction.

    See Command Line Arguments in C++ - Cprogramming.com

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Alright, I'm trying to trouble shoot a while before asking questions.

    All I'm trying to do at the moment is read the file and print its values (I start at 1 because the best character is a letter giving the instruction for that line)

    The first line in the .txt file has 22 values after a B, for build heap, the values are as followed: B foo 9.1 bar 2.1 a 1 b -4 c 7 d 2 e -4 f 5 g 7 h 9 i 4

    When I run my program I get -8xxxxxxxx, which I am assuming is an address? But I receive the identical value for each index.

    Code:
    #include <string>
    #include <queue>
    #include <iostream>
    #include <fstream>
    #include <conio.h>
    using namespace std;
    int main()
    {
     ifstream inFile;
     ofstream outFile;
     int i = 0;
     int idArray[100];
     inFile.open ("F:\\pa1Run1a.txt");
     outFile.open ("F:\\pa1out.txt");
     for (i = 1; i < 23; i++)
     {
      inFile>>idArray[i];
     }
     for (i = 1; i < 23; i++)
     {
      cout<<idArray[i];
     }
     getch ();
    
     
    
    }


  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Maybe because there not all integers, sorry, I figured it out all.

    But on another note, I'm having trouble wrapping my head around this and what I need to do. I have the .txt file and can display it:

    B foo 9.1 bar 2.1 a 1 b -4 c 7 d 2 e -4 f 5 g 7 h 9 i 4
    D
    U f 2.0 d 11 c 3
    P f d c
    R
    R
    R
    D

    But each letter signifies an action for that row. (B - build heap, D - dump, U - update, etc)

    My plan was to make a switch and for each case it performs specific task, so would I store foo-4 into an array, then have a for loop for a separate array with the whole txt contents in to act upon original array? Sorry if that question is muddy.
    Last edited by carpeltunnel; 09-14-2013 at 06:18 PM. Reason: Real Question

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Also, I am supposed to store the .txt contents into argv[] trough forum searching, google, and trial and error I haven't had any luck if someone could help out with that.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by carpeltunnel View Post
    Also, I am supposed to store the .txt contents into argv[] trough forum searching, google, and trial and error I haven't had any luck if someone could help out with that.
    Although argv is certainly a kind of receptacle, I wouldn't call it a trough.

    It's unlikely that you are meant to store the .txt contents into argv. More likely you're supposed to accept the name of the files from argv.
    Code:
    int main(int argc, char **argv)
    {
        if (argc < 3)
        {
            std::cerr << "Usage: myprog inputfile outputfile\n";
            return 1;
        }
    
        // open filename argv[1] for input
        // open filename argv[2] for output
    
        // do stuff
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Quote Originally Posted by oogabooga View Post
    Although argv is certainly a kind of receptacle, I wouldn't call it a trough.

    It's unlikely that you are meant to store the .txt contents into argv. More likely you're supposed to accept the name of the files from argv.
    Code:
    int main(int argc, char **argv)
    {
        if (argc < 3)
        {
            std::cerr << "Usage: myprog inputfile outputfile\n";
            return 1;
        }
    
        // open filename argv[1] for input
        // open filename argv[2] for output
    
        // do stuff
    
        return 0;
    }
    I can't for the life of me figure out the syntax to set the input file to argv[1], or is that what.

    Also, I tried to enter a simple print statement after the snippet you typed, and get nothing after usage: myprog input out.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Here is the sample code

    Code:
    #include <iostream>
    
    int main (int argc, char* argv[])
    {
        std::cout << "Program was called as:\n";
        for(int i=0;i<argc;i++)
        {
            std::cout << argv[i] << " ";
        }
        std::cout << std::endl;
        return 0;
    }
    And here is the result of running this program from command line
    Code:
    C:\Users\vart\svn\test_sample\Debug>test_cpp_sample.exe param1 param2
    Program was called as:
    test_cpp_sample.exe param1 param2
    
    C:\Users\vart\svn\test_sample\Debug>
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The tutorial I linked in my first post will pretty much show you the proper code to open and read a file from the command line. If you started out with the tutorial's code, it's output would at least ensure that you are opening the file correctly, because when it succeeds, it outputs the entire file onto the screen.

    One thing you have to do to avoid getting a usage message is actually pass in command line arguments. After you build your project, you can always invoke your program from a command prompt. Or, you could make your IDE give your exe program arguments when you use the run or debug button. The exact steps are different for every IDE, unfortunately. I know the steps for code::blocks: go to the project menu and select "Set program's arguments", then you type in what you want.

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    How did you run the program? It's supposed to be run from the command line like:

    myprog inputfile outputfile

    So run the program like that after adding this line after the if block and it should print the name of the inputfile and outputfile:
    Code:
    printf("%s\n%s\n", argv[1], argv[2]);
    Of course, I'm assuming this is what you want since your request didn't make sense, but it's up to you to determine and communicate your needs.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    oogabooga, is it truly so much to ask to use C++ code on the C++ board instead of C code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Could someone help me pass the arguments in command line.

    I have Microsoft Visual Studio 2010

    If I click start debugging, the program runs and closes instantly
    If I click start without debugging I get my usage print out from oogabooga's post.

    If I'm understanding correctly, I can add the parameters before running (which I can't find where to do) or do it one I run the program which obviously can't do when it closes instantly.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Goto project properties -> Debugging and enter your parameters under "Command Arguments".
    Also consider upgrading. 2012 has been out for a long while now. 2013 RC is also out if you like experimenting with new software (not production ready).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Quote Originally Posted by Elysia View Post
    Goto project properties -> Debugging and enter your parameters under "Command Arguments".
    Also consider upgrading. 2012 has been out for a long while now. 2013 RC is also out if you like experimenting with new software (not production ready).
    Thank you!
    I used to use Dev C, and it was much more simple there. And I will look into an update, I don't have a ton of code experience.

    So I finally got my program to out the .txt file names via argv[].

    But this leads me to a couple other questions:

    • Can I retried the actual contents from the argv element or do I still have to open the files in main and operate on them?
    • Also, once I do that how do I skip the first letter of each row i.e. B in my first post and just build my heap from the value pairs?


    If anything is unclear let me know, I am trying to understand a lot of new concepts and communicate clearly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Priority queue STL
    By dpp in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2009, 02:21 AM
  2. Priority queue
    By cjwenigma in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:30 AM
  3. Priority Queue Help
    By cjwenigma in forum C++ Programming
    Replies: 6
    Last Post: 11-15-2007, 12:48 AM
  4. Priority Queue Question
    By ac251404 in forum C++ Programming
    Replies: 26
    Last Post: 08-16-2006, 11:51 AM
  5. Priority Queue
    By evotron in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 01:46 PM