-2

I'm working on a a project for my class where I have to use all four algorithms for the maximum subarray sub problem (cubic, quadratic, linear, and recursive). My problem is that I'm supposed to read the input from a file and I'm having trouble figuring out how to read until the end of a line, execute code with that data, and then move onto the next line. The input file looks like this:

2
-5 -10 -2 -4
2
-2 10 -5 -6
3
-10 -5 0 5 -20 20 -50
4
10 8 2 -20 -50 -100 -150
0
-1 -2 -3 -4 -5
1
-100 -200 0
4
200 500 -700 1000 2000 -5000 4 10
4
100 200
6
0 0 0 0 0 0 -10
5
-4 10 -3 200 500 -700 2
0
5 10 15 20 25 30 35
2
10 30 50 70 100
3
-15 16 23 -30 0 -2 13 8 6 0 -4 -15
12
9 8 23 -1 -23 -4 0 0 4 7 9 10 -23 68 1 -2 -3 -6 -19 10 5 1 1 2 4 3 -46 12     -100 78 -23 0 0 12 2 7
5
-1 -3 0 2 3 6 1 -5 -3 -2

now my issue is that I want to be able to read in a single number (the minimum subarray length) and then fill the array with the row of numbers below it, then execute all four algorithms using that data, then I want to move on to the next minimum subarray length and the next array of values below it, execute all four algorithms etc., and I want to be able to do this all in one go.

For example, I want to read 2 as the minimum subarray length, then use (-5,-10,-2,-4) as the array, execute all four algorithms, then use 2 as the minimum length, (-5,10,-5,-6) as the array, execute all four algorithms, and keep doing that until the end of the file.

Logan Crocker
  • 59
  • 2
  • 7
  • Zero effort to solve the problem shown by OP results in zero effort to solve the problem given by me. Sorry man. Here are some non-random keywords to get you started: `std::getline`, `std::stringstream`, `while (sstream >> number)` – user4581301 Oct 27 '15 at 01:23
  • use **getline** method to get whole lines at once and use **strtok** on alternate lines to split the line based on whitespaces – Swastik Padhi Oct 27 '15 at 01:26
  • @CrakC `strtok` is a destructive function that does not work with std::string without copying the string to a char array, and using `std::getline` with anything other than std::string is a supremely ungood idea. – user4581301 Oct 27 '15 at 01:32

1 Answers1

0

Here is a quick possible solution that might help ("test.txt" contains your values...)

#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int main()
{
    ifstream in("test.txt");

    string str;
    int value;

    while (in)
    {
        vector<int> vec;

        getline(in, str);
        stringstream stream(str);

        while (stream)
        {
            stream >> value;
            vec.push_back(value);
        }
        vec.erase(vec.end()-1);

        //Here the vec conntains all values of the current line
        for (auto v : vec)
            cout << v << " ";
        cout << endl;
    }
}
Varius
  • 459
  • 3
  • 14
  • Fails because `while (in)` tests for validity of the stream before making a read. [Variant of this.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) Instead use `while (getline(in, str))` and `while (stream >> value)` in place of `while (stream)` – user4581301 Oct 27 '15 at 01:57