0% found this document useful (0 votes)
60 views9 pages

SME 1013 Programming For Engineers: Arrays

The document provides information on arrays in C programming. It defines arrays as a way to work with large quantities of data using indices. It discusses defining, initializing, processing, and passing one-dimensional and two-dimensional arrays. Examples are given for calculating values and operations using arrays, including summation, average, dot product, identity matrix, and matrix multiplication.

Uploaded by

jfl2096
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views9 pages

SME 1013 Programming For Engineers: Arrays

The document provides information on arrays in C programming. It defines arrays as a way to work with large quantities of data using indices. It discusses defining, initializing, processing, and passing one-dimensional and two-dimensional arrays. Examples are given for calculating values and operations using arrays, including summation, average, dot product, identity matrix, and matrix multiplication.

Uploaded by

jfl2096
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SME 1013

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)

x_ave = (x[1] + x[2] + … + x[n])/n

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

storage-class data-type array_name[expression];

 The storage-class is optional.


 It is sometimes convenient to define an array size in term
of a symbolic constant.

Example 9.1
Several typical one-dimensional array definitions

int x[100];
char text[80];
static char message[25];
static float n[12];

Array definition using symbolic constant

#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.

 Operations must be carried out on element-by-element


basis. This is usually accomplished within a loop.

 When it is desirable to assign initial values to the


elements of an array, the array must be defined either
globally or as static array.

Example 9-8 summation and average

#include <stdio.h>
main(){
float x[5], sum=0.0, ave; int k, count=0;

x[0]=5; x[1]=4; x[2]=3; x[3]=2; x[4]=1;

for(k=0; k<5; ++k)


{
sum=sum + x[k];
++count;
printf(“sum: %3.1f count: %d”,sum,count);
system{“pause”);
}
ave=sum/count;
printf(“sum=%f and average=%f\n”, sum, ave);
}

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;

for(k=0; k<3; ++k){


dotpr = dotpr + a[k]*b[k];
}

printf(“Dot Product = %f\n”, dotpr);

Passing Arrays to Functions


 An entire array can be passed to a function as an
argument.
 The array name appears without brackets as an actual
argument within the function call.
 The corresponding formal argument must be declared as
an array within the formal argument declarations with a
pair of empty square brackets.
 When writing function prototypes, an empty pair of
square brackets must follow the name of each array
argument.
 If an array is altered within the function, the alteration
will be recognized in the calling portion of the program.

5
Example 9.10 Passing of an array to a function

float average(int a, float x[]);

main(){
int n; float avg, list[10];
. . .
avg = average(n, list);
. . .
}

float average(int a, float x[]){


. . .
}

Example 9.11 Alteration of an array by a function.

#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]);
}

void modify(int a[]){


a[1]=-9; a[2]=-9; a[3]=-9;
return;
}

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];

Examples of array initializations


int values[3][4]={1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12};

int values[3][4]={ {1, 2, 3, 4},


{5, 6, 7, 8},
{9, 10, 11, 12} };

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;

sp[0][0]=62.0; sp[0][1]=80.0; sp[0][2]=95.0;


sp[1][0]=65.0; sp[1][1]=75.0; sp[1][2]=82.0;

for(row=0; row<2; ++row)


{
for(col=0; col<3; ++col)
{
printf(“%6.1f”, sp[row][col]);
}
printf(“\n”);
}
}

Create a program that calculates the


Sample 9-19 sound intensity at each grid point.
[Intensity = (sp^2)/410]
#include <stdio.h>
#include <math.h>
main(){
float sp[2][3], intsty[2][3]; int row, col;

sp[0][0]=62.0; sp[0][1]=80.0; sp[0][2]=95.0;


sp[1][0]=65.0; sp[1][1]=75.0; sp[1][2]=82.0;

for(row=0; row<2; ++row){


for(col=0; col<3; ++col){
intsty[row][col]=(pow(sp[row][col],2))/410;
printf(“%12.3e”, intsty[row][col]);
}
printf(“\n”);
}
}

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;

printf(“Enter the size m of the I matrix\n”);


scanf(“%d”, m);

for(j=0; j<m; ++j){


for(k=0; k<m; ++k){
eye[j][k] = 0.0;
if(j==k) eye[j][k] = 1.0;
printf(“%3d”, eye[j][k]);
}
}
}

Sample 9-21 Write a program that multiply two


matrices of agreeable size (dimension).
. . .

float a[50][50], b[50][50], c[50][50];

. . .

/* imax = number of rows in a */


/* jmax = number of columns in b */
/* kmax = number of rows in b (= columns in a) */

for(i=0; i<imax; ++i){


for(j=0; j<jmax; ++j){
c[i][j]=0.0
for(k=0; k<kmax; ++k){
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}

. . .

You might also like