0

I need help creating a nested for loop. This is for "Jump Game" and basically there is an array with some numbers in it. The number at each array[position] has a certain number of times it can jump to reach the end. Ex: [2 3 1 1 4 0] so array[0] = 2 and it can jump 2 or 1 places, array[1] = 3 and can jump 3,2, or 1 places. Ex: [3 2 1 0 4 0] so you basically can never reach the position in the array.

I've been going at it for a while and can't come up with the loop. The readFromFile just basically creates the array size. Now that I have the size I need to check each number which I am stuck on.

void readFromFile(int list[], int& size, ifstream& infile)
{
    infile >> list[size];

    while (!infile.eof()) // Loops until end of file is reached
    {
        size++;
        infile >> list[size];
    }

    infile.close();
    return;

}

bool getToLastIndex(int list[], int size)
{
    for(i=0; i< size;i++)
    {
        if(list[i] == 0)
            break;
        j=i+list[i]; //Resetting it for a new jump or section on array?
        i=j;

        for(int j=0; j <= list[i]; j++)
        {
            list[j] == return true;
        }
    }
}

I want to be able to use getToLastIndex(list,size);

if(getToLastIndex() == true)
{
    cout << "Mario FTW" << endl; // Got to the end
}
else
    cout << "This bridge has the workings of bowser all over it >:( " << endl; //Couldnt make it to the end

The homework is Mario themed... I didn't choose the outputs

arsdever
  • 1,000
  • 6
  • 24
101001
  • 73
  • 7

1 Answers1

0

Try the following:

void readFromFile(int list[], int &size, ifstream& infile)
{
    size = 0;
    if(!infile.is_open())
        return;

    while(!infile.eof())
    {
        infile >> list[size++];
    }
    infile.close();
}

bool isTheLastReachable(int list[], int index, int size)
{
    if(index < size)
    {
        if(index)
        {
            for(int i = 0; i < list[index]; ++i)
                if(isTheLastReachable(list, index + i, size))
                    return true;

            return false;
        }
        else
            return false;
    }
    return true;
}


bool getToLastIndex(int list[], int size)
{
    return isTheLastReachable(list, 0, size);
}
arsdever
  • 1,000
  • 6
  • 24
  • 1
    This is a code only answer. You can salvage it by explaining what the asker got wrong and why your solution solves it. Also remember that `while(!infile.eof())` is [a subtle-but-nasty bug](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) and should not be reproduced in an answer. – user4581301 Apr 24 '19 at 21:42
  • @Michael Torres did my answer solved your problem? If som accept it – arsdever May 23 '19 at 11:21