PLEASE HEELP IM STUCK IN A LOOP AND CANT FIND IT

//I cant seem to find my problem

#include <iostream>
#include <fstream>

const int MAXROWS = 4;
const int MAXCOLS = 4;
const int MAXFILENAME = 255;

// Structure used to define a point (x,y) in the grid.
typedef struct
{
int x, y;
} Point;

// Structure used to store a partially filled array, consisting of an
// array of integers and it's used size (number of filled positions).
typedef struct
{
int list[MAXROWS * MAXCOLS];
int size;
} Sequence;

// Prints the contents of the nums array to the screen
// from position 0 to position usedSize-1.
void printArray(const int nums[], int usedSize);

// Finds and returns the longest increasing sequence of numbers in grid, beginning at startPos.
// The value found at position startPos in the grid will be the first number in the returned sequence.
// Note: this is a helper function which is used to start the recursive generateSeq function.
Sequence getSequence(int grid[][MAXCOLS], Point startPos);

// Finds and returns the longest increasing sequence of numbers in grid, beginning at startPos.
// seq is the sequence which has been found so far.
Sequence generateSeq(int grid[][MAXCOLS], Point startPos, Sequence seq);

int main()
{
int grid[MAXROWS][MAXCOLS]; // Holds the grid, as read from the file
char filename[MAXFILENAME]; // Holds the filename of the grid file
Sequence maxseq; // Holds the max sequence found in the grid
Point startPos; // The position in the grid of where the sequence begins
cout << "Enter filename containing grid: ";
cin.getline(filename, MAXFILENAME);
Sequence tmpseq;
maxseq.size = 0;

ifstream input;
input.open(filename);
if (input.fail())
{
cout << "ERROR: input file cannot be opened.\n";

return 1;
}
// these two nested for loops read the first number from the file and place in the array named grid.
for(int y=0; y < MAXROWS; y++)
{
for(int x=0; x <MAXCOLS; x++)
{
input >> grid[y][x];
//cout << x << " " << y << " " << grid[y][x] << "\n";
}
}
input.close();
//these two nested for loops assign x and y as values for the start position on the grid.
for(int y=0; y < MAXROWS; y++)
{
for(int x=0; x< MAXCOLS; x++)
{
startPos.x = x;
startPos.y = y;
tmpseq = getSequence(grid, startPos);
if (maxseq.size > tmpseq.size)
{
maxseq = tmpseq;
}
}
}


cout << "The longest increasing sequence in the grid is:\n\n";
printArray(maxseq.list, maxseq.size);
cout << "\nLength: " << maxseq.size << endl;

return 0;
}

Sequence generateSeq(int grid[][MAXCOLS], Point startPos, Sequence seq)
{
Sequence maxseq;
Sequence tmpseq;
maxseq = tmpseq = seq;

Point reference;

tmpseq.size = 0;
maxseq.size = 0;
seq.list[seq.size] = grid[startPos.x][startPos.y];
seq.size++;
//if (grid[startPos.x][startPos.y] < grid[startPos.x + x][startPos.y + y])
for (int y = -1; y < 2; y++)
{
cout << "First For loop in generate sequence.\n";
if (startPos.y+y <= MAXROWS - 1 && startPos.y+y >= 0){
cout << "Bounds for Y.\n";
for(int x = -1; x < 2; x++)
{
cout << "Second For loop in generate sequence.\n";
if (startPos.x + x <= MAXCOLS - 1 && startPos.y + y >= 0){
cout << "Bounds for X.\n";
if (grid[startPos.x][startPos.y] < grid[startPos.x + x][startPos.y + y]){
reference.x = startPos.x + x;
reference.y = startPos.y + y;
cout << "StartPos.\n";
maxseq = generateSeq(grid, startPos, seq);
}
cout << "Just after the recursive function call.\n";
if (tmpseq.size < maxseq.size){
cout << "Comparing tmp seq with maxseq.\n";
tmpseq = maxseq;
cout << "After maxseq is stored in tmpseq.\n";
}
//if (maxseq.size > tmpseq.size)
}
}
}
}
/* if (tmpseq.size > seq.size){
cout << "Comparing Original sequence.\n";
return tmpseq;
}

else return seq; */
return (tmpseq.size > seq.size) ? tmpseq : seq;
cout << "Exit the program.\n";
}

Sequence getSequence(int grid[][MAXCOLS], Point startPos)
{
// Create an empty sequence
Sequence emptySeq;
emptySeq.size = 0;

// Use the empty sequence to start the recursive function
return generateSeq(grid, startPos, emptySeq);
}

void printArray(const int nums[], int usedSize)
{
cout << "{";

for (int x = 0; x < usedSize; x++)
{
cout << nums[x];

if (x < usedSize-1) cout << ", ";
}

cout << "}\n";
}
Last edited on
Please put in code brackets and tell us where you're stuck.
I agree with greenleaf please use code tags, and state your problem. http://www.cplusplus.com/articles/z13hAqkS/

I will point out that you don't have a using namespace std; statement.

Topic archived. No new replies allowed.