Professional Documents
Culture Documents
SME 1013 Programming For Engineers: Arrays
SME 1013 Programming For Engineers: Arrays
SME 1013 Programming For Engineers: Arrays
Programming for
Engineers
Arrays
by: Mohamed Hussein
compliment to:
Assoc. Prof. Dr. Mohd Shafiek Yaacob
Arrays
Arrays are a convenient way to work with large
quantities of data.
Each array has an index that allows us to locate
and manipulate the quantities stored in the array
Arrays are equivalent to subscripted variables
commonly used in mathematics In single-valued
variable format
xave = (x1 + x2 + … + xn)/n
In subscripted
variable format
x_ave = (x1 + x2 + … + xn)/n
(array)
1
Defining an Array
Arrays are defined with a name that is accompanied by
size specification, enclosed in square brackets.
A one-dimensional array definition may be expressed as
Example 9.1
Several typical one-dimensional array definitions
int x[100];
char text[80];
static char message[25];
static float n[12];
#define size[60]
. . .
char letter[size];
2
More on Array Definition
Automatic Arrays cannot be initialize in the definition
statement.
External and Static array definitions can include the
assignment of initial values if desired.
All individual array elements that are not assigned
explicit initial values will automatically be set to zero.
The array size need not be specified explicitly when
initial values are included as part of array definition.
When a string is assigned to an external or a static
character array, the array size specification is usually
omitted.
Example 9.3
Array definitions that include initial values
int digits[5]={1,2,3,4,5};
static float x[6]={0,0.25,0,-0.5,0,0};
char color[3]={‘R’, ‘E’, ‘D’};
int digits[]={1,2,3,4,5};
char color[]=“RED”
3
Processing an Array
Single operations which involved entire arrays are not
permitted in C.
#include <stdio.h>
main(){
float x[5], sum=0.0, ave; int k, count=0;
4
Example 9-9 Create a program that calculates
the dot product of two vectors
#include <stdio.h>
main(){
float a[3], b[3], dotpr=0.0; int k;
a[0]=1.0; a[1]=2.0; a[2]=3.0;
b[0]=9.0; b[1]=8.0; b[2]=7.0;
5
Example 9.10 Passing of an array to a function
main(){
int n; float avg, list[10];
. . .
avg = average(n, list);
. . .
}
#include <stdio.h>
void modify(int a[]);
main(){
int count, a[3];
a[1]=0; a[2]=1; a[3]=2;
printf(“%d %d %d\n”, a[1], a[2], a[3]);
modify(a);
printf(“%d %d %d\n”, a[1], a[2], a[3]);
}
6
Two-Dimensional Arrays
Two-dimensional arrays can be thought of as
representing a table of information.
To select a value from a table, we need to specify the
row and the column.
Two subscripts are needed.
Nested loop is usually used to control the indices.
A two-dimensional array definition can be written as
storage-class data-type array_name[m][n];
Initialization of a two-dimensional array will be assign
by row.
Example 9.17
Examples of array definitions
float table[50][50];
char page[24][80];
static record[m][n];
7
Sample 9-18 Store and display the following noise
measurements in a two-dimensional array.
Grid X=2m X=4m X=6m
#include <stdio.h> Y=1m 62 dB 80 dB 95 dB
main(){ Y=3m 65 dB 75 dB 82 dB
float sp[2][3];
int row, col;
8
Sample 9-20 Write a program that create an
identity matrix of size m x m.
#include <stdio.h>
main(){
int eye[50][50], m, j, k;
. . .
. . .