Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Nov 1st, 2006, 9:38 AM   #31
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
I made the changes, and I also change the overload operator and draw so that it display correctly in the monitor.

I succeded to have to correct form ( the 20X30 table) but again a run time error:
Ichange the 2 entity like this:
   void Screen::draw()const
   {
        for( int i=0;i<height;i++)
            {for( int j=0;j<width;j++)
                 cout<<*(pixels+(i*width)+j);
                 cout<<endl;}
                 
   } 
        
    ostream& operator<<(ostream & out, const Screen & TV)
    { 
             for(int i=0;i<TV.height;i++)
                 {for(int j=0;j<TV.width;j++)
                    out<<*(TV.pixels+(i*TV.width)+j);
                    out<<endl;}// i changed the position of  the'}' here and in draw().
    }

debugging run time error is new I found them difficult to catch. what is the deal with those?
B.

Last edited by brad sue; Nov 1st, 2006 at 10:12 AM.
brad sue is offline   Reply With Quote
Old Nov 1st, 2006, 11:12 AM   #32
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 810
Rep Power: 4 The Dark is on a distinguished road
When I run it, I only get the run time error when a rectangle tries to write outside the screen. Then I get a runtime when it tries to free the screen.

Can you show your changed code?
The Dark is offline   Reply With Quote
Old Nov 1st, 2006, 3:42 PM   #33
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
I did disable all commands of other functions.
Here is my Screen class:
  class Screen
   {
         private:
                 char* pixels;
                 int height;//rows
                 int width;//columns
         public: 
                friend ostream& operator<<(ostream & out, const Screen & TV);
                Screen(int=10, int=10);
                Screen(const Screen&);
                ~Screen(void);
                void setH(int );
                int getH(void)const;
                void setW(int);
                int getW() const;
                void setPoint(const Point&,char);
                void resetPoint(const Point&);
                void clear(void);
                void draw(void)const;
   };

// implementation of screen class

   Screen::Screen(int r,int c)
   {
         int size;
         height=r;
         width=c;
         size=r*c;
         pixels=new char[size];
         for(int i=0;i<r;i++)
            {
              for(int j=0;j<c;j++)
                 {
                  *(pixels+(i*c)+j)='.';
                  }
            }
   }             
      
      
   Screen::~Screen(){delete []pixels;}
   
   void Screen::setPoint(const Point & object, char c)
   {
        int i=object.getX();
        int j=object.getY();        
        *(pixels+(i*width)+j)=c;
   }          
                 
      
   
   void Screen::resetPoint(const Point& object)
   {
        int x_del,y_del;
        x_del=object.getX();
        y_del=object.getY();
        *(pixels+(x_del*width)+y_del)='.';
   }   
   
   void Screen::clear()
   { int i,j;
         for(i=0;i<height;i++){
             for(j=0;j<width;j++)
                 *(pixels+(i*width)+j)='.';}
   }
    
    void Screen::setH(int row)
    {
         height=row;
    }
    
    int Screen::getH()const{ return height;}
    
    void Screen::setW(int col)
    {
         width=col;
    }
    
    int Screen::getW() const{ return width;}
    
         
    void Screen::draw()const
   {
        for( int i=0;i<height;i++)
            for( int j=0;j<width;j++)
                { cout<<*(pixels+(i*width)+j);
                 cout<<endl;}
                 
   } 
        
    ostream& operator<<(ostream & out, const Screen & TV)
    { 
             for(int i=0;i<TV.height;i++)
                 {for(int j=0;j<TV.width;j++)
                    out<<*(TV.pixels+(i*TV.width)+j);
                    out<<endl;}
    }
  

for the moment my main is:
int main()
{
    Screen ecran(20,30);
    cout<<"\n\nPrinting a blank Screen"<<endl;
    cout<<ecran<<endl;
    return 0;
}

It is the only changes I made beside the the two I did show above.
brad sue is offline   Reply With Quote
Old Nov 1st, 2006, 5:58 PM   #34
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 810
Rep Power: 4 The Dark is on a distinguished road
I don't know if it causes the problem, but you are not returning the stream in your << function.
The compiler should have warned you about this problem.
The Dark is offline   Reply With Quote
Old Nov 2nd, 2006, 1:31 AM   #35
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
thank you !Yes that was the issue .
I am struggling with a function that reduces the size of the screen by a factor x.
The problem is that I dont succeed to keep the location of the painted point s on the screen canvas. the redcution is done by deleting the remaining points
I coded this::
    void Screen::shrink(int reduc)
    { 
         if((reduc<height)&&(reduc<width))
           {  height=height/reduc;
              width=width/reduc;
                  for( int i=height;i<0;i--)
                      for( int j=width;j<0;j--)
                        delete[](pixels+(i*width)+i);
           }//end if           
    }

if the screen is:
4455*888
4455*888
5555*888
*******


if we do shrink(2), the screen should be:
4455
4455


In my function the size it OK but the location of the points is not kept. because I don't know how to 'jump' one line up to start deleting again.
I continue to work on it but Please if someone has a suggestion please send it to me.
B.
brad sue is offline   Reply With Quote
Old Nov 2nd, 2006, 6:36 AM   #36
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 810
Rep Power: 4 The Dark is on a distinguished road
Since you allocted pixels as one block of char, it is a very bad idea to try to delete[] the chars individually. You either can leave the block at its original size (and have some unused memory) or allocate a new block, copy the contents you want across and then delete the old block.

As for copying the data across to the new size, you need to keep track of the old size and use that to find the location of the pixels you want to copy from the old data.
The Dark is offline   Reply With Quote
Old Nov 2nd, 2006, 7:12 AM   #37
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Just an additional caveat, though it's implicit in The Dark's post: don't forget that your destructor is going to delete 'pixels', so be wary of how you mess with that.
__________________
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, 8:03 AM   #38
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
I think it would be better to change my constructor and destructor as allocate memory individually. I will try to do this first then tackle the 'shrink' again.
thank you for the suggestions.
brad sue is offline   Reply With Quote
Old Nov 2nd, 2006, 2:51 PM   #39
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
I tried to change my constructor but it fails.Then I tried this
void Screen::shrink(int reduc)
    { 
         if((reduc<height)&&(reduc<width))
           {  
            int newrow,newcol;
            newrow=height/reduc;
            newcol=width/reduc;
            char *newpixels=new char[newrow*newcol];
                //iterate the old block line by line            
            for ( int i = 0; i < newrow; i++)
              {
               memcpy( newpixels + (i*newcol), pixels + (i*width), newcol);  //copy only the required amount of data
              }
               //delete the old data     
            delete []pixels;      
            pixels = newpixels;
            
             } 
   }
I dont see my mistake but it does not work. My problem is that I still fail to keep track of the old data and save them in newpixels.
See where I am wrong in my code?
brad sue is offline   Reply With Quote
Old Nov 2nd, 2006, 3:32 PM   #40
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 810
Rep Power: 4 The Dark is on a distinguished road
Same problem as in post #29, which was answered in post #30
The Dark 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 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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:19 AM.

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