![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#41 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Do you ever consider using your debugger or output statements to see what's going on?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#42 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
About the debugger, I really tried but there is a bunch of commands that does not make too much sense to me.
About the comment of The Dark, refering to post#29 , I think I do not need to set the values of height and width since it is a member function. so I still get the error. Last edited by brad sue; Nov 2nd, 2006 at 9:51 PM. |
|
|
|
|
|
#43 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
Quote:
I assume that that is the error you are seeing - its not printing correctly - as you haven't told us what the error is. |
|
|
|
|
|
|
#44 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
Sorry the problem was as you said that it print a mix of good and garbage values. I just fix it. But the location is not conserved.
what happened is that the function constructs the newpixels not by cutting the row and column that are beyond the new range, but filling out the new pixel with the old values one after the other. That is it reads beyond the range and put those values in the newpixels. Here below is an example OLD pixels(6x10) 8888999111 8888999111 5555999000 2222222222 2222222222 2222222222 NEW pixels with my function shrink(2)- reduce by 2 (3x5); 88889 99111 88889 WRONG!! instead of the good format that must be: 88889 88889 55559 I don't know if you see what is happening.I cannot fix it |
|
|
|
|
|
#45 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
Post your new shrink code.
|
|
|
|
|
|
#46 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
void Screen::shrink(int reduc)
{
if((reduc<height)&&(reduc<width))
{
height=height/reduc;
width=width/reduc;
char *newpixels=new char[height*width];
//iterate the old block line by line
for ( int i = 0; i < width; i++)
{
memcpy( newpixels + (i*width), pixels + (i*width),width); //copy only the required amount of data
}
//delete the old data
delete []pixels;
pixels = newpixels;
}
} |
|
|
|
|
|
#47 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
Re-read post 36 again. You had it almost right before, but now you are wiping out the old size with the new size when you still need to use it.
Your code in post #39 was almost right - all you needed to do was assign the values of newrow and newcol to height and width after you have finished using them in the memcpy loop. |
|
|
|
|
|
#48 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
I am overloading the operator* to perform the intersection of 2 rectangle.
The function works well in the sense that it finds the intersection. However, when printing, the program does not display the rectangle corresponding the left operand. My function is set up like this: Rectangle Rectangle:: operator*(const Rectangle& r2)
{
Rectangle result;
int xi,yi;// coordinate of upper left point intersection of 2 rectangles
int urx,lry;//oordinate of lower right point intersection of 2 rectangles
if( upLeft.getX()> r2.getX() )
xi=upLeft.getX();
else
xi=r2.getX();
if( upLeft.getY() > r2.getY() )
yi=upLeft.getY();
else
yi=r2.getY();
if( (upLeft.getX()+width ) < (upLeft.getX()+r2.getWidth() ) )
urx= upLeft.getX()+width;
else
urx=upLeft.getX()+r2.getWidth();
if( (upLeft.getY()+height ) < (r2.getY()+r2.getHeight() ) )
lry= upLeft.getY()+height;
else
lry=r2.getY()+r2.getHeight();
upLeft.setPoint(xi,yi);
width=urx-xi;
if(width<0)
width=0;
height=lry-yi;
if(height<0)
height=0;
result.set(upLeft,width,height,'*');
return result;
}rectar[5]=rectar[3]*rectar[4];
rectar[5].setPaint('5');it does not display r3 but display r5 and r4. why is that? |
|
|
|
|
|
#49 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
I think the best way for you to find whats wrong in these cases is to learn to use the debugger. Failing that, you should add cout statements in important places in your code, so that you can see what the values of certain variables are.
Note that in this case, your operator * is changing the values of the height and width member variables of the current object. I'm pretty sure you don't want to be doing that. |
|
|
|
|
|
#50 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Brad, the first thing you have to know is what your code is supposed to do. I mean that quite seriously; it has to click in you mind, whether it's from imposed requirements or from your own devising. You then devise a way to do that. Along the way you encounter new things that you must conquer, so you refer to literature and to the forums, whatever is necessary for you to gain that knowledge. Then you produce it. Then it doesn't work. Part of that "doesn't work" is that you didn't implement it correctly. Those errors can be of many kinds (syntax, fuzzy thinking, etc.), but sometimes they reflect a lack of thought in the beginning that is now going to require even more thought in the correction.
You may easily think, "it doesn't work." It's not quite as easy to think, "it doesn't work because the presentation line wraps, but the content doesn't." You have to identify WHAT the failure is, how it manifests itself. Then you have to determine WHY. Information is crucial; it is the key. If your program is not producing enough information in its normal operation, then you have to make it produce more or you have to use a tool that can peep inside and gather that additional information. Until you learn to do that, your corrective efforts are going to take much longer than the production effort. This post is not meant to be derogatory, but explanatory. I think that you are ready to move on to more mature code production, so you need to think seriously about incorporating these quite-normal approaches.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C# corruption!!! | Kilo | C++ | 32 | May 21st, 2006 8:44 PM |
| Masm | rsnd | Assembly | 4 | May 20th, 2006 9:05 PM |
| libraries | matko | C | 1 | Jan 22nd, 2006 2:12 PM |
| HELP please!!! | hamacacolgante | C | 7 | Nov 21st, 2005 5:36 AM |
| New to Java - Compiling Error | Kiwwa | Java | 4 | Jun 16th, 2005 10:25 AM |