Lecture 06
Lecture 06
Lecture 06
Arrays
1 / 24
Lecture Outline
2 / 24
Primitive vs Array Declaration
declaration
type primitiveDeclaration ;
arrayDeclaration
,
primitiveDeclaration
identifier
= value
3 / 24
1D Array Declaration
arrayDeclaration
identifier [ integerVal ]
= initializer
Use arrays to work with a set of data values of the same type
Example:
double x[8]; x ? ? ? ? ? ? ? ?
An array represents a sequential group of memory locations
integerVal: size of the array
– pre-determined number of array elements
– must be as large as, or larger than, the maximum number
of values to be stored
4 / 24
1D Array Initialization
double x[8]={16.0,12.0,6.0,8.0,2.5,12.0,14.0,-54.5};
x 16.0 12.0 6.0 8.0 2.5 12.0 14.0 -54.5
5 / 24
Array Subscripts
subscriptedVariable
identifier [ integerExpr ]
6 / 24
Accessing Array Elements
Array elements are accessed individually
Example: fill array sq with squared values 02 , 12 , 22 , . . . , 102
#include <stdio.h>
#define SIZE 11
int main(void)
{
int i, sq[SIZE];
for (i = 0; i < SIZE; i++)
sq[i] = i * i;
for (i = 0; i < SIZE; i++)
printf("%d ", sq[i]);
printf("\n");
return 0;
}
8 / 24
1D Array as Function Argument
Passing an array to a function requires two parameters:
– the array (specifically, its location)
– the number of array elements to process
#include <stdio.h> /*
printArray prints first n
#define SIZE 11 elements of the array.
void printArray(int array[], Precondition: n >= 0
int n); */
int main(void) void printArray(int array[],
{ int n)
int i, sq[SIZE]; {
int i;
for (i=0; i<SIZE; i++)
sq[i] = i * i; for (i=0; i<n; i++)
printf("%d ", array[i]);
printArray(sq,SIZE); printf("\n");
return 0; return;
} }
9 / 24
1D Array as Function Argument
main
sq 0 1 4 9 16 25 36 49 64 81 100
printArray
6
array
11 n
sq, 11
printArray(sq,11); -
10 / 24
Example: Cumulative Sum
11 / 24
Example: Cumulative Sum
12 / 24
Example: Cumulative Sum
13 / 24
Declaring Arrays in main
14 / 24
2D Array Declaration
identifier [ intVal ]
= initializer
Example:
int t[4][3];
Assuming row-major ordering, specify the number of rows
followed by the number of columns
15 / 24
2D Array Initialization
int t[4][3]={{ 2, 3,-1},
{ 0,-3, 5},
{ 2, 6, 3},
{-2,10, 4}};
subscriptedVariable
identifier [ integerExpr ]
17 / 24
Accessing 2D Array Elements
Use nested for loops to access all elements
Example: multiplication table
#include <stdio.h>
#define NROWS 21
#define NCOLS 11
int main(void)
{
int i, j, mulTable[NROWS][NCOLS];
for (i = 0; i < NROWS; i++)
for (j = 0; j < NCOLS; j++)
mulTable[i][j] = i*j;
for (i = 1; i < NROWS; i++)
{
for (j = 1; j < NCOLS; j++)
printf("%4d", mulTable[i][j]);
printf("\n");
}
return 0;
}
18 / 24
2D Array as Function Argument
When using 2D array as function argument, pass the array
together with the number of rows and columns to process
#include <stdio.h> /*
#define NROWS 21 print2D prints r x c
#define NCOLS 11 table of elements, t
Precondition: r>=0, 0<= c<NCOLS
void print2D(int t[][NCOLS], */
int r, int c); void print2D(int t[][NCOLS],
int main(void) int r, int c)
{ {
int i, j;
int i, j, t[NROWS][NCOLS];
for (i = 1; i < r; i++)
for (i = 0; i < NROWS; i++) {
for (j = 0; j < NCOLS; j++) for (j = 1; j < c; j++)
t[i][j] = i*j; printf("%4d", t[i][j]);
print2D(t,NROWS,NCOLS); printf("\n");
return 0; }
} return;
}
Example:
1 2 3 1 3 6
T = 4 5 6 ⇒ S = 5 12 21
7 8 9 12 27 45
20 / 24
Example: 2D Cumulative Sum
#include <stdio.h>
#define SIZE 100
void readData(int t[][SIZE], int r, int c);
void print2D(int t[][SIZE], int r, int c);
void cumulativeSum(int t[][SIZE], int r, int c);
int main(void)
{
int t[SIZE][SIZE]={{0}}, r, c;
scanf("%d%d", &r, &c);
readData(t,r,c);
cumulativeSum(t,r,c);
print2D(t,r,c);
return 0;
}
21 / 24
Example: 2D Cumulative Sum
void readData(int t[][SIZE], int r, int c)
{
int i, j;
for (i = 1; i <= r; i++) /* start from row 1 */
for (j = 1; j <= c; j++) /* start from col 1 */
scanf("%d", &(t[i][j]));
return;
}
void print2D(int t[][SIZE], int r, int c)
{
int i, j;
for (i = 1; i <= r; i++) /* start from row 1 */
{
for (j = 1; j <= c; j++) /* start from col 1 */
printf("%4d", t[i][j]);
printf("\n");
}
return;
}
22 / 24
Example: 2D Cumulative Sum
23 / 24
Lecture Summary
1D arrays
– Declaration/initialization with pre-determined size
– Subscripting to assess individual elements
– Loops to access array elements
– Arrays as function arguments
2D arrays are simple extensions of 1D arrays
24 / 24