Simple For loop not running!

Below is my code for a project I am working on. The code gets past the first two groupings of For loops and then says Windows experienced and error and terminates the .exe file. From what I can tell this is nothing wrong, obviously to my beginner knowledge, with any references. Could someone please run it and see what their computer says. Or, tell me if I have an error in my code.
The output should be:
spot 1
spot 2
spot 3
spot 4
spot 4
spot 3
spot 2
spot 1
spot 5
spot 6
spot 7
spot 8
spot 12
spot 11
spot 10
spot 9
spot 13
spot 14
spot 15
spot 16

Code:
//Test of matrix manipulation
//12/24/15

#include <iostream>
using namespace std;

//This just gives each point a name for the sake of printing the pattern
char Point1[]="Spot 1";
char Point2[]="Spot 2";
char Point3[]="Spot 3";
char Point4[]="Spot 4";
char Point5[]="Spot 5";
char Point6[]="Spot 6";
char Point7[]="Spot 7";
char Point8[]="Spot 8";
char Point9[]="Spot 9";
char Point10[]="Spot 10";
char Point11[]="Spot 11";
char Point12[]="Spot 12";
char Point13[]="Spot 13";
char Point14[]="Spot 14";
char Point15[]="Spot 15";
char Point16[]="Spot 16";

//This is a dumby holder of the GPS Location to show the simple version of the grid system
char* array_2[4][4]={
//Col 0 1 2 3
{Point1,Point2,Point3,Point4},//Row 0
{Point5,Point6,Point7,Point8},//Row 1
{Point9,Point10,Point11,Point12},//Row 2
{Point13,Point14,Point15,Point16}//Row 3
};

//The matrix is only 4x4....Really the matrix/array will be dynamic from drive to drive....dependent on length and width of drive.
const int numRows=4;
const int numCols=4;
//This code provides the sweep pattern for the robot. Remember the std::out will be replaced with commands telling the robot what to do from point to point.
void pathcode(){
for(int row=0; row<1; ++row){
for(int col=0; col<numCols; ++col){
std::cout<<array_2[row][col]<<endl;} //Sweep first row right to left
}
for(int row=0; row<1; ++row){
for(int col=3; col<numCols; --col){
std::cout<<array_2[row][col]<<endl;} //sweep back to the first position of row 1
}
for(int row=1; row<2; ++row){
for(int col=0; col<=numCols; ++col){
std::cout<<array_2[row][col]<<endl;} //go to row 2 and sweep to the end
}
for(int row=2; row<3; ++row){
for(int col=3; col<=0; --col){
std::cout<<array_2[row][col]<<endl;} //drop a row and reverse sweep to row[0]
}
for(int row=3; row<=numRows-1; ++row){
for(int col=0; col<=numCols; ++col){
std::cout<<array_2[row][col]<<endl;} //drop to final row[3] and sweep last time right to left
}
}

//This simply runs the code. :)
int main(){
cout <<"Here is what your pattern will look like: "<< endl;
pathcode();
return 0;
}






1
2
        for(int col=3; col<numCols; --col){
            std::cout<<array_2[row][col]<<endl;}

This lets col take on negative values, which produces an out-of-bounds access to array_2.

1
2
        for(int col=0; col<=numCols; ++col){
            std::cout<<array_2[row][col]<<endl;}

This allows col == 4, which results in out-of-bounds access
Last edited on
There are several problems in the for loops.
I've left the original code preceded by a //
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
void pathcode()
{
    // Sweep first row right to left
    for (int row=0; row<1; ++row)
    {
        for (int col=0; col<numCols; ++col)
        {
            std::cout << array_2[row][col] << endl;
        } 
    }
    
    // sweep back to the first position of row 1
    for (int row=0; row<1; ++row)
    {
//      for (int col=3; col<numCols; --col)
        for (int col=numCols-1; col>=0; --col)
        {
            std::cout << array_2[row][col] << endl;
        } 
    }
    
    // go to row 2 and sweep to the end
    for (int row=1; row<2; ++row)
    {
//      for (int col=0; col<=numCols; ++col)
        for (int col=0; col<numCols; ++col)
        {
            std::cout << array_2[row][col] << endl;
        } 
    }
    
    //drop a row and reverse sweep to row[0]
    for (int row=2; row<3; ++row)
    {
//      for (int col=3; col<=0; --col)
        for (int col=numCols-1; col>=0; --col)
        {
            std::cout <<array_2[row][col] << endl;
        } 
    }
      
    // drop to final row[3] and sweep last time right to left
    for (int row=3; row<=numRows-1; ++row)
    {
//      for (int col=0; col<=numCols; ++col)
        for (int col=0; col<numCols; ++col)
        {
            std::cout << array_2[row][col] << endl;
        } 
    }
}


Hope that helps.

However the code is confusing to read as it gives the impression of a nested loop when the value of row does not change, thus there is no need for a loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void pathcode()
{
    // Sweep first row right to left
    for (int col=0, row=0; col<numCols; ++col)
        std::cout << array_2[row][col] << endl;

    // sweep back to the first position of row 1
    for (int col=numCols-1, row=0; col>=0; --col)
        std::cout << array_2[row][col] << endl;
         
    // go to row 2 and sweep to the end
    for (int col=0, row=1; col<numCols; ++col)
        std::cout << array_2[row][col] << endl;
    
    //drop a row and reverse sweep to row[0]
    for (int col=numCols-1, row=2; col>=0; --col)
        std::cout <<array_2[row][col] << endl;
    
    // drop to final row[3] and sweep last time right to left
    for (int col=0, row=3; col<numCols; ++col)
        std::cout << array_2[row][col] << endl;
}

Last edited on
Thank you guys so much! I know this seems very basic stuff, obviously it is, but I am just learning c++. I am a mechanical engineer and just started getting into the world of coding! Chervil, The suggestion of the new for loops is very helpful! I didn't know that the for loop could take arguments like that. I understand the logic of coding basics but need to learn more of the syntax! Happy holidays!
Glad that helped. The for loop is quite flexible. It combines several pieces of functionality into a single statement.

For example this code:
1
2
    for (int col=0, row=3; col<numCols; ++col)
        std::cout << array_2[row][col] << endl;


could be re-written like this:
1
2
3
4
5
6
7
8
9
10
11
{
    int col=0;
    int row=3;

    while  (col<numCols)
    {
        std::cout << array_2[row][col] << endl;
        
        ++col;
    }
}

Sometimes it may help to think of it that way, in order to visualise how it will behave.
See the tutorial page on control structures:
http://www.cplusplus.com/doc/tutorial/control/
Very helpful again! I am going through the tutorial now and try sample examples!
Topic archived. No new replies allowed.