Click here to Skip to main content
15,906,947 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Read/Write C++ object members to ini file? Pin
Todd Smith7-Nov-01 16:10
Todd Smith7-Nov-01 16:10 
GeneralRe: Read/Write C++ object members to ini file? Pin
Tomasz Sowinski8-Nov-01 0:03
Tomasz Sowinski8-Nov-01 0:03 
GeneralRe: Read/Write C++ object members to ini file? Pin
Joaquín M López Muñoz8-Nov-01 3:42
Joaquín M López Muñoz8-Nov-01 3:42 
GeneralAbout CListCtrl Pin
7-Nov-01 15:18
suss7-Nov-01 15:18 
GeneralRe: About CListCtrl Pin
Mukkie8-Nov-01 8:09
Mukkie8-Nov-01 8:09 
GeneralCTreeCtrl with checkboxes Pin
Crercio O. Silva7-Nov-01 14:55
Crercio O. Silva7-Nov-01 14:55 
GeneralRe: CTreeCtrl with checkboxes Pin
Tomasz Sowinski8-Nov-01 0:12
Tomasz Sowinski8-Nov-01 0:12 
Generaldynamic memory allocate 2 dimensional arrays using 2 pointers Pin
Shannon Mesquite7-Nov-01 14:18
Shannon Mesquite7-Nov-01 14:18 
I have an assignment about writing 2 dimensional arrays class Array2D, but I don't know the syntax for using int *iptr and int **array2D to dynamically allocate memory. The following is my program. The class interface and main driver given as it is. I couldn't really find the error on my class Array2D implementation part. Please help me out.

#include <iostream>
using namespace std;

#include <iomanip>


#include <cstdlib>
#include <cassert>
#include "PA_02.h"






//Output the array in row & column format (table format)
//Overloaded output operator for class Array2D
ostream &operator<<( ostream &out, const Array2D &obj )
{
for ( int i = 0; i < obj.rows; i++ )
{
for ( int j = 0; j < obj.cols; j++ )
out <<setw(10)<< obj.array2d[i][i];
="" out="" <<="" "\n"="" <<endl;
="" }

=""
="" return="" out;

="" input="" the="" entire="" array="" contents
="" overloaded="" operator="" for="" class="" array2d
="" istream="" &operator="">>( istream &in, Array2D &obj )
{
cout<<"Enter array values, one row at a time:\n"<<endl;
for="" (="" int="" i="0;" <="" obj.rows;="" i++="" )
="" {
="" cout<<"enter="" column="" values="" row="" "<<="" <<":\n"<<endl;
="" j="0;" obj.cols;="" j++="" cout="" <<"\nenter="" value="" ("="" <<="" <<"="" ,"="" <<")="" :";=""
="" in="">> obj.array2D[i][j];;
}

}
cout<<"\n\nInput complete\n"<<endl;
return="" in;
="" }


="" initialization="" and="" termination="" methods
=""
="" dynamically="" create="" an="" initialized="" array,="" contents="" will="" be="" to="" value
="" array2d::array2d(="" int="" r,="" c,="" value="" ):rows(r),="" cols(c)
="" {
="" i,="" j;="" iptr="new" int[="" r="" ];
="" assert(="" !="0);
" array2d="&iptr;
" for(="" i="0;" i<r;="" i++="" )
="" j="0;" j<c;="" j++="" array2d[i]="new" int[j];
="" );
="" }
="" }=""

="" initialize="" array="" equals="" 0
="" for="" (="" <="" r;="" c;="" array2d[i][j]="value;
" }

="" the="" copy="" constructor
="" const="" &rhs="" )="" :="" rows(="" rhs.rows),=""
cols(rhs.cols)
="" j;
="" rows="" rows;="" j<cols;="" rhs="" into="" object
="" cols;="" assignment="" operator="" (performs="" assignment)
="" &array2d::operator="(" j,="" i;
="" if="" check="" self="" assignment
="" delete="" []="" iptr;
="" cols="" array2d;
="" i<rows;="" *this;

="" clean="" up="" (e.g.="" allocated="" memory)
="" array2d::~array2d(="" void="" cout<<"\ndestructor="" called"<<endl;
="" =set="" get="" methods.="" explicitly="" whether="" index="" is="" within="" bounds="" of="" array

="" set="" item="" in="" at="" location(="" row,="" col="" ).
="" returns="" false="" out-of-bounds,="" else="" true
="" bool="" array2d::set(="" &new_item,="" if(="" row="">0 && col>0 )
{
for( int i=0; i<row; i++="" )
="" {
="" for(="" int="" j="0;" j<col;="" j++="" array2d[i][j]="new_item;
" }
="" return="" true;
="" else
="" false;
=""
="" }

="" get="" an="" item="" in="" the="" array="" at="" location="" (="" row,="" col="" ).
="" returns="" false="" if="" index="" is="" out-of-bounds,="" else="" true.
="" bool="" array2d::get(="" &item,="" )="" const
="" if(="" row="">0 && row<=rows )
{
if( col>0 && col<=cols )
{
for( int i=0; i<row; i++="" )
="" {
="" for(="" int="" j="0;" j<col;="" j++="" array2d[i][j]="item;
"
="" }
="" return="" true;
="" else
="" false;
="" }

="" returns="" the="" length="" of="" array
="" array2d::size(="" void="" )="" const
="" (="" sizeof(="" array2d[rows][cols]="" );
="" number="" rows
="" array2d::getrow(="" rows;
="" columns
="" array2d::getcol(="" cols;
="" compare="" this="" array="" with="" <rhs=""> for equality. Two arrays are equal if
//their
//rows, and cols are equal and all the elements are equal
bool Array2D::operator==( const Array2D &rhs ) const
{
if ( rows != rhs.rows && cols != rhs.cols )
return false; //arrays of different size

for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < cols; j++ )
if ( array2D[i][j] != rhs.array2D[i][j] )
return false; //arrays are not equal

}
return true;
}

//Compare this array with <rhs> for inequality
bool Array2D::operator!=( const Array2D &rhs ) const
{
if ( rows == rhs.rows && cols == rhs.cols )
return false;

for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < cols; j++ )
if ( array2D[i][j] == rhs.array2D[i][j] )
return false;
}

return true;
}

//Overload () to allow use of ( i, j ) notation ( leftvalue )
int &Array2D::operator()( int i, int j )
{
if ( i >= rows || j >= cols )
cout<<"out of bounds"<<endl;

="" return="" array2d[i][j];
="" }
="" overload="" ()="" to="" allow="" use="" of="" (="" i,="" j="" )="" notation="" rightvalue="" )
="" const="" int="" &array2d::operator()(="" const
="" {
="" if="" i="">= rows || j >= cols )
cout<<"out of bounds"<<endl;
return="" array2d[i][j];
="" }


="" utility="" functions

="" returns="" true="" if="" <index=""> is within range, else false
bool Array2D::in_range( int r, int c ) const
{
if ( r < 0 || r >= rows && c < 0 || c >= cols )
return false;
else
return true;

}

//Perform dynamic memory allocation
void Array2D::allocate( void )
{
rows = ( rows>0 ? rowsBlush | :O );
cols = ( cols>0 ? colsBlush | :O );
iptr = new int[ rows ];
assert( iptr != 0 );
array2D = &iptr;
for( int i=0; i<rows; i++="" )
="" {
="" for(="" int="" j="0;" j<cols;="" j++="" array2d[i]="new" int[="" ];
="" assert(="" !="0" );
="" }
=""
="" }



--------------------------------------------------------------------------------

#include="" <iostream="">#include <cassert>
#include "PA_02.h"
#include "a2D.cpp"
using namespace std;

int main()
{
Array2D a1( 2, 3, 5 );
Array2D a2 = a1;
Array2D a3, a5( 4, 2, 3 );
Array2D a4( a3 );

//Test the << operator
cout<<"\nPrinting a1 ...\n"<<a1;
cout<<"\nprinting="" a2="" ...\n"<<a2;
="" a3="" ...\n"<<a3;
="" a4="" ...\n"<<a4;
="" a5="" ...\n"<<a5;

="" test="" the="">> operator
cin>>a3;
cout<<"\nPrinting a3 ...\n"<
GeneralRe: dynamic memory allocate 2 dimensional arrays using 2 pointers Pin
8-Nov-01 3:23
suss8-Nov-01 3:23 
GeneralCreateProcess Pin
7-Nov-01 12:27
suss7-Nov-01 12:27 
GeneralRe: CreateProcess Pin
Masaaki Onishi7-Nov-01 15:59
Masaaki Onishi7-Nov-01 15:59 
GeneralPassword Edit & XP Pin
7-Nov-01 12:17
suss7-Nov-01 12:17 
GeneralRe: Password Edit & XP Pin
Masaaki Onishi7-Nov-01 16:08
Masaaki Onishi7-Nov-01 16:08 
GeneralEntering a password in the Command Prompt Pin
Alvaro Mendez7-Nov-01 11:50
Alvaro Mendez7-Nov-01 11:50 
GeneralRe: Entering a password in the Command Prompt Pin
Chris Losinger7-Nov-01 12:02
professionalChris Losinger7-Nov-01 12:02 
GeneralRe: Entering a password in the Command Prompt Pin
Alvaro Mendez7-Nov-01 12:17
Alvaro Mendez7-Nov-01 12:17 
GeneralRe: Entering a password in the Command Prompt Pin
Nish Nishant7-Nov-01 13:57
sitebuilderNish Nishant7-Nov-01 13:57 
GeneralRe: Entering a password in the Command Prompt Pin
Michael P Butler7-Nov-01 22:01
Michael P Butler7-Nov-01 22:01 
GeneralRe: Entering a password in the Command Prompt Pin
Nish Nishant7-Nov-01 22:23
sitebuilderNish Nishant7-Nov-01 22:23 
GeneralRe: Entering a password in the Command Prompt Pin
Alvaro Mendez8-Nov-01 5:28
Alvaro Mendez8-Nov-01 5:28 
GeneralRe: Entering a password in the Command Prompt Pin
Alvaro Mendez8-Nov-01 6:16
Alvaro Mendez8-Nov-01 6:16 
GeneralRe: Entering a password in the Command Prompt Pin
Joaquín M López Muñoz7-Nov-01 12:20
Joaquín M López Muñoz7-Nov-01 12:20 
QuestionLoops and MFC? Pin
7-Nov-01 11:16
suss7-Nov-01 11:16 
AnswerRe: Loops and MFC? Pin
Stan Shannon7-Nov-01 11:24
Stan Shannon7-Nov-01 11:24 
GeneralRe: Loops and MFC? Pin
7-Nov-01 11:31
suss7-Nov-01 11:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.