Arrays: Single and 2D
Arrays: Single and 2D
Single and 2D
Wh at is an a rra y?
Mark De Vera
How d o y ou d eclare a o ne-
di mens ion al arr ay in C?
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]
Mark De Vera
Wh at oper atio ns can y ou
perfor m on a rrays ?
Mark De Vera
Examples of read operations are:
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
Examples of combined read/write
operations are:
Mark De Vera
Can y ou pass arr ays as
parame ters?
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!
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.
Mark De Vera
AddArrays() Example…
void main(void)
{
double C[5], A[5], B[5];
int n;
clrscr();
AddArrays(C,A,B,4);
getch();
}
Mark De Vera
How d o y ou d eclare a two-
di mens ion al arr ay in C?
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
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 ?
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:
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.
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!
Mark De Vera
Sample Program: (Matrix)
#include<iostream.h>
int row, col;
Mark De Vera
Continuation:
}
Mark De Vera
Cont….
int IdentityMatrix(int M[][3])
{
int disp;
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