Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 2nd, 2006, 4:41 PM   #41
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 2nd, 2006, 10:39 PM   #42
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
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 10:51 PM.
brad sue is offline   Reply With Quote
Old Nov 2nd, 2006, 11:13 PM   #43
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by brad sue View Post
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.
You have changed the size of the pixels array to match the new width and height. When you next try to set a pixel or print the screen, the pixels member variable will be pointing to a buffer that matches the new width and height, but the width and height member variables will still have the old values. This will mean that you won't print what you are expecting to see.

I assume that that is the error you are seeing - its not printing correctly - as you haven't told us what the error is.
The Dark is offline   Reply With Quote
Old Nov 2nd, 2006, 11:48 PM   #44
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
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
brad sue is offline   Reply With Quote
Old Nov 3rd, 2006, 12:42 AM   #45
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
Post your new shrink code.
The Dark is offline   Reply With Quote
Old Nov 3rd, 2006, 8:43 AM   #46
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
    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;           
             } 
   }
brad sue is offline   Reply With Quote
Old Nov 3rd, 2006, 9:02 AM   #47
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Nov 4th, 2006, 2:53 AM   #48
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
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;
     }
So when I called the function
rectar[5]=rectar[3]*rectar[4];
    rectar[5].setPaint('5');

it does not display r3 but display r5 and r4.
why is that?
brad sue is offline   Reply With Quote
Old Nov 4th, 2006, 5:27 AM   #49
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Nov 4th, 2006, 8:54 AM   #50
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# corruption!!! Kilo C++ 32 May 21st, 2006 9:44 PM
Masm rsnd Assembly 4 May 20th, 2006 10:05 PM
libraries matko C 1 Jan 22nd, 2006 3:12 PM
HELP please!!! hamacacolgante C 7 Nov 21st, 2005 6:36 AM
New to Java - Compiling Error Kiwwa Java 4 Jun 16th, 2005 11:25 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:46 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC