0

I was given a program in C++ from my professor and was asked to change the struct into a class, privatize int **data, and then add getters and setters. However, I've never worked with a double pointer before and I have no idea how to proceed. All I have done is modify the Triangle header file, but I don't even know if I'm doing that correctly. Help would be greatly appreciated!

//Triangle.h

#ifndef TRIANGLE_H
#define TRIANGLE_H

class Trngl{
private:
    int **data;
public:
    int size;
    void setData(int **d){**data = **d;};
    int getData(){return **data;};
};

#endif /* TRIANGLE_H */

//main.cpp

//System Libraries Here

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

//User Libraries Here
#include "Triangle.h"

//Global Constants Only, No Global Variables
//Like PI, e, Gravity, or conversions

//Function Prototypes Here
Trngl *fillStr(int);
void prntStr(Trngl *);
void destroy(Trngl *);

//Program Execution Begins Here
int main(int argc, char** argv) {
    //Set the random number seed
    srand(static_cast<unsigned int>(time(0)));

    //Declare all Variables Here
    int row=10;

    //Input or initialize values Here
    Trngl *triangle=fillStr(row);

    //Output Located Here
    prntStr(triangle);

    //Return Memory
    destroy(triangle);

    //Exit
    return 0;
}

void destroy(Trngl *tri){
    for(int row=0;row<tri->size;row++){
        delete []tri->data[row];
    }
    delete []tri->data;
    delete tri;
}

void prntStr(Trngl *tri){
    cout<<endl;
    for(int row=0;row<tri->size;row++){
        for(int col=0;col<row;col++){
            cout<<tri->data[row][col]<<" ";
        }
        cout<<endl;
    }
    cout<<endl;
}

Trngl *fillStr(int n){
    //Allocate a structure to hold the Matrix
    Trngl *tri=new Trngl;
    //Create the 2-D allocated pointer
    tri->data=new int*[n];
    for(int row=0;row<n;row++){
        tri->data[row]=new int[row+1];
    }
    tri->size=n;
    //Fill the array with values
    for(int row=0;row<n;row++){
        for(int col=0;col<row;col++){
            tri->data[row][col]=rand()%90+10;
        }
    }
    //return the Array pointer
    return tri;
}
  • In the `Trngl::setData` function, where does `data` point when you dereference it? Think a little bit more about the `setData` and `getData` functions, and what the dereferencing mean (hint: `*anyPointer` is equal to `anyPointer[0]`). – Some programmer dude Feb 21 '19 at 06:39
  • Suggestion: Take advantage of [RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii). Move the allocation of the storage for `Trngl::data` to a `Trngl` constructor. Handle the `delete`s in the `Trngl` destructor and keep an eye on the [Rule of Three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – user4581301 Feb 21 '19 at 06:40
  • @user4581301 Personally I would rather encourage [the rule of *zero*](https://en.cppreference.com/w/cpp/language/rule_of_three#Rule_of_zero), by using `std::vector` for the data. – Some programmer dude Feb 21 '19 at 06:51
  • Sorry, I'm very new at programming, would it be possible to tell me exactly what needs to be done? I've tried using a constructor and destructor and I keep on messing it up. I've been working on this for hours, and I've never used getters and setters in this fashion before. – 91837465839 Feb 21 '19 at 07:09
  • @Someprogrammerdude won't argue that, but if the instructor wants `int **`, no sense flunking the assignment. `vector`'s the dead right option. It's the right choice, but the grade's still dead. – user4581301 Feb 21 '19 at 07:10

1 Answers1

0

The **data is not that complicated, think about how the triangle looks like: 00 10 11 20 21 22

so you have a number of arrays or *lines but how do you store them? An Array is great for that, and what is the type of the array? its an array of arrays or as an array is a pointer its a pointer of pointers. if you dont want to write it, or its a bit confusing, you can use a typedef and just use it as normal variable.

Instead of the set, you can write a real constructor: Triangle(**d) and as destructor ~Triangle() As mentioned above: be careful with the destructor, you want to delete every pointer, not just the outer pointer.

And: put the prototypes belonging to the class in the header

Brueni
  • 53
  • 7