2-dim array, variables are changing w/o me telling them to

i am working with 2 dimensional arrays... I fill a 2-dim array with numbers and have an int variable 'numRows'. For some reason when i re-assign the value of 'numRows' it also reassigns the value of 'array[0][0]'.....WHY??? PLEASE tell me where these 2 are connected or why this is happening... =(


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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
using namespace std;

class table
{
 private:
         int array[][5];
         int numRows;
 public:  
          void insertData();
          void print();         
};

int main()
{
    table numbers;
    numbers.insertData();
    numbers.print();
    
 system("pause");
 return 0;   
}

void table::insertData()
{
     array[0][0] = 0; //row 0 = column labels
     array[0][1] = 1;
     array[0][2] = 2;
     array[0][3] = 3;
     array[0][4] = 4;
     
     array[1][0] = 5; //row 1 = 5n
     array[1][1] = 10;
     array[1][2] = 15;
     array[1][3] = 20;
     array[1][4] = 25;
     
     array[2][0] = 10; //row 2 = 10n
     array[2][1] = 20;
     array[2][2] = 30;
     array[2][3] = 40;
     array[2][4] = 50;
     
     array[3][0] = 2; //row 3 = evens
     array[3][1] = 4;
     array[3][2] = 6;
     array[3][3] = 8;
     array[3][4] = 10;
     
     array[4][0] = 1; //row 4 = odds
     array[4][1] = 3;
     array[4][2] = 5;
     array[4][3] = 7;
     array[4][4] = 9;

     array[5][0] = 2; // row 5 = 2^n
     array[5][1] = 4;
     array[5][2] = 8;
     array[5][3] = 16;
     array[5][4] = 32;
    
     cout << array[0][0] << " " << numRows << endl;   //Output: 0 0
     numRows = 6;
     cout << array[0][0] << " " << numRows << endl;   //Output: 6 6
}

void table::print()
{  /* int i = 0, j = 0;
    while(i < numRows)
    {    
         j = 0;
         while(j < 5)
         {
              cout << array[i][j] << "          ";   
              j++;  
         }
         cout << endl;
         i++;
    } 
    */
}


when I call the print function the first row should be "0 1 2 3 4". I need it set up this way so that later i can make a select function that will create a new sub-table of different(smaller) size.

Thank you for any help anyone can offer.
I might be wrong but I think line 7 is incorrect because you're not declaring a size for the array
well since the array is part of a class it will be different sizes...it will always have 5 columns, but will have different numbers of rows.
my reference book says i dont need to declare the first parameter of the array...is this true? if not...how do i make a class of arrays that will be different sizes???
You don't need to define the first dimension of the array if you are initializing it at the time of definition. Like this:

 
int x[][2] = { {1, 1}, {2, 2} };


In that case, you still really know at compile time the first dimension.
If you want to create an array that is of varying size, unknown at compile time, you need to use a dynamically allocated array. This gets a little tricky.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int** x;                                             // an int pointer to an int pointer  
int variableSize = 3;                         // this variable can change, it holds your first dimension
x = new int*[variableSize];              // creates this size 3 array of int pointers;
for (int i = 0; i < variableSize; ++i)   // this loop iterates over the 3 elements of the int pointer array
  x[i] = new int[5];                // and creates a new dimension of size 5 for each element

/* now you have an array x of dimensions [3, 5] you can fill it with int values as normal 
    when you are finished with this array or want to change the dimensions, you need to 
    free the memory! like this: */

for (int i = 0; i < variableSize; ++i)
  delete[] x[i];                                   // use delete[] to free array of ints from each element of 
                                                        // first dimension
delete[] x;                                       //  finally, free the array representing the first dimension itself

/* now you can use new on x as shown in the first part of the code to make new dimensions */


You need to be careful when you use new and delete. delete[] is what is required to free arrays from memory. It is different from delete without the brackets. Do not double-delete your variables, you you will have problems. Only delete things one time after newing them one time.

Have fun.

~psault
Read this:
http://cplusplus.com/forum/articles/7459/

psault and mcleano are on the right track. I don't believe that you can declare an array in the class without a full declaration although I'm surprised that it compiles. If that class declaration compiles, I may have to research that further and figure out why.
Wow, this example compiles and runs but I don't know why. This seems like an interesting C++ trivia question. If it is simply undefined behavior, fine but why can't the compiler just complain about the struct declaration if it is wrong?

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

struct ArrayType
{
    int array[][2];
};
   
int main()
{
    ArrayType MyArray;
    MyArray.array[0][0] = 1;
    MyArray.array[0][1] = 2;
    MyArray.array[1][0] = 3;
    MyArray.array[1][1] = 4;

    for(int i = 0; i < 2; ++i)
	for(int j = 0; j < 2; ++j)
	    std::cout << MyArray.array[i][j] << "\n";

    std::cout << std::endl;
    return 0;
}


EDIT: According to the ISO std, section 9.2
8 Non-static (9.4) members that are class objects shall be objects of previously defined classes. In particular,
a class cl shall not contain an object of class cl, but it can contain a pointer or reference to an object
of class cl. When an array is used as the type of a nonstatic member all dimensions shall be specified.
Last edited on
Topic archived. No new replies allowed.