You are on page 1of 29

ARRAYS

Single and 2D
Wh at is an a rra y?

 An array is a group of elements with the same
data type (i.e., it is homogenous)
 An array is characterized by its name,
dimension, size and element data type
 An array has a dimension which we will denote
by d
- when d = 1, we say that the array is 1-
dimensional; it is also referred to as a list
- when d = 2, we say that the array is 2-
dimensional; it is also referred to as a table

Mark De Vera
How d o y ou d eclare a o ne-
di mens ion al arr ay in C?

The syntax is:
<array name> [<index>]

i.e., simply type the array name, followed by the left
square bracket, followed by the array element index and
finally by the corresponding right square bracket.

Key Points!
- the range of values that you can use as array index is
from 0 to <size> - 1
- the first element therefore is <array name>[0]
- the last element is <array name>[<size> - 1]

Mark De Vera
Examples of valid array references
are:

 s[0]
 a[5]
 MyArray[5]

Examples of invalid array element
references are:
 s[-1] // index cannot be negative
 X[2.5] // index cannot be a real number
 Data[1000] // last index should size - 1

Mark De Vera
Wh at oper atio ns can y ou
perfor m on a rrays ?

 Only operations on a per-array element basis are
possible. Operations such as array to array
assignment, or adding two arrays are not supported
by C language.

There are basically two operations that can be
performed on array elements, namely:

array element
 write operation, specifically, overwrite the original
content of an array element

Mark De Vera

printf(“%d\n”, a[1]);
/* assume that i was declared as an int
variable*/
for (i = 0; i < 1000; i++)
printf(“%lf\n”, Data[i]);

Mark De Vera
Examples of write operations are:

s[0] = ‘c’;
X[2] = 3.1416;
/* assume that i was declared as an int
variable*/
for (i = 0; i < 25; i++)
X[i] = i + 1;

Mark De Vera
operations are:

M[3] = a[2] + MyArray[10];
Data[55] = X[9]/2.5 * a[1];

Mark De Vera
Can y ou pass arr ays as
parame ters?

 Yes, it’s almost like passing simple variables
as parameters.

 Example: The following code shows an
example of a function that accepts an integer
array as parameter.

 The function simply prints all the elements
from index 0 to n-1.

Mark De Vera
Example 1
void PrintArrayElements(int A[], int n)
{
int i;
for (i = 0; i < n; i++)
printf(“Element number %d\n”, i);
}
void main (void)
{
int MyArray[20], n;
clrscr();
PrintArrayElements(MyArray, 20);
getch();
}

Mark De Vera
Key Points!

- note that the size of the one-
dimensional array is optional in the
parameter declaration
- n in the example function above need
not be the same as the size of the array;
it maybe less than the size but cannot be
more because otherwise a logical error
will occur

Mark De Vera
Example
 The following code shows an example that will add two arrays, i.e., on
a per element basis. The sum will be stored on a third array.

void AddArrays(double C[], double A[], double B[], int n)
{
int i;
for (i =0; i < n; i++)
{
printf(“\nEnter values for A[%d]”,i);
scanf(“%lf”,&A[i]);
printf(“Enter values for B[%d]”,i);
scanf(“%lf”,&B[i]);
C[i]=A[i] + B[i];
printf(“The values for C[%d] = %lf\n”,i,C[i]);
}
}

Mark De Vera
void main(void)
{
double C[5], A[5], B[5];
int n;
clrscr();
getch();
}

Mark De Vera
How d o y ou d eclare a two-
di mens ion al arr ay in C?

The syntax is:

<data type> <array name> [ <row size> ] [<column size>]

The array elements are arranged in row-
major order.

Mark De Vera
Examples:

int m[3][3];
int MatrixA[3][3];
float MatrixB[4][8];
double Table[5][20];

Mark De Vera
The graphical representation of two-
dimensional array, in the case, for example
of m is shown below.
Ex. int m[3] [3];
2

A graphical representation of a matrix
Mark De Vera
Inside RAM

Mark De Vera
How d o y ou refer en ce a n
el emen t in a tw o-d imen sio nal
arr ay ?

The syntax is:
<array name> [<row index>][<column index>]

Key Points!
- the range of row index is from 0 to <row size> - 1
- the range of column index is from 0 to <column size> - 1
- the first element therefore is <array name>[0][0]
- the last element is <array name>[<row size> - 1][<column size> - 1]

Mark De Vera
Examples of valid array access
are:

printf(“%d\n”, MatrixA[0][2]);
MatrixB[3][1] = 1.25;
Table[0][0] = Table[1][2] + Table[5][1];

Mark De Vera
Examples of invalid array access
are:

printf(“%d\n”, MatrixA[1,2]); // syntax error!
MatrixB[-1][1] = 1.25; // negative index
Table[5.2][0] = 3.1416; // incorrect row index

Mark De Vera
How d o y ou p ass tw o-
di mens ion al arr ay s as function
parame ters?

Basically the same as in the one-dimensional array case, but the size of
the column will have to supplied! It is not optional like the row size.

Example: The following function will print a table with 5 rows and 10
columns.

void PrintTableElements(int A[2][3])
{
int i, j;
for (i = 0; i < 5; i++)
for (j = 0; j < 10; j++)
printf(“Row %d, column %d element = %d\n”,i, j, A[i][j]);
}

Mark De Vera
2D Array Example Cont’n…
void main(void)
{
int A[2][3],i,j;
clrscr();

for(i=0;i<2;i++)
for(j=0;j<3,j++)
{
printf(“Enter values: ”);
scanf(“%d”,&A[i][j]);
}
PrintTableElements(A);
getch();
}

Mark De Vera
Key Points!

- normally, a double loop is used as control
structure for processing two-dimensional
array as shown in the previous example
- also, normally, the processing is done
row-by-row, and column-by-column
within the same row

Mark De Vera
Sample Program: (Matrix)
 #include<iostream.h>
 int row, col;

 void InputData(int M[][3])
 {
 for (row=0; row<3; row++){
 for (col=0; col<3; col++){
 cout<<"Input M["<<row<<"]["<<col<<"]:";
 cin>>M[row][col];
 }
 }

Mark De Vera
Continuation:
 }

 void PrintData(int M[][3])
 {
 for(row=0; row<3;row++){
 for (col=0; col<3; col++){
 cout<<M[row][col];
 }
 cout<<"\n";
 }
 }

Mark De Vera
Cont….
 int IdentityMatrix(int M[][3])
 {
 int disp;

 for(row=0; row<3; row++){
 for(col=0; col<3;col++){

if((M[row]==M[col])&&(M[row+1]==M[col+1])&&(M[row+2]==M[col+2])) disp=1;
 else disp=0;
 //else if((M[row]!=M[col])==1 &&) disp=0;

 }

Mark De Vera
Cont….
 }
 return(disp);

 }

 void main(void)
 {
 int M[3][3];
 int count;

Mark De Vera
Cont…

 InputData(M);
 PrintData(M);
 count = IdentityMatrix(M);
 if(count==1)
 cout<<"square matrix\n";
 else if(count==2)
 cout<<"both\n";
 else if(count==0)
 cout<<"inline\n";
 }

Mark De Vera
Mark De Vera