jeudi 23 juillet 2015

get function not returning what i expect

I am trying to create a get function in my class to return that will take i, j argument and return the value of what is located at Object(i, j).

So far in my get function it is converting i, j to its equivalent in a 1d array by i * (number of columns) + j, which in my code would equal 5. Next I want to show what value is at location 5 in my array. However it seems to return the first value of my array, not the location 5 value. Any ideas to where I am going wrong? am I using the pointer in the wrong way? My full program is below:

 #include <iostream>
    using namespace std;

    class MyMatrix{

    public:
        //default constructor set member variables to null states
        MyMatrix();
        MyMatrix(int sizeR, int sizeC, double * input_data);        
        ~MyMatrix();    //destructor

        //member functions
        int get(int i, int j);

    private:
        int m;      //rows
        int n;      //columns
        double * data;
    };

    int main(){

        const int rows = 3;
        const int columns = 2; 
        double * userInput;

        cout << "The array is 3*2, or 6 elements." << endl;

        userInput = new double[rows*columns];
        double temp;
        for (int i = 0; i < rows*columns; i++){             //let the user type in the array
            cout << "Please enter value" << i << endl;
            cin >> temp;
            *(userInput + i) = temp;
        }

        MyMatrix ObjectA(rows, columns, userInput); //creating object with specifications
        cout << "The value of ObjectA 2,1 is: " << ObjectA.get(2, 1) << endl;


        return 0;
    }

    MyMatrix::MyMatrix(){
        cout << "MyMatrix constructor lets go" << endl;
        m = 0;
        n = 0;
        data = 0;
    }

    MyMatrix::MyMatrix(int sizeR, int sizeC, double * input_data){
        cout << "MyMatrix::MyMatrix(int sizeR, int sizeC, double * input_data) is called." << endl; //Showing the constructor working :)
        m = sizeR;
        n = sizeC;

        data = new double[m*n];
        for (int i = 0; i < m*n; i++){
            data[i] = *input_data;
        }

        cout << "The items you have entered are:" << endl;      //printing out array showing the array is filled
        for (int i = 0; i < m*n; i++){
            cout << i << "item is: " << *(input_data + i) << endl;
        }
    }

    int MyMatrix::get(int i, int j){
        cout << "getFunction is happening" << endl;

        //val should just be K  [K = i * N + j]
        int val = 0;
        val = i * n + j;        //n is COLUMNS, m is ROWS derp
        cout << "val is equal to: " << val << endl; //so val would be 5
        //how do i get it to display what is at location 5 in object A

        return data[val]; // shouldnt this return the 5th element in data?
    }


    MyMatrix::~MyMatrix(){
        cout << "MyMatrix::~MyMatrix() is invoked" << endl; //showing DESTRUCTA function is working
        delete[] data;  //the memory management 

    }

Aucun commentaire:

Enregistrer un commentaire