Hi, i'm trying to write a function that enlarges a given two dimensional array horizontaly and Verticaly by different numbers. So far i have got this code. Bu it doesnt really work at the moment. Please help.
Code:

void BitMap::enlarge(int horiz, int vert)
{
 bool temp[MaxSize][MaxSize];
 temp[0][0]=true;
 int m, k;

 for (int i = 0; i<numrows; i++)
        for (int j = 0; j <numcols; j++)
        temp[m][k]=myGrid[i*horiz][j*vert];


 for (int i = 0; i<m; i++)
          for (int j = 0; j<k; j ++)
          myGrid[i][j]=myGrid[i][j]+horiz;

}

I don't see any code that actually enlarges the 2d matrix. it just copies data from one array to another then back to the original again. how is myGrid defined? Is it dynamically or statically allocated? You need to post a bit more code that shows these details.

Hi,

Your code contains at least three major errors :

1.m and k don't get assigned anything anywhere
2.you make addition with an integer for an element of a bool (I suppose) array for no apparent reason. (you must have ment [j+hoirz] which wouldn't work neither)
3.You wouldn't need anything like myGrid[i*horiz][j*vert] unless you are doing some sort of Intrapolation.

Loren Soth

Thank you for your replay i worked out how to do it incase anyone needs it:

void BitMap::enlarge(int horiz, int vert) 
{    
   int enlR = numrows * vert;      
   int enlC = numcols * horiz;     
   bool temp[MaxSize][MaxSize];    

   for(int i = 0; i < enlR; i++)   
   {
        for(int j = 0; j < enlC; j++) 
                                {
            temp[i][j] = myGrid[i/vert][j/horiz];
                                }
   }

   numrows = enlR;
   numcols = enlC;

   for(int i = 0; i < numrows; i++) 
   {
        for(int j = 0; j < numcols; j++) 
                                {
            myGrid[i][j] = temp[i][j];
                                 }
   }
}

Dear DashM, correct me if I'm wrong; but since you are (supposedly) enlarging your BitMap with a given horiz and vert , shouldn't your temporary array be sized according to that array? In fact, shouldn't you be using new or malloc to create that array? You are trying to dynamically create an array at runtime so you can't predefine the size of the array like this at your source. I believe that is why you are forced to use MaxSize for your new grid. Furthermore, such an allocation on the stack is volatile and will be destroyed when the method returns.

One nested loop is sufficient to reassign all your values. Why do you use two?

Shouldn't you set your numcols and numrows to your new horiz and vert respectively?

I hope I've given you a rough idea of the things you need to do. If you still need help or clarification, feel free to post a reply. I'd be most willing to help.


P.S. I would recommend implementing either with memcpy or realloc (if you know how to use either) instead of using a nested loop. Its more efficient that way.

Dear DashM, your new code still seems weird to me. Can you explain how exactly is your class supposed to work?

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.