7 Replies - 865 Views - Last Post: 11 November 2010 - 06:35 PM Rate Topic: -----

#1 tKc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 38
  • Joined: 25-October 10

2D Arrays

Posted 11 November 2010 - 01:55 AM

I need help in using 2d arrays in c++. I want to output a table that looks like this
0 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
except i want to do it using a 2d array and have the user input a value (n) and the table to be nxn.
I literally have no idea where to start on this so any amount of help or suggestions would be greatly appreciated.
#include <iostream>
using namespace std;
int main()
{
int n, i, j;
cin >> n;
int board[n][n], array[n][n];

for(i=0;i<n; i++)
{
for(j=0;j<n;j++)
{
array[n][n]>>board[i][j];
}
}

for(i=0;i<n;i++){
cout<<endl;
for(j=0;j<n;j++){
cout<<" "<<board[i][j];
}
}

system("PAUSE");
return 0;
}


this is my code thus far, pretty much all it does right is output the correct number of columns and rows but not the correct values and im pretty sure im going wrong with int board[n][n], array[n][n];

Is This A Good Question/Topic? 0
  • +

Replies To: 2D Arrays

#2 Anarion  Icon User is online

  • The Persian Coder
  • member icon

Reputation: 308
  • View blog
  • Posts: 1,508
  • Joined: 16-May 09

Re: 2D Arrays

Posted 11 November 2010 - 02:11 AM

Let's see what your loop looks like:
for(i=0;i<n; i++) {
    for(j=0;j<n;j++) {
        array[n][n]>>board[i][j];
    }
}


Explain what you are trying to achieve with array[n][n]>>board[i][j]; ?
Was This Post Helpful? 1
  • +
  • -

#3 tKc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 38
  • Joined: 25-October 10

Re: 2D Arrays

Posted 11 November 2010 - 02:20 AM

View PostAnarion, on 11 November 2010 - 01:11 AM, said:

Let's see what your loop looks like:
for(i=0;i<n; i++) {
    for(j=0;j<n;j++) {
        array[n][n]>>board[i][j];
    }
}


Explain what you are trying to achieve with array[n][n]>>board[i][j]; ?

i guess i figured it worked the same as cin>> so that a nxn array would get the values of i and j and since they are incremented it would be
1 2 3 4 5 6
7 8 9 10 11 12... etc
Was This Post Helpful? 0
  • +
  • -

#4 Anarion  Icon User is online

  • The Persian Coder
  • member icon

Reputation: 308
  • View blog
  • Posts: 1,508
  • Joined: 16-May 09

Re: 2D Arrays

Posted 11 November 2010 - 02:39 AM

operator>> is a function and is not defined for all types; it can be seen usually in stream objects like the cin you said. But there are two problems here:
  • This function is an extractor, used to extract data from the object in the left and assign it to the one on right side
  • This function is not defined for an integer object


Consider the nature of an integer array:
int array[10];

The above line is used to create an array of 10 integers. Now, look at this:
cout<<array[0];

This outputs the first integer of the array to standard output(cout). So, basically an array[index] is an int(here).

To assign integers, you can use operator= function defined for integer objects, like this:
int a(5), b; //two dummy integers, one with default value of 5
b = a; //use operator= to assign a to b


Given that, if you want to assign a value to elements of an array, you can simply do like this:
int array[10];
for(int i=0; i<10; i++) {
    array[i] = i+1; //use the value of i which is updated in this loop
}

After the loop, you have an array with values in the range [1, 10]. So, what you need to do is, change that line in your loop to assign the right values using i and j. Note that you may need to add some value to the loop variable to get the correct values for assigning(like here, I added 1 to i).
Was This Post Helpful? 3
  • +
  • -

#5 tKc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 38
  • Joined: 25-October 10

Re: 2D Arrays

Posted 11 November 2010 - 03:20 AM

View PostAnarion, on 11 November 2010 - 01:39 AM, said:

Given that, if you want to assign a value to elements of an array, you can simply do like this:
int array[10];
for(int i=0; i<10; i++) {
    array[i] = i+1; //use the value of i which is updated in this loop
}

After the loop, you have an array with values in the range [1, 10]. So, what you need to do is, change that line in your loop to assign the right values using i and j. Note that you may need to add some value to the loop variable to get the correct values for assigning(like here, I added 1 to i).


okay so lets say i have a 2d array now, would i work the same?
for(i=0;i<n; i++)
{
for(j=0;j<n;j++)
{
                board[i][j] += 1;
}
}
cout << board[i][j]<< endl;


Was This Post Helpful? 0
  • +
  • -

#6 Anarion  Icon User is online

  • The Persian Coder
  • member icon

Reputation: 308
  • View blog
  • Posts: 1,508
  • Joined: 16-May 09

Re: 2D Arrays

Posted 11 November 2010 - 03:39 AM

View PosttKc, on 11 November 2010 - 12:50 PM, said:

okay so lets say i have a 2d array now, would i work the same?
for(i=0;i<n; i++)
{
for(j=0;j<n;j++)
{
                board[i][j] += 1;
}
}
cout << board[i][j]<< endl;

Although the concept is the same, what you are doing above is not what you are supposed to do. You are just incrementing uninitialized integers, and the results are random(somehow). What you need to do is use the values of i and j, to update all ints of your array. For example:
#include <iostream>
using namespace std;

int main() {
    int array[4][4];
    for(int i=0; i<4; i++) {
        for(int j=0; j<4; j++) {
            array[i][j] = j + i;
        }
    }
    for(int i=0; i<4; i++) {
        for(int j=0; j<4; j++) {
            cout<<array[i][j]<<"-";
        }
    }
    return 0;
}

But this will not give you the outputs you are after. Find out how you can modify j + i so that you get the results you are asked for. First, try to understand how things work, then go after the right answer :)

This post has been edited by Anarion: 11 November 2010 - 10:12 AM

Was This Post Helpful? 1
  • +
  • -

#7 tKc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 38
  • Joined: 25-October 10

Re: 2D Arrays

Posted 11 November 2010 - 02:38 PM

okay i found that by replacing j+i with one value it works properly. this is my code thus far and seems to work
#include <iostream>
using namespace std;

int main(){
    int n;
    cin >> n;
    int array[99][99];
    int value(0);
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            array[i][j] = value++;
        }
    }
    for(int i=0; i<n; i++) {
            cout << endl;
        for(int j=0; j<n; j++) {
            cout<<array[i][j]<<"  ";
        }
    }


    system("PAUSE");
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#8 tKc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 38
  • Joined: 25-October 10

Re: 2D Arrays

Posted 11 November 2010 - 06:35 PM

okay, now that i understand this, my next dauting problem is how to replace a number on the grid with either a b or w when the user inputs? i am thinking i might be able to use a cordinate system, and reset value to 0 each time. something like (y = int(value) / 20, x = value % 20)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1