0% found this document useful (0 votes)
23 views

04 02 Multivalued Variable Array

This document discusses arrays in procedural programming. It begins by defining arrays as collections of variables of the same type that are referred to through a common identifier. The document then covers declaring and accessing single and multi-dimensional arrays, including how to calculate the memory space reserved. It demonstrates examples of declaring, initializing, and accessing elements of single and multi-dimensional arrays in C code. The document concludes by mentioning additional topics like displaying memory addresses of arrays.

Uploaded by

Jusen Samosir
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)
23 views

04 02 Multivalued Variable Array

This document discusses arrays in procedural programming. It begins by defining arrays as collections of variables of the same type that are referred to through a common identifier. The document then covers declaring and accessing single and multi-dimensional arrays, including how to calculate the memory space reserved. It demonstrates examples of declaring, initializing, and accessing elements of single and multi-dimensional arrays in C code. The document concludes by mentioning additional topics like displaying memory addresses of arrays.

Uploaded by

Jusen Samosir
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/ 20

Array

Procedural Programming

Mario Simaremare, S.Kom., M.Sc.


Program Studi Sarjana Sistem Informasi
Institut Teknologi Del
Objectives

• The objective of this session is the following:


• The students are able to elaborate the basic concept of array
and its structure.
• The students are able to develop solutions using either
single or multi dimensional array of primitive data type.

Procedural Programming 2
Please see this material first

Procedural Programming 3
Outlines

1. PEMVIS’ “Array”.
2. Array: definition.
3. Multi-dimensional arrays.

Procedural Programming 4
Array: Definition

Procedural Programming 5
Array: definition

• It is a collection of variables of the same type (homogenous)


that are referred through a common identifier or label.

• An array may have one or more elements (n).


• In the memory, a contiguous space is allocated for the array.
• The size of an array is permanent.

• Arrays can have one to several dimensions.


• Each dimension has its own size.

Procedural Programming 6
Memory layout

cells ... a b c d e f ... 9 8 7 6

addresses 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

1 byte

Procedural Programming 7
Declaring an array

• Like ordinary variable, an arrays must be explicitly declared


before it is used.

• There are 3 important parts in array declaration:


• Data type, it could be either primitive or composite;
• Identifier or variable name or label, has to be unique;
• The array size or the number of element (n).

unsigned short int array_size = 4;


short int arr[array_size]; // one dimension array

Procedural Programming 8
Declaring an array

• C has no array bound checking mechanism.


• Meaning it does not track array size.
• The programmer must have a tracking mechanism in place.
• The tracking mechanism must always follow the array
where ever it goes.

unsigned short int array_size = 4; // used for tracking


short int arr[array_size];

Procedural Programming 9
Accessing & Manipulating array data

• An element of an array is accessible through its index.


• The index is ranged from 0 to n-1.
• To access the element, use the square bracket [].

unsigned short int array_size = 4; // used for tracking


short int arr[array_size];

arr[0] = 0;
arr[1] = 8;
arr[2] = 5;
arr[3] = 2;

Procedural Programming 10
Memory layout of
a single-dimensional array

cells ... 0 8 5 2 ... ... ... ... ... ... ...

addresses 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

1 byte

Procedural Programming 11
Calculating the reserved space

• The size of the memory space reserved for an array can be


calculated with the help of the sizeof() function.
• The function measures the space needed for a particular
data type.

unsigned short int array_size = 4;


short int arr[array_size];
short int total_byte = sizeof(short int) * array_size;

Procedural Programming 12
#include <stdio.h>

Prepare the tracking mechanism.


Array in C
int main(int _argc, char **_argv) {
unsigned short int array_size = 4;
Declaring an array of short int.
short int arr[array_size];
short int total_byte = sizeof(short int) * array_size;

arr[0] = 0; Calculate the reserved space.


arr[1] = 8;
arr[2] = 5;
arr[3] = 2; Manipulating array’s elements.
for (int index = 0; index < array_size; ++index) {
printf("arr[%d]=%d\n", index, arr[index]);
}
printf("%hu byte(s) is required\n", total_byte); Accessing array’s element.

return 0;
}

arr[0]=0
arr[1]=8
arr[2]=5
arr[3]=2
8 byte(s) is required

Procedural Programming 13
Multidimensional Arrays

Procedural Programming 14
Multidimensional arrays

• An array can be defined in a multidimensional structure.


• The first dimension is acting as the container to the second.
• The second is acting as the container to the third.
• More dimension  more complicated  error prone.
• It seems no different in the memory layout.

Procedural Programming 15
Memory layout of
a multidimensional arrays

cells ... a b c d e f ... 9 8 7 6

addresses 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

1 byte

Procedural Programming 16
#include <stdio.h>
Prepare the tracking mechanism

Multidimensional
int main(int _argc, char **_argv) {
unsigned short int s_size1 = 2; for the 1st and the 2nd dimension.
unsigned short int s_size2 = 2;

arrays in C
Declaring an array of short int.
short int s_array[s_size1][s_size2];
short int total_byte = sizeof(unsigned int) * s_size1 * s_size2;

s_array[0][0] = 9; Calculate the reserved space.


s_array[0][1] = 8;
s_array[1][0] = 7;
s_array[1][1] = 6; Manipulating array’s elements.

printf("s_array[0][0]=%d\n", s_array[0][0]);
printf("s_array[0][1]=%d\n", s_array[0][1]);
printf("s_array[0][0]=%d\n", s_array[1][0]);
printf("s_array[0][1]=%d\n", s_array[1][1]);
Accessing array’s elements.
printf("%hu byte(s) is required\n", total_byte);

return 0;
}

s_array[0][0]=9
s_array[0][1]=8
s_array[0][0]=7
s_array[0][1]=6
8 byte(s) is required

Procedural Programming 17
Todo

• Can you display the memory address of an array?


• What about the address of the array’s elements?
• Use the & and * operators.

Procedural Programming 18
References

• Kerninghan & Ritchie. The C Programming Language.


• Herbert Schildt. C: The Complete Reference.

Procedural Programming 19
Thank
you

Procedural Programming 20

You might also like