ARRAY
SINGLE DIMMENSION
MULTIDIMMENSION
STACK
QUEUE
Reasoning
There is a need to handle similar types of data for
example 100 pieces
One method is to create similar data 100 times
Array is very important part of programming
Variable can be identified by the index (subscript)
of the variable
Array formatting
Syntax on program:
data_type array_name[array_size];
Example:
int age[5];
There are five variables of integer with subscripts
from 0 – 4
Initializing an array:
int age[5]={2,4,34,3,4};
int age[]={2,4,34,3,4};
Compiler will determine the size for the 2nd part
Array example
#include <stdio.h>
void main()
{
int age[]={2,67,4,35,21};
int i;
int sum_age = 0;
for (i=0; i<5; i++)
printf ("Age[%d] = %d\n", i, age[i]);
for (i=0; i<5; i++)
sum_age+=age[i];
printf ("Average age is %d\n", sum_age/5);
}
Array example sorting
Sorting number from small to large
Simplest is called bubble sort
Sorting can be from small to large or vice versa
Check the largest from beginning to end and put at
the end
Start over again until second from end and so on
Example program:
Bubble.c– 5 numbers
Bubble1.c – 100 numbers
Array sorting animation
Simplest is called bubble sort
Array multi dimension
Many occasions we need multi dimension array such
as for a matrix
Visualization of a multidimensional array:
array consists of 12 data arranged into 6 column
and 2 rows
Initializing multidimensional array
int c[2][3]={{1,3,0}, {-1,5,9}};
OR
int c[][3]={{1,3,0}, {-1,5,9}};
OR
int c[2][3]={1,3,0,-1,5,9};
Initializing 3 dimensional array
double cprogram[3][2][4]=
{ {{-0.1, 0.22, 0.3, 4.3}, {2.3, 4.7, -0.9, 2}},
{{0.9, 3.6, 4.5, 4}, {1.2, 2.4, 0.22, -1}},
{{8.2, 3.12, 34.2, 0.1}, {2.1, 3.2, 4.3, -2.0}}
};
String and array
A string is a collection of character
Therefore a string is an array of character
For example we can create a variable name
consisting of characters 62 pcs
Format:
char name[62]; // this is static declaration
name = malloc(62*sizeof(char));// dynamic allocation
Example creating alphanumeric 0-9a-zA-Z
Each character can have index/ subscript
String and array example/ exercise (1/3)
Three programs (first program):
string-intro.c
Write a program to compare string and tell which
one is smaller, same or larger using strncmp()
Example:
anwar < anwari
angan < angin
sting < stink
wind =wind
String and array example/ exercise (2/3)
Three programs (2nd and 3rd programs):
string-arab-to-roman.c
string-roman-to-arab.c
Watch the program run
Create your own ARAB ↔ ROMAN
Rules and regulations follow
String and array example/ exercise (3/3)
Convert number from Arab to Roman
Convert number from Roman to Arab
Check all of the validity
/* rules of roman numbers:
1. A number written in Decimal numerals can be broken into digits. For example, 1903
is composed of 1, 9, 0, and 3.
To write the Roman numeral, each of the non-zero digits should be treated
separately. In the above example, 1,000 = M, 900 = CM, and 3 = III.
Therefore, 1903 = MCMIII.
2. The symbols I, X, C, and M can be repeated three times in succession, but no more.
(They may appear more than three times if they appear non-sequentially, such as
XXXIX.) D, L, and V can never be repeated. Therefore the maximum number is 3999
3. I can be subtracted from V and X only. X can be subtracted from L and C only. C can
be subtracted from D and M only. V, L, and D can never be subtracted.
4. 1 5 10 50 100 500 1000
I V X L C D M */
Exercise anagram
Anagram:
An anagram is a rearrangement of the letters of
one word or phrase to form another word or phrase
Example:
"Evil" ↔ "Vile“
"Clint Eastwood" ↔ "Old West action“
"Psychotherapist" ↔ "Psycho the rapist“
Write a program to check if two words are
anagrams
Disregard all spaces if there are any
Exercise matrix
Create a matrix of 3x3
Create another matrix of 3x3
Print both matrices
Create procedure to add both matrices
Print the sum matrix
Matrix multiplication M[r,c]=sum(row*column)
Create procedure to multiply both matrices (10x10?)
Example:
matrix-mul-array-0.c
matrix-mul-array-1.c
matrix-mul-array-2.c
Stack and queue
Stack in Indonesian is “TUMPUKAN”
Queue in Indonesian is “ANTRIAN”
Stack and queue is part of data structure which is
methods to organize data
Modern computes use stack and queue as part of
their architecture/ organization
Stack (1/3)
Element of stack is added to or removed from the
top of the pile
Other than the top part, any part of the stack
cannot be manipulated
To add to the top of the stack, there is a ‘push’
operation
To remove from the top of the stack, there is a ‘pop’
operation
Last part that goes In, will be the First to come Out
(LIFO)
Stack (2/3)
Left is stack operation
illustration
Push element to add to the
top of the stack
Pop element to remove
from the top of the stack
Operation only on top
element
Stack (3/3)
Stack is used when:
Program called a function
Operating system spawn a process (other class)
Operating system/ program service interrupt (other class)
To manage a stack you have to be able to:
Push and pop the stack
Display the stack
Tell if the stack is full
Tell if the stack is empty
Use a stack indicator (stack pointer) to manage the
above
Queue (1/3)
Element of queue is added from the back (tail)
Element of queue is removed from the front (head)
Adding element to the queue is INSERT
Removing element from the queue is DELETE
Manipulation can only be performed only at the
end and at the beginning of the queue
First part that goes In, will be the First to come Out
(FIFO)
Queue (2/3)
Left is queue operation illustration
INSERT (=enqueue) is from the back
DELETE (=dequeue) is from the front
Queue (3/3)
Queue is used when:
Program ask for resources from computer (memory, network,
printer)
Programs need to be executed
To manage a stack you have to be able to:
Insert and Delete queue
Display the queue
Tell if the queue is full
Tell if the queue is empty
Use a queue indicator (queue pointer) to manage
the above
Queue and stack exercise
Two programs for this part:
queue.c
stack.c
Watch how the programs works
Duplicate the program