0

I have a comp-sci project at university that has me somewhat stumped. 1[Project Link]This link will take you to the website, and the project is the PDF called "LiDAR Data and 3D arrays". The basic premise of the project is to be able to read and store data into a 3D char array, and then show the actual data in the format of a picture. Here is my code that contains the 3 methods we were assigned to write in the class array3d.

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
using namespace std ;

// -----------------------------------------------------------------
// array3d class with variables for 3D Matrix boundaries, 3D Matrix itself,
// Methods to store data in 3D Matrix, and display LiDAR data

class array3d
{
    private:
    int m, n, p;
    int a, b, c;
    char *** G;
    public:
    array3d();
    ~array3d();
    bool read(char * fname);//Method to read file and store data in 3D    Matrix
    void get_sizes(int & a, int & b, int & c);//Method to fine values of m, n, and p 
    int get_zmap_value(int x, int y);// Method to return highest occupied cell for pos. x, y
};
// -----------------------------------------------------------------
bool array3d::read(char * fname)
{
    //Read the data into a 3D matrix
    ifstream ifs;
    ifs.open(fname);
    if(!ifs.is_open())
    {
        cerr << "Can not open (read) file '" << fname << "'" << endl;
        return false;
    }
    ifs >> m;
    ifs >> n;
    ifs >> p;
    G = new (nothrow) char **[m];
    for(int i = 0; i < m; i++)
    {
        G[n] = new (nothrow) char *[n];
        for(int j = 0; j < n; j++)
        {
            G[n][p] = new (nothrow) char [p];
            for(int k = 0; k < p; k++)
            {
                ifs >> G[m][n][p];
            }
        }
    }
    ifs.close();
    return true;
}
// -----------------------------------------------------------------
void array3d::get_sizes(int & a, int & b, int & c)
{
    //Get values for m, n, and p using pass-by-reference semantics
    a = m;
    b = n;
    c = p;
}
// -----------------------------------------------------------------
int array3d::get_zmap_value(int x, int y)
{
    //Returns the highest occupied cell (z-index) for pos. x, y in the  ground plane
    for(int z = 0; z < p; z++)
    {
        if(G[x][y][z] == 1)
        {
            return z;
        }
    } 
}
// -----------------------------------------------------------------

And here is the code for the MakeFile we are also supposed to create

##
## FILE: MakeFile
##
zview:      array3d.o main.o
        g++ -o zview array3d.o main.o -L/user/local/lib -lppm_graphic 
##
array3d.o: array3d.cc
        g++ -c array3d.cc
##
##
clean:
        /bin/rm array3d.o main.o zview

For the makefile, we have been given the main.o file, but are unable to access it. Also the library file is in another directory, so we have to access that using a number of different cmd line options.

What ends up happening when I run my code, and do zview, is that it opens the program that is supposed to show pictures, but there is no picture on it, just the title of the picture program and some other options. Basically, I am not sure what my problem is here, but any help would be appreciated. Also, please don't just give me the answer, I would like to actually understand why this is not working, so any constructive help would be greatly appreciated.

  • 1
    You wrote some code, you plugged it into a provided `main`, and the whole thing didn't work. **You must test your code.** Write your own test data, and your own `main.cc` which will run these functions on that test data. See if your code gives you the results you expect. *Then* try plugging them into the teacher's `main.o`. – Beta Sep 27 '16 at 23:21
  • P.S. If your tests indicate that your `get_zmap_value(...)` is correct, then your tests are inadequate. – Beta Sep 27 '16 at 23:24
  • The problem is, and this is highly due to my inexperience, is that I am not sure what form that test data would take. I know these are all questions for my professor, but he is away for a week and so I am sort of stuck. –  Sep 27 '16 at 23:44
  • How about a 2x3x4 array, filled with the character 'a'? Do you know how to write that data file? If you `read` that file, and then call `get_sizes`, what do you think you will get? Verify that, and then try `get_zmap_value` for a few different arguments. Once all of that works perfectly, you can try a more challenging array, like one divided diagonally between 'a's and 'b's. – Beta Sep 28 '16 at 00:03
  • So I think I have managed to solve the problem, however what is troubling me now is the makefile. Linking the libppm_graphic.a is confusing me. How would I go about linking this because it is in a seperate directory, and it requires -L/user/local/lib -lppm_graphic to access –  Sep 28 '16 at 00:06
  • Your question is confusing me. Did you or did you not compile, link and run your code? Does the library `/user/local/lib/libppm_graphic.a` exist? Does the command in your makefile contain the linker flags you say it must have? – Beta Sep 28 '16 at 00:13
  • so the libppm_graphic.a does exist, but in a seperate file/directory. My code works fine when I move the file into the directory I am working in, called Lab1, so my question is how do I link libppm_graphic.a in the makefile, evne though it is in a seperate directory. –  Sep 28 '16 at 00:40
  • My professor gave us two lines of code that he said would allow us to link libppm_graphic.a, which are the lines -L/user/local/lib -lppm_graphic –  Sep 28 '16 at 00:42
  • Sorry, I am not sure how to indent code in comments –  Sep 28 '16 at 00:42
  • Your makefile, as written, should work; the problem is difficult to diagnose over this narrow channel. Anyway, you have a work-around, and you have solved your main problem. If you want to pursue the linking problem, there are probably plenty of people in your building who can help you. – Beta Sep 28 '16 at 02:14

0 Answers0