Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,561 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,902 people online right now. Registration is fast and FREE... Join Now!




accessing 1d array as 2d array

 
Reply to this topicStart new topic

accessing 1d array as 2d array, array

upant
11 Apr, 2008 - 12:22 PM
Post #1

D.I.C Head
**

Joined: 15 Feb, 2008
Posts: 84

here is my copy constructor..anyone has idea how can I make it for 2d arrays( this copy constructor is for 1d array)

I have private varivales : column, row, size and double *ptr


CODE
Array::Array(const Array &arrayToCopy)
:size(arrayToCopy.size)
{
    size = arrayToCopy.getSize();
    ptr = new double[size];
    for( int i = 0; i<size;i++)
    {
        ptr[i] = arrayToCopy[i];
    }
}



this works for 1d array but I don't know how can I make it for 2d array?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Accessing 1d Array As 2d Array
11 Apr, 2008 - 12:37 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,213



Thanked: 217 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Your for loop is going to be nested. The outside loop can be for your rows and your inner loop can be for your columns. Then what you will see in the inner loop is something along the lines of...

cpp

for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
// Copy cell at row i and column j of incoming array to ptr array.
ptr[i,j] = arrayToCopy[i,j];
}
}


Something along this line should work for you. This is assuming that you set your ptr 2d array to have the same rows and columns as your incoming arrayToCopy.

Hopefully this is what you were looking for. smile.gif

User is offlineProfile CardPM
+Quote Post

upant
RE: Accessing 1d Array As 2d Array
11 Apr, 2008 - 12:50 PM
Post #3

D.I.C Head
**

Joined: 15 Feb, 2008
Posts: 84

here is what am I trying to do. I am trying to access 1d array as 2d array
my header file is :-

CODE
#ifndef ARRAY2D_H
#define ARRAY2D_H

#include <iostream>
using std::ostream;
using std::istream;

class Array2D
{
    friend ostream &operator<<( ostream & , const Array &);
    friend istream &operator>>( istream &, Array &);
public:
    Array2D(int,int);
    Array2D( const Array2D &);
    ~Array2D();
    int getSize() const;

    const Array2D &operator = (const Array2D &);
    bool operator ==(const Array2D &) const;

    bool operator!=(const Array2D &right) const
    {
        return!(*this == right);
    }

    double operator() (double,double);

    double &operator() (double, double) const;
private:
     int size;
     double *ptr;
     int row;
     int column;
};












and my .cpp is

CODE
#include <iostream>
using std::cerr;
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;

#include <cstdlib>
using std::exit;

#include "Array2D.h"

Array2D::Array2D(int rows, int columns)
{
    row = rows;
    column = colmns;
    size = rows * columns;
    ptr = new double[size];

    for(int i = 0; i<size; i++)
    {
        ptr[i] = 0;
    }
}

Array2D::~Array2D()
{
    delete [] ptr;
}

int Array2D::getSize() const
{
    return size;
}


Array2D::Array2D(const Array2D &arrayToCopy)
:size(arrayToCopy.size)
{
    size = arrayToCopy.getSize();
    ptr = new double[size];
    for( int i = 0; i<row;i++)
    {
        for(int j = 0; j<column;j++)
        {
          ptr[i] = arrayToCopy[i];
        }
    }
}


double Array2D::operator()(int row1, int col1)
{
    int index;
    index = column *row1 + col1;
    return ptr[index];
}

double &Array2D :: operator()(int row2, int col2)
{
    int index;
    index = (columns *row2) + col2;
    return ptr[index];
}

const Array2D &Array2D::operator=( const Array2D &right)
{
    if( &right != this)
    {
        delete [] ptr;
        size = row*column;
        ptr = new double[ size];
        row = right.row;
        col = right.column;
    }
    for( int i = 0; i <size; i++)
    {
        ptr[i] = right.ptr[i];
    }
    return *this;
}

bool Array2D::operator==( const Array2D &right) const
{
    if( row != right.row)
        return false;
    else if(col!= right.col)
        return false;

    for( int i = 0; i< size; i++)
        if(ptr[ i] ! = right.ptr[ i])
            return false;

    return true;
}

istream &operator>>(istream &input , Array &a)
{
    for(int i = 0; i <a.size; i++)
    {
        input a.ptr[i];
    }
    return input;
}

ostream &operator<<(ostream &output, const Array &a)
{
    for(int i = 0; i <a.size;i++)
    {
        output<<a.ptr[i];
    }
    return output;
}




will that work for here? I don't think soo right??


User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Accessing 1d Array As 2d Array
11 Apr, 2008 - 01:04 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,213



Thanked: 217 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
I am not sure what you mean by accessing 1d array as 2d array. Your code there shows a one dimension array the size of rows * columns (which btw you have no access to). No where yet have you defined anywhere showing a 2d array.

So what you are showing me here is that you have your class called Array2D for which the copy constructor is another Array2D, both have one dimensional arrays which you can copy straight over between one another. Now are you attempting to do something like row = 1 and column =1 and have it return the value from the one dimensional array?

This post has been edited by Martyr2: 11 Apr, 2008 - 01:05 PM
User is offlineProfile CardPM
+Quote Post

upant
RE: Accessing 1d Array As 2d Array
11 Apr, 2008 - 01:10 PM
Post #5

D.I.C Head
**

Joined: 15 Feb, 2008
Posts: 84

yes, like I should store all data in a dynamically allocated one dimensional array but I need to keep track of the number of rows and columns.

Array2D u1(2,3); //2 rows and 3 columns
u1(0,0) = 15;
u1(2,3) = u1(0,0)
cout<<u1<<endl

This is what am I supposed to test for this program.


User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Accessing 1d Array As 2d Array
11 Apr, 2008 - 02:12 PM
Post #6

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,213



Thanked: 217 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You know this program is riddled with errors.

1) Some functions you refer to your column private variable as col, others you refer to it as column
2) Your >> and << operator overloads are taking Array when Array is undefined (unless you got that somewhere else)
3) You have things like input a.ptr[i]; in your >> operator overload so you are missing the >> operator there
4) You have some operator definitions in the .h file does not match the definitions in your .cpp file.
5) You have two definitions for the () operator, both which do practically the same thing. Might want to scale that down to one.
6) last but not least, your constructor that takes an Array2D, you aren't going be able to do ptr[i] = arrayToCopy[i]; because you haven't defined an operator to pull out indexes. It is a class now, not a simple array.

So lets fix these up and perhaps we can continue. smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:38PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month