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;
}
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.
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;
}
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!