Vector Warm up: project#0

Words of wisdom when working on projects this semester.

  1. Start on the project immediately.  Many of my project will take much longer than you think.  This gives you time to ask questions, fix bugs etc.
  2. When you start on the project late, the stress of trying to get it completed will radically reduce what you learn from it as well as ruin your enjoyment of the project.  I am NOT kidding.
  3.  If you do not enjoy solving and writing projects (when not under stress) you are most likely in the wrong major. CAPICE!
  4. Starting on your project last will also temp you to copy a solution you find on the net or from a friend.  BAD, BAD, BAD.  I am always here to help you if you start early. Remember the words of a great sage from the past.  The path to self confidence will never be found through cheating!  

This semester we will writing our programs so that they read their data from stdin (ie cin) and write their data to stdout(ie cout).  You will not open files.  Here is a discussion on how to do this in visual studio C++.  This method makes it very easy to feed data into your programs by running your executable with command line redirection.  For example if you program generates the executable search.exe then I can feed the data in file.txt into your program via the dos command c:\search.exe < data.txt.  If you wish to do both input and output at the same time then  c:\search.exe < data.txt >output.txt will read its data from data.txt and write out its output to output.txt.  This first program will give you practice in doing just this.

Problem Definition:  Your problem is to read in integers from the input file until a 0 is encountered, loading the values into a vector<int>.  Note that this is a very simple example of a problem that cannot easily be handled using classic c++ arrays.  Why? Well for one thing, you have no idea how many integers will be read in, and consequently cannot allocate the proper sized array.  With vector<>’s we just push_back() the integers until we hit the end of the data and the vector container will automatically expand as needed.

Your problem is to calculate the longest repeated sequence of integers which are the same. If there are two or more sequences that have the same length you are to select the first one to output.  You need to do this for multiple sequences. For example suppose that you data is the following sequence.

2 5 4 7 8 7 9 9 9 3 3 3 5 4 1 7 9 2 2 2 2 4 5 5 5 7 9 0
6 0
5 5 5 5 3 3 3 3 0
23 43 55 55 12 2 47 47 8 2 19 19 19 0

You can read in the above lines using simple scheme such as the following method. You can assume that each line will have at least one value other than the 0.

while(cin>>n){// is there any more data?
    vector<int> lst;// create a new lst for this data case
    // Do your processing on lst here. Try to be creative in you algorithm.   

 //IMPORTANT NOTE: The vector was allocated within this loop
 // so consequently the destructor for lst will be called automatically as
 // as we fall out of the loop.

For each data set you are to output a single line containing three integers, N L P.  N is the number that is repeated, L is the length of the repeat and P is is position where the repeated section began.  Here is the output that should be generated for the above data.

2 4 17
6 1 0
5 4 0
19 3 10

To Turn in: After you get this working, place the entire well documented project on a jump drive. Copy your executable (ie .exe file in debug) to the root directory.  Do not load only the .cpp files etc on the drive, load your entire visual studio C++ project so I can compile it if I choose.  Print the source and also turn in the output for the above data.  Print the output file, do not print the output window. No documentation results in a 10% reduction in your grade.

Comments are closed.