Lecture07_Arrays
Lecture07_Arrays
Your Task
Write a program that reads 10 numbers from the keyboard and then
stores them
2
Your Task
Write a program that reads 10 numbers from the keyboard and then
stores them
Solution 1
. . .
4
Array
To setup an array in memory, we must declare both the
name of the array and the number of cells associated with it:
double x[8];
5
Array
To process the data stored in an array, we reference each
individual element by specifying the array name and
identifying the element desired using a subscripted
variable.
6
Array
Declaration and Initialization
float prices[3];
prices[0] = 1.2;
prices[1] = 2.3;
prices[2] = 3.4;
7
Array
Declaration and Initialization
8
Array
Sample Manipulation of the elements of array x
9
Your Task
Write a program that reads 10 numbers from the
keyboard and then stores them
Solution 2
int i, numbers[10];
10
Array
Example
#define N 10
int i, numbers[N];
#include<stdio.h>
int main(){
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
return 0;
}
12
Array as Formal Parameters
‣ when an array with no subscript appears in the argument
list of a function call, what is actually stored in the
function’s corresponding formal parameter is the address
of the initial array element or the base address of the array
13
Array as Formal Parameters
Example
int i;
for(i=0;i<n;i++)
list[i] = value;
14
Array as Formal Parameters
Example (continuation..)
int main(){
int i, size=5,
y[size], value=1;
for(i=0;i<size;i++)
printf("%d ",y[i]);
return 0;
}
15
Array as Formal Parameters
16
Multidimensional Arrays
‣ an array with two or more dimensions
Two-Dimensional Array
‣ we will use to represent tables of data, matrices and other two-dimensional objects
Syntax
Example
double table[ROW_SIZE][COLUMN_SIZE];
void process_matrix(int in[][4], int out[][4], int row_count);
‣ the size of the first dimension is the only size that can be omitted
‣ multidimensional arrays are initiialized by listing values that are grouped by row
17
Multidimensional Arrays
Example
18