two dimensional arrays in a class

Hi.

First, I want to create a two dimensional array. It is 50 rows by 2 columns.
The first column contains an ID #: 1, 2, 3, 4,...50
The second column contains associated values: 1,3,5,etc (odd numbers). I'm not inputting any values - I want to use a loop that will go through the 50 rows and assign values. It should look like this:

ID Value
1 1
2 3
3 5
4 7...


Is my code below right? I have an inkling that my logic is slightly off somewhere :\

This piece is a part within a class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class ZY
{
  public:
  void start();
  void end();

class zy: ZY
{
  const int row = 50;
  const int col = 2;
  int myArray[row][col];
    for (int i = 1; i < row; i++)
       for (int j = 1; j < row; j++)
          myArray [i][] = i+2;
          myArray [][j] = 1+2*j;
}
}


Thank you so much for your help!
Last edited on
im just a beginner aswell but that looks pretty messy,
I personally would use (but it could be wrong as all hell)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class ZY
{
  public:
  void start();
  void end();

class zy: ZY
{
  int myArray[50] [2];
    for (int i = 0, int j = 0; i < 50, j < 2 ; i++, j++)
       {
          myArray [i][j] = i + 2, 1 + j*2;
          (though I don't know if this bit's working code)
       }
}
}


hope this is right and that I've helped :)
Last edited on
Is my code below right? I have an inkling that my logic is slightly off somewhere


Have you tried it?
yes, i did and got multiple errors.

I tried the code above, and it seems to compile and build on MS VS.However, I don't get an output, so I"m not sure at all. Can anyone give me tips/pointers in what to do in this case?

In addition, could I take the same code and place it in a sub-class of a class?

Thanks for all your help - i'm v. novice!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{    
  const int row = 50;
  const int col = 2;
  int array1 [row][col];
    for (int i = 0, j = 0; i < row, j <col ; i++, j++)
	{ 
		array1[i][j] = i+1, 1+j*2;
		cout << array1[i][j] << endl;
	}
		system ("pause");
return 0;
}
Last edited on
yeah sorry it's that line I was worried about,
try

array1[i] [j] = (i+1) * (1+j*2)

(can't say I understand this logic so I'll butt out here and let someone with expertise settle your prob :))
Ok...I don't really get what any of the suggestions above are trying to do >.>

Ok, making array1[50][2] makes an array like this:

1
2
1 2 3 4 5 6 7 8 9 10 ... 50
1 2 3 4 5 6 7 8 9 10 ... 50


So to loop through it you would have to do this:

1
2
3
4
5
for(int i = 0; i < length; ++i) {
   for(int j = 0; j < height; ++j) {
      //accessing element array[i][j]...do whatever with it...
   }
}


I think you can figure out how to do separate things for each side.
Looks to me like ur for loop would only run twice because you only have 2 columns.

Try splitting your program into two loops; nest one inside the other, loop 50 values into the first array and then the other 50 into the next one.
Something like:

1
2
3
4
5
6
for (int i=0; i<col; i++) {
   for (int j=0; j<row; j++) {
      array[i][j] = value, value;
      cout << array etc.
   }
}


you can't do them at the same time because your column gets full faster than your row.
Hi Firedraco -

Isn't int [a][b] that the first bracket contains rows and the second column? So it'd be 50 rows with 2 columns.

Hello everyone ---
instead of using a static array, would lists or vectors be easier to use?

Thanks!
yep, would have been better to ignore me :)
just for reference here's how I did it

1
2
3
4
5
6
	for( int ndisp = 1; ndisp <= 9; ndisp++ )
	{
		if( x == 3 ) { y += 1; x = 0; }
		square[x] [y] = ndisp;
		x += 1;
	}


probably inefficient but it works.
Dont fret with the rows and columns stuff, its just a way to explain it to make it sound easier for beginners.
However you describe it the data is at point [x] [y].

For your program it is best to use the array.
I just realized that 2d arrays must be filled in by rows first and then column. What I want is to have the ID's in one column and the associated values in the next, but there is a pattern that is associated with the first column of ID's : 1,2,3, etc. and for the second column, it'd just contain the odd integers: 1, 3, 5, etc.

Umz, I took yours but can't seem to get an output -- can anyone test on their end to see if you're getting an output?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;

int main(){
  const int row = 100;
  const int col = 2;
  int array1 [row][col];
	for (int i=0; i<col; i++) {
		for (int j=0; j<row; j++) {
		array1[i][j] = i, 2*j+1;
		cout << array1 << endl;
		}}
  int array2 [row][col];
	for (int i=0; i<col; i++) {
		for (int j=0; j<row; j++) {
		array2[i][j] = i, 2*j+2;
		cout << array2 << endl;
		}} 
return 0;
}


If this isn't so ideal, would it be best then to create two one dimensional arrays and put them inside a larger array (2 dimensional)?

Please advise. thx!




Last edited on
God, everything above is horrible :P

You want a 2D Array in a class. You have 2 ways of doing this.

1) use a vector of vectors. Simple and you don't need to worry about memory management.

2) use a pointer to a pointer. A lil cleaner code, but you have to worry about memory management.

Method 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <vector>
using std::vector;

class MyClass {
 vector<vector<int> > my2DArray;
 const int Rows = 50;
 const int Cols = 2;

public:
 // Constructor
 MyClass() {
  // Make Vector Right Size
  my2DArray.resize(Rows);
  for (int i = 0; i < Rows; ++i)
   my2DArray[i].resize(Cols);
 }

 // De-Constructor
 ~MyClass() { }

 // Do Something
 void AddRandomStuffToArray() {
   my2DArray[someRow][someCol] = 12; // Etc
  }
}


Method 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class MyClass {

 int **p2DArray;
 const int Rows = 50;
 const int Cols = 2;
public:

 // Constructor
 MyClass() {
  // Allocate Memory
  p2DArray = new int*[Rows];
  for (int i = 0; i < Rows; ++i)
    p2DArray[i] = new int[Cols];
 }

 // De-Constructor
 ~MyClass() {
  // De-Allocate so we don't have memory leak
  for (int i = 0; i < Rows; ++i)
   delete [] p2DArray[i];

  delete [] p2DArray;
 } 

 // Do Something
 void doSomething() {
  p2DArray[someRow][someCol] = 12;
 }
}


Note: My code doesn't follow OO-Encapsulation. It's merely there to illustrate 2 methods of having a 2D Array in a class. Yes I am aware that I could make Method 2 a single-dimensional array by using (Rows*Cols), but meh.

Enjoy.
Z.
I just realized that 2d arrays must be filled in by rows first and then column. What I want is to have the ID's in one column and the associated values in the next, but there is a pattern that is associated with the first column of ID's : 1,2,3, etc. and for the second column, it'd just contain the odd integers: 1, 3, 5, etc.


1
2
3
4
5
for (int i = 0; i < Rows; ++i) {
 for (int j = 0; j < Cols; ++j) {
  my2DArray[i][j] = 1 + (i*cols) + (j*2);
 }
}


You cannot store 2 values in the location. The location is mapped by 2 Values (Row and Col). At Location Array[Row][Col] is 1 Value.

array2[i][j] = i, 2*j+2;
Is crap, you cannot store 2 values at 1 memory location.

I think your problem is a design one, more than a code one.


A solution to what you are trying to do. But not using a 2D array as it's pointless.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using std::cout;
using std::endl;

int main() {

  const int rows = 50;
  int myArray[rows] = {0}; // Only need 1D Array

  for (int i = 0; i < rows; ++i)
    myArray[i] = 1 + (i*2);

  for (int i = 0; i < rows; ++i)
    cout << (i+1) << ": " << myArray[i] << endl;

  return 0;
}
Last edited on
Right only one value goes in per run, my bad I did it wrong.
Just use the loop, enter the first 50 values on the first run then the second values on the one after.
this is how i would do it
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
 int rows = 50, cols = 2;
 int array[rows][cols];
 
 for(int i = 1, j = 1; i <= rows; i++)
 {
     array[i][0] = i;
     array[i][1] = j;
     cout << i << " " << j << endl;

     j += 2
 }


If you are only going to associate two values, I would not use a 2D array at all. If you forsee increasing the dimensions later, then my suggestion will not be applicable.

The STL has an associative container called a map. It relates two things: a key and a value. Since each of 1-50 is unique and can be related to a single odd number value, this container is ideal.

Consider:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <map>

using namespace std;

int main( int argc, char* args[] ) {

    // the map for odd numbers
    map<int,int> odd;

    // initialize the map
    for( int i = 1; i <= 50; ++i )
    {
        odd[i] = (i * 2) - 1;
    }

    // output the odd number associated with 10
    cout << odd[10] << endl;

    return 0;
}
Last edited on
Topic archived. No new replies allowed.