sorry if this a bit of a pain and there are other threads to read about this but can anyone explain how arrays work a bit, i need a quick refresher course.

like how they work, are they like the equivalent of stacks in assembly language? and say you set up an array like

array[20][60]

meaning there are 20 rows of 60 columns?

so how would u change whats stored in each one?

i can expand any of it if i have been vague.

thanks for reading

a one-dimension array is defined by type name[dimension]; or explicitly type name[] = { element1, element2, ... , elementn } where n = dimension. It has exactly n elements, indexed from 0 to n-1. For example, an array of five int would be defined either by int myarray[5]; or by int myarray[] = { 1, 2, 3, 4, 5 } . To access the element you can use array[position] where 0 <= position < dimension of the array. To assign the numbers from 1 to 5 to an array of int you could do

int myarray[5];
myarray[0] = 1;
myarray[1] = 2;
myarray[2] = 3;
myarray[3] = 4;
myarray[4] = 5;

For 2-D arrays the syntax is quite the same: int my2Darray[5][5]; would declare a matrix of 5 rows and 5 columns, and you could access every element exactly like you did with 1-D arrays:

int my2Darray[3][3];
my2Darray[0][0] = 0;
my2Darray[0][1] = 1;
my2Darray[0][2] = 2;
my2Darray[1][0] = 3;
my2Darray[1][1] = 4;
my2Darray[1][2] = 5;
my2Darray[2][0] = 6;
my2Darray[2][1] = 7;
my2Darray[2][2] = 8;

would define the matrix

0 1 2
3 4 5
6 7 8

For N-D arrays you go on like this...

i know how to give each one random numbers, if i went

myarray[0][1] = code for random number;
.
.
.
.
.
.
.
myarray[20][60] = code for random number;

is there a way to make all of them random numbers with one or 2 lines of code?

well you could use loops, something like

for(int i = 0; i < 20; i++) {
     for(int j = 0; j < 60; j++) {
          myarray[i][j] = "code for random number";
     }
}

well you could use loops, something like <snip>

Arrays and loops go together like peanut butter and jelly. Good things separately, great thing together.

Comments
lol :-D

is there a way in 2-D arrays to overwrite or change full rows if array[row][col]

is that the same as the loops from earlier only doing the loop for one dimension?

You will have to do a loop for the row, handling it element by element, just as in a 1D array.

You can write code that handles just the 1D data, and call it for whichever row you want out of the 2D array. Example:

void clear_1D ( int ar[], int size )
{
    int i;
    for( i = 0; i < size; i++ )
        ar[i] = 0;
}

int data[10][20];
//fill the array with some stuff
//now I want row 2 set back to all 0
clear_1D( data[2], 20 );

sorry if this a bit of a pain and there are other threads to read about this but can anyone explain how arrays work a bit, i need a quick refresher course.

like how they work, are they like the equivalent of stacks in assembly language? and say you set up an array like

array[20][60]

meaning there are 20 rows of 60 columns?

so how would u change whats stored in each one?

i can expand any of it if i have been vague.

thanks for reading

Hey There ....

Arrays In C++ Are Simple To Understand.
All Ya Gotta Do Is To Conceptualize Arrays
As Rooms Numbered From 0, 1, 2, To X.
These Rooms Ya Call As Data Store Houses.
Now To Access, Retrieve Or Alter Data In Any Room You Would Have To Use The Room Number. For Example, If Room Number 3 Contains 3 People, We Can Access The Data As:

Room[3] = 3;

This Sets The Room 3 To The Value Of 3.

Similarly, Ya Can Access, Retrieve Or Alter The Data In Different Rooms Of An Array.
Once Ya Understand These One Dimensional Arrays ... Two Or Higer Dimensional Arrays Would Be Much Easier To Understand.....

If Ya Need Any More Assistance Please Mail Me At: [email removed]

REGARDS:
MUJASH

can you do calculations with individual cells

e.g. like array[1][2] * array[1][3], will that work

I suggest you to read this before you ask more questions. Read it carefully and you should be set an gone, and in case of doubt come back here for help.

Just a tip about

For Example, If Room Number 3 Contains 3 People, We Can Access The Data As:

Room[3] = 3;

This Sets The Room 3 To The Value Of 3.

: remember that Room Number 3 is the Fourth Room of your Hotel :)

This article has been dead for over six months. Start a new discussion instead.