Hello, I am a non-cs student who has not really had any prior experience with C++ besides coding up a few simple programs on my own time.

I am trying to implement the A* pathfinding algorithm and I have all of the logic figured out and written down in pseudo-code, but the implementation has got me banging my head against the wall. I just spent the last 40 minutes searching these forums and google, but I can't seem to find the answer to my question on what syntax do I use to declare a matrix?

I am trying to hardcode a 5x5 matrix that will store values which I define.

Here is my code that I have written, but I know this isn't right because I tried to compile this in terminal and I got a whole lot of errors:

#include "math.h"
#include <iostream>

using namespace std; 



int main()
{
double distance[][] = 0; 	
int x = 5;
int y = 5;

//set the matrix to be 5x5 and assign every element to be 0
distance[x][y] = 0; 

//view matrix in terminal 
cout << distance << endl;

return 0;
}

Could someone also tell me how to access/assign elements in a matrix correctly, or just direct me to an article that goes over the syntax on how to do this?

Also, another nooby question, but I was wondering how to compile and run this code in linux properly.

I type: g++ astartest.cc

then I see an a.out file in my folder and I try to type gcc a.out and ./a.out but neither work properly.

I know that this is really simple stuff, but I tried looking around for it and just found a lot of articles/threads that seem above my level since everyone knows how to declare matrices.... :P

Thanks a lot to anyone that decides to help!

Recommended Answers

All 4 Replies

The following link will help you regarding matrix syntax:
http://stackoverflow.com/questions/618511/a-proper-way-to-create-a-matrix-in-c

Ok, so I read through the article, and there is a lot of syntax I have never seen before such as the double colon (::) in front of variables it seems.

Not to sound spoiled or anything, but I was hoping that there were articles somewhere that were more fundamental? Because I looked through some of the sample code and I probably did not recognize about 30% of the syntax and could not really follow without comments for things such as:

typedef array<array<array<char,N>,N>,N> C;

Anyway, thank you for the article, it was somewhat helpful.

A few options for 2-dimensional arrays.

  1. Static allocation.
  2. Dynamic allocation.

Static allocation is when you know the size of the array beforehand, as you do. You know in advance that the matrix will be a 5 x 5 matrix. So you are in the first category (static). That's good because that makes it easier.

int array[5][5];

// now let's store some values in it (how about an addition table?

for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 5; j++)
    {
        array[i][j] = i + j;
    }
}

// now let's display the array

for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 5; j++)
    {
        cout << array[i][j] << "\t";
    }
    cout << std::endl; // line return.
}

That's the basics.

Also, another nooby question, but I was wondering how to compile and run this code in linux properly.

I type: g++ astartest.cc

then I see an a.out file in my folder and I try to type gcc a.out and ./a.out but neither work properly.

./a.out should work. gcc a.out won't. gcc is the C compiler. You aren't using C, so don't use gcc. Use g++, which is the C++ compiler. And a.out is already an executable, so you wouldn't use gcc or g++ to run the program. You use them to CREATE the executable program. Again, you won't be using gcc at all since you are programming in C++. a.out is the default name for the executable program. If you want to name your program myprogram , do this, assuming the C++ program is named astarttest.cc .

g++ astarttest.cc -o myprogram
./myprogram

This is assuming you are using Linux. I forget what the default program name in Windows is. Maybe a.exe or something. Anyway, I think the ./a.out only applies to Linux. Not sure. In Windows, if the executable was a.exe , I think you would just type a on the command line to run it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.