# Thread: Some Quick Help

1. ## Some Quick Help

Hey

I'm trying to flip an apmatrix like this:

original matrix

0 1 2
3 4 5

flipped/reversed

543
210

So basically it takes the first number and flips it with the last number and does it til the end.. heres my code so far

Code:
```#include <iostream.h>
#include "apmatrix.h"

int main()
{

int x,y,num=0;
cout<< "Num of Rows and Columns ";
cin >> x;
cin >> y;

apmatrix <int> mat(x,y);

int a,b;

for (a=0;a<mat.numrows();a++)
{
for (b=0;b<mat.numcols();b++)
{
mat[a][b] = num;
num++;
}
}

cout <<'\n';

for (a=0;a<mat.numrows();a++)
{
for (b=0;b<mat.numcols();b++)
{
cout << mat[a][b];
cout << "   ";

}
cout << endl;
}

// Reverse and Flip thing

int z,b,length,temp;

a=0;
b=0;
c=m.numrows();
d=m.numcols();

for (

return(0);
}```

2. ## Question

Whose the babe on the car??

can you also include your .h file??

She looks cute??

3. there is no userdefined .h file. apmatrix.h is created by the APCS people, designed for High School APCS students.

it should be fairly similar to 2d array linguistics.

but here is a copy of it anyway.

Code:
```#ifndef _APMATRIX_H
#define _APMATRIX_H

#include "apvector.h"

// *******************************************************************
// Last Revised: 8/14/98
//               changed abort() to exit(1), dhj
//
// APCS matrix class
//
// extends apvector.h to two dimensional "safe" (range-checked) matrices
// examples are given at the end of this file
// *******************************************************************

template <class itemType>
class apmatrix
{
public:

// constructors/destructor
apmatrix( );                                      // default size 0 x 0
apmatrix( int rows, int cols );                   // size rows x cols
apmatrix( int rows, int cols,
const itemType & fillValue );           // all entries == fillValue
apmatrix( const apmatrix & mat );                   // copy constructor
~apmatrix( );                                     // destructor

// assignment
const apmatrix & operator = ( const apmatrix & rhs );

// accessors
int numrows( ) const;                             // number of rows
int numcols( ) const;                             // number of columns

// indexing
const apvector<itemType> & operator [ ] ( int k ) const;  // range-checked indexing
apvector<itemType> & operator [ ] ( int k );              // range-checked indexing

// modifiers
void resize( int newRows, int newCols );   // resizes matrix to newRows x newCols
// (can result in losing values)
private:

int myRows;                             // # of rows (capacity)
int myCols;                             // # of cols (capacity)
apvector<apvector<itemType> > myMatrix; // the matrix of items
};

// *******************************************************************
// Specifications for matrix functions
//
// To use this class, itemType must satisfy the same constraints
// as forvector class.
//
// Any violation of a function's precondition will result in an error  message
// followed by a call to exit.
//
// constructors/destructor
//
//  apmatrix( );
//     postcondition: matrix of size 0x0 is constructed, and therefore
//                    will need to be resized later
//
//  apmatrix( int rows, int cols );
//     precondition: 0 <= rows and 0 <= cols
//     postcondition: matrix of size rows x cols is constructed
//
//  apmatrix( int rows, int cols, const itemType & fillValue );
//     precondition: 0 <= rows and 0 <= cols
//     postcondition: matrix of size rows x cols is constructed
//                    all entries are set by assignment to fillValue after
//                    default construction
//
//  apmatrix( const apmatrix<itemType> & mat );
//     postcondition: matrix is a copy of mat
//
//  ~apmatrix( );
//     postcondition: matrix is destroyed
//
// assignment
//
//  const apmatrix & operator = ( const apmatrix & rhs );
//     postcondition: normal assignment via copying has been performed
//                    (if matrix and rhs were different sizes, matrix has
//                    been resized to match the size of rhs)
//
// accessors
//
//  int numrows( ) const;
//     postcondition: returns number of rows
//
//  int numcols( ) const;
//     postcondition: returns number of columns
//
// indexing
//
//  const apvector<itemType> & operator [ ] ( int k ) const;
//     precondition: 0 <= k < number of rows
//     postcondition: returns k-th row
//
//  apvector<itemType> & operator [ ] ( int k );
//     precondition: 0 <= k < number of rows
//     postcondition: returns k-th row
//
// modifiers
//
//  void resize( int newRows, int newCols );
//     precondition: matrix size is rows X cols,
//                   0 <= newRows and 0 <= newCols
//     postcondition: matrix size is newRows X newCols;
//                    for each 0 <= j <= min(rows,newRows) and
//                    for each 0 <= k <= min(cols,newCols), matrix[j][k] is
//                    a copy of the original; other elements of matrix are
//                    initialized using the default constructor for itemType
//                    Note: if newRows < rows or newCols < cols,
//                          elements may be lost
//
//  Examples of use:
//
//     apmatrix<double> dmat( 100, 80 );       // 100 x 80 matrix of doubles
//     apmatrix<double> dzmat( 100, 80, 0.0 ); // initialized to 0.0
//     apmatrix<apstring> smat( 300, 1 );      // 300 strings
//     apmatrix<int> imat;                     // has room for 0 ints

#include "apmatrix.cpp"
#endif```
this actually includes apvector.h also

apvector.h
Code:
```#ifndef _APVECTOR_H
#define _APVECTOR_H

// *******************************************************************
// Last Revised: 8/14/98, abort changed to exit
//
// January 13,1998, added explicit to int constructor
// APCS vector class template
//
// implements "safe" (range-checked) arrays
// examples are given at the end of this file
// *******************************************************************

// If your compiler supports the keyword explicit, comment out the
// #define explicit line below, leaving the #define means explicit
// is ignored, but doesn't generate an error
//
//   This will disallow a typically erroneous implicit type-conversion:
//   vector<int> v( 10 );
//   v = 0; // Oops!! Allowed because of implicit type-con2lversion.

#define explicit

template <class itemType>
class apvector
{
public:

// constructors/destructor
apvector( );                        // default constructor (size==0)
explicit apvector( int size );      // initial size of vector is size
apvector( int size, const itemType & fillValue ); // all entries == fillValue
apvector( const apvector & vec );   // copy constructor
~apvector( );                       // destructor

// assignment
const apvector & operator = ( const apvector & vec );

// accessors
int  length( ) const;                   // capacity of vector

// indexing
itemType &       operator [ ] ( int index );       // indexing with range checking
const itemType & operator [ ] ( int index ) const; // indexing with range checking

// modifiers
void resize( int newSize );             // change size dynamically;
// can result in losing values
private:

int  mySize;                            // # elements in array
itemType * myList;                      // array used for storage
};

// *******************************************************************
//  Specifications for vector functions
//
//  The template parameter itemType must satisfy the following two conditions:
//   (1) itemType has a 0-argument constructor
//   (2) operator = is defined for itemType
//  Any violation of these conditions may result in compilation failure.
//
//  Any violation of a function's precondition will result in an error message
//  followed by a call to exit.
//
//  constructors/destructor
//
//   apvector( )
//     postcondition: vector has a capacity of 0 items, and therefore it will
//                    need to be resized
//
//   apvector( int size )
//     precondition: size >= 0
//     postcondition: vector has a capacity of size items
//
//   apvector( int size, const itemType & fillValue )
//     precondition: size >= 0
//     postcondition: vector has a capacity of size items, all of which are set
//                    by assignment to fillValue after default construction
//
//   apvector( const apvector & vec )
//     postcondition: vector is a copy of vec
//
//   ~apvector( )
//     postcondition: vector is destroyed
//
//  assignment
//
//   const apvector & operator = ( const apvector & rhs )
//     postcondition: normal assignment via copying has been performed;
//                    if vector and rhs were different sizes, vector
//                    has been resized to  match the size of rhs
//
//  accessor
//
//   int  length( ) const
//     postcondition: returns vector's size (number of memory cells
//                    allocated for vector)
//
//  indexing
//
//   itemType &       operator [ ] ( int k )       -- index into nonconst vector
//   const itemType & operator [ ] ( int k ) const -- index into const vector
//     description: range-checked indexing, returning kth item
//     precondition: 0 <= k < length()
//     postcondition: returns the kth item
//
//  modifier
//
//   void resize( int newSize )
//     description:  resizes the vector to newSize elements
//     precondition: the current capacity of vector is length; newSize >= 0
//
//     postcondition: the current capacity of vector is newSize; for each k
//                    such that 0 <= k <= min(length, newSize), vector[k]
//                    is a copy of the original; other elements of vector are
//                    initialized using the 0-argument itemType constructor
//                    Note: if newSize < length, elements may be lost
//
//  examples of use
//      apvector<int> v1;         // 0-element vector
//      apvector<int> v2(4);      // 4-element vector
//      apvector<int> v3(4, 22);  // 4-element vector, all elements == 22.

#include "apvector.cpp"
#endif```

4. yea that doesnt help :-) but i seriously could use a little pointing in the right direction, you dont have to code it for me.. just a hint or two.. :-)

5. actually, that was posted for correlcj;

anyways...i pondered on it a bit...have you tried to set a=c and b=d

umm...try to swap the first slot with the last slot...possibly use a decrement...

edit:: sorry if this doesn't help much, i just dont feel like thinking today...

6. yea I thought aboiut that and I really dont know how to do it.. could someone please help me.. :-D

I always get out of bounds and stuff, anyone got any ideas.

7. why are you getting out of bounds errors?

remember matrices start at 0 and are 1 less than length;

for example:

if you want this matrix:
0 1 2
3 4 5

to look like this:
5 4 3
2 1 0

code would look like this:

Code:
```apmatrix<int> name(2, 3);
apmatrix<int> name2(2, 3);

//actually now that i think of it, send it to a function, return an apmatrix, and do a const reference parameter, store it in a diff apmatrix.

apmatrix function(const apmatrix<int> &grid);

name2 = function(name);

do all the swaps in the function

//sorry about the confusion, some of it is in main, apmatrix function... is outside.

//something like this.```
edit: name has 3 ints, but they are called as such
name[0][0];
name[0][1];
name[0][2];

you cannot do name[0][3] or you'll get out of bounds errors;

8. well i have this but it doesn't seem to work...

Code:
```a=0;
b=0;
int c=mat.numrows();
int d=mat.numcols();

int totallength=mat.numrows()*mat.numcols();

cout << totallength;

for (a=0;a<mat.numrows();a++)
{
for (b=0;b<mat.numcols();b++)
{
temp=mat[a][b];
for (c; c<0; c--);
{
for(d; d<0; d--);
{
temp2=mat[c][d];
}
}
}

mat[a][b]=temp2;
mat[c][d]=temp;
}```
Basically waht im trying to do is 4 nested for loops, 2 for the beginning and 2 for the end, and store the first one in a temp, store the last one in a temp2 and switch.. but doesnt work.

9. try c > 0 and d > 0, instead of < 0

10. It won't work with 4 nested for loops, since the inner two will complete every time the outer two increment, not what you want. Try this:
Code:
```for (a=0, c=mat.numrows()-1;a<mat.numnumrows();a++, c--)
{
for (b=0, d=mat.numcols()-1;b<mat.numcols();b++, d--)
{
swap(mat[a][b], mat[c][d]);
}
}```
And BTW, your out of bounds errors were coming from the fact that you were setting c and d to the number of columns and rows, but you needed to subtract one from both to account for row and column 1 being 0.

11. well it doesnt flip anything, it outputs the same...

Code:
```for (a=0, c=mat.numrows()-1;a<mat.numrows();a++, c--)
{
for (b=0, d=mat.numcols()-1;b<mat.numcols();b++, d--)
{
temp=mat[a][b];
temp2=mat[c][d];
mat[a][b]=temp2;
mat[c][d]=temp;
}
}```

12. LOL, oops it reverses, then re-reverses itself again! Forgot an important part, to not swap anymore once rows*coloumns*1/2 swaps have been done! Try this:
Code:
```int stop=0;
for (a=0, c=mat.numrows()-1;a<mat.numnumrows();a++, c--)
{
for (b=0, d=mat.numcols()-1;b<mat.numcols();b++, d--)
{
if (stop<numcols*numrows/2)
{
swap(mat[a][b], mat[c][d]);
stop++;
}
}
}```
That should *hopefully* work!

13. it works thanks.. :-D

Popular pages Recent additions