for some reason when i run the program the print method for the array gets called instead of the one for the matrix..but it jus print out "array index out of bound".which is the exemption...the code is attached
Dr EUU 0 Newbie Poster
/*
the Integer is user define class which jus wraps an int into its type Integer..so its not a
problem..you can change an use normal int.
*/
#include <iostream>
#include "integer.h"
class dynamicArrayList{
protected:
Integer *theList; //the dynamic array i
int listSize; //max number of elements for which memory is allocated
public:
dynamicArrayList(int Size){
if (Size > 0)
{
listSize = Size;
theList = new Integer [listSize];
for (int i = 0; i < listSize; i++)
theList[i] = 0;
}
else
{
listSize = 0;
theList = NULL;
}
};
~dynamicArrayList(void){
delete [] theList;
theList = NULL;
};
const dynamicArrayList& operator=(const dynamicArrayList& obj){
if(this != &Obj) //avoid self copy
{
listSize = Obj.listSize;
theList = new Integer[listSize];
for(int i = 0; i < listSize; i++)
{
theList[i] = Obj.theList[i];
}
}
return *this;
};
dynamicArrayList(const dynamicArrayList& obj){
listSize = obj.listSize;
theList = new Integer[listSize];
for(int i = 0; i < listSize; i++)
{
theList[i] = obj.theList[i];
}
};
void print(){};// print the list
void InsertObject(Integer& obj, int idx){
if(idx >= 0 && idx < listSize)
{
theList[idx] = Obj;
}
else
cout<< " array index out of boud" << endl;
};
};
class Matrix{
protected:
int row;
int colu;
dynamicArrayList** matrix;
public:
FixedSizeMatrix(Integer row_,Integer col_){
row = row_;
colu = col_;
matrix = new dynamicArrayList*[row];
for(int i=0 ;i< row_;i++)
matrix[i] = new dynamicArrayList[colu];
};
~FixedSizeMatrix(){
for(int i=0;i< row;i++)
delete[] matrix[i];
delete[] matrix;
matrix = NULL;
row = 0;
colu = 0;
};
FixedSizeMatrix(const FixedSizeMatrix& other){
row = other.row;
colu = other.colu;
for(int i=0; i < other.row ;i++){
for(int j=0; j < other.colu ; j++){
matrix[i][j] = other.matrix[i][j];
}
}
};
FixedSizeMatrix& operator=(const FixedSizeMatrix& other){
row = other.row;
colu = other.colu;
for(int i=0; i< other.row ;i++){
for(int j=0; j < other.colu ; j++){
matrix[i][j] = other.matrix[i][j];
}
}return *this;
};
//can u help with this function?
//i want it to add different element in the matrix;
void Insert(Integer& elem){
//i dont know what to specify in the index of the matrix..
//i tried to use a for loop to add the same elements in but did not work..
for(int i= 0;i< row ;i++)
for(int j=0;j< colu;j++)
matrix[i][j].InsertObject(elem,j);
//this tells me that the array index is out of bound and for some reason the print() of
//dynmamicArrayList gets called..
};
void print(){} //prints the matrix;
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
How do you know which of the two methods are called? Neither actually do anything (just do-nothing methods)
Dr EUU 0 Newbie Poster
How do you know which of the two methods are called? Neither actually do anything (just do-nothing methods)
i gave em implementation, the code i posted here is a prototype.......
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.