You are on page 1of 10

BIL104E

INTRODUCTION TO
SCIENTIFIC AND ENGINEERING
COMPUTING (C)

WEEK 6

One dimensional arrays:

array elements: i++

An array is a collection of variables that are of the same data type

Datatype:
yp integer
g
Array-Name: array_int
Array-Size: 8

Data-type: character
Array-Name:
y
dayy
Array-Size: 7

Example
int array[6];
for(i=1; i<=6; i++){
array_int[i] = i*i;
printf(array[%d] = %d*%d = %d \n, i, i, i, array_int[i]);
}
Output:
array[1] = 1*1 = 1;
array[2] = 2
2*2
2 = 4;
array[3] = 3*3 = 9;
array[4] = 4*4 = 16;
array[5] = 5
5*5
5 = 25;
array[6] = 6*6 = 36;

array elements: J++

Multidimensional arrays

array elements: i++

i array_int[5][8]
int
i [5][8]

Datatype: integer
Array-Name: array_int
Array-Size1: 5
Array-Size2: 8

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


f ( 0 j<8;
for(j=0;
8 j++);
)
array_int[i][j] = i*j;
printf(array_int[%d][%d]=%d \n,i,j, array_int[i]);
}
}

i=0; j++
Array_int[0][0] = 0;
Array_int[0][1] = 0;
....
Array_int[0][6] = 0;
Array_int[0][7] = 0;

i=0
j=0

i=0
j=1

i=0
j=2

.....

.....
.

.....

.....

i=0
j=7

i=1
j=0

i=1
j=1

i=1
j=2

.....

.....

.....

.....

i=1
j=7

.
.

.
.

.
.

.
.

i=4
j=0

i=1; j++
Array_int[1][0] = 0;
Array_int[1][1] = 1;
....
Array_int[1][6] = 6;
Array_int[1][7] = 7;

i=4
j=1

.....

.....

.....

.....

i=4
j=6

i=4
j=7

i=4; j++
...................

Array_int[4][0] = 0;
Array_int[4][1] = 4;
....
Array_int[4][6] = 24;
Array_int[4][7] = 28;

Application I
Application-I

Write a program that calculates the elements of a


two dimensional array A by

A[i][j] = (i+j)*x
where
h
both
b th dimensions
di
i
and
d x will
ill be
b asked
k d from
f
user.

dimension1=3, dimension2=3, x = 2
if indexes are started from 1
4
6
8

6
8
10

8
10
12

if indexes are started from 0


0
2
4

2
4
6

4
6
8

Application-II
Application
for(i=0;
for(i
0; i<N;i++){

#i l d <
#include
<stdio.h>
tdi h>

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

int main(){

printf("%d \t ",A[i][j]);

int i,j,N, M, x;
printf("Please enter the first dimension
\n");
(
)
scanf("%d",&N);
printf("Please enter the second
dimension \n");
scanf("%d",&M);
scanf(
%d ,&M);
printf("Please enter the values of x \n");
scanf("%d",&x);
int A[N][M];
for(i=0; i<N;i++){
(j j
j ){
for(j=0;j<M;j++){
A[i][j]= (i+j)*x; }
}

}
printf("\n");
}
return 0;

Passing one dimensional array to


functions

Assume an array that


A
h is named
d array1 which
h h has
h N
integer elements and assume that the array will be
passed to the function arrayfunc
int array1[N];
arrfunc(array1,
(
y , N);
);

Function prototype:

int arrfunc(int array[], int arraysize)


main function

arrfunc

int array1[N];
arrfunc(array1,N);

int arrfunc(int array[], int arraysize)


Do array calculations vs.

Passing two dimensional array to


functions

Assume an array that


A
th t iis named
d array2
2 which
hi h has
h N
rows and M columns that have N*M integer elements
and assume that the arrayy will be p
passed to the
function arrayfunc
int array2[N][M];
arrfunc(array2,
arrf nc(arra 2 N)
N);

Function prototype:

int arrfunc(int
( array[][M],
y[][ ], int rowsize))
main function

arrfunc

int array2[N][M];
arrfunc(array2,N);

int arrfunc(int array[][M], int rowsize)


D array calculations
Do
l l ti
vs.

Application II
Application-II

Write a program that calculates the elements of a two dimensional array A[N][M]
A[i][j] = (i+j)*x
where column dimension will be defined (#define M 4 ), but the row dimension and
x will be asked from user.
user
Calculations will be performed in a function and the array will be passed to
function
1. Define integers
2. Want row dimension
from user
ser
3. Want x value from user
4. Define array1[N][M]
5 Call arrayfunc and pass
5.
array1 to function
6. Print results

int arrayfunc( ?, ?, ? ){
Calculate A[i][j] = (i+j)*x
(i+j) x
}

Application II
Application-II
#include <stdio.h>
#define M 4
void arrayfunc(int array2[][M], int
rowsize, int y)
int main(){
int i,j,N,x;
printf("Please enter the row size
\n");
scanf("%d",&N);
printf("Please
i f("Pl
enter the
h value
l off x
\n");
scanf("%d",&x);
i t A[N][M];
int
A[N][M]
arrayfunc(A,N,x);
printf("According to the calculation
A[i][j] = (i+j)*x
(i+j)*x, \n A MATRIX : \n \n
\n");
);

for(i=0; i<N;i++){
for(j=0;j<M;j++){
printf("%d
p
(
\t \t",A[i][j]);
j)
}
printf("\n");
}
return 0;
}
void arrayfunc(int
y
array2[][M],
y
int
rowsize, int y){
int i,j;
for(i=0; i<rowsize;i++){
for(j=0;j<M;j++){
array2[i][j]= (i+j)*y;
}
}
}

You might also like