Professional Documents
Culture Documents
Module 2 (Arrays, Records, Stacks, Queues)
Module 2 (Arrays, Records, Stacks, Queues)
Lesson 2: ARRAYS
Array – collection of similar data elements. It is a data structure stored in memory.
Array Variable – can be distinguished through a pair of square brackets and on specified
number inside the square brackets[].
Syntax:
data_type_array[size];
Examples:
int num[5];
char name[80];
float area[3];
Graphical Representation
int grade[7];
95 88 93 91 90 87 93
/*This code will accept 5 integers from the /*This code will print the numbers from 0
user.*/ to 10*/
printf(“%d”, num[5]);
1
Data Structures
/*This array program gets the sum of 10 numbers inside the array*/
#include<stdio.h>
main(){
printf("Enter 5 numbers:");
for(x=0;x<5;x++)
{;
scanf("%d",&num[x]);
sum = sum + num[x];
}
printf("%d",sum);
}
#include<stdio.h>
main(){
for(x=0;x<5;x++)
int x,num[5],min,max; {
if(min>num[x])
printf("Enter five numbers:"); min = num[x];
else
for(x=0;x<5;x++) if(max<num[x])
{ max=num[x];
scanf("%d",&num[x]); }
} printf("\nMINIMUM NUMBER: %d",min);
printf("\nMAXIMUM NUMBER: %d",max);
min=num[0];
max=num[0]; }
TWO-DIMENSIONAL ARRAY
Syntax:
Examples:
int score[2][3];
char list[5][9];
float ave[5][5];
The first number specifies the maximum row[r] while the second number specifies the maximum
column[c].
Graphical Representation:
Int score[2][2];
0 1 2
0 89 95 87
1 88 92 91
2 85 84 83
2
Data Structures
score[0][0]=89
score[0][1]=95
score[0][2]=87
score[1][0]=88
score[1][1]=92
score[1][2]=91
score[2][0]=85
score[2][1]=84
score[2][2]=83
Example 1
/*This array program accepts 9 numbers from the user*/
#include<stdio.h>
main()
{
int x, y, num[3][3];
clrscr();
Example 1
/*This array program computes the sum of the numbers.*/
#include<stdio.h>
main()
{
int sum;
int num[3][3]= { 2, 4, 6,
8,10,12,
14,16,18};
int x, y;
clrscr();
sum=0;
for(x=0;x<3;x++)
for(y=0;y<3;y++)
{
sum=sum+num[x][y];
}
printf(“The sum of the numbers inside the array: %d”, sum);
getch();
}
3
Data Structures
Lesson 3: RECORDS
RECORDS
A structure that can have a number of heterogeneous elements.
Field1, field2, field3 … fieldN can be of any data type (i.e. integer, float, real,
record)
4
Data Structures
Lesson 3: STACKS
A stack is an ordered list in which all insertions and deletions are made at one end called the TOP. The
reversing attribute has led to stacks being known as the Last-In-First-Out (LIFO) data structure.
Arithmetic precedence:
/,*
+, -
Expression: A + B * C
1. Parenthesize the expression using any explicit parentheses and the arithmetic precedence,
multiply and divide before add and subtract.
(A + (B*C))
2. Change all infix notations in each parentheses to postfix notation starting from the
innermost expression. This is done by moving the operator to the location of the
expression’s closing parenthesis.
(A + (BC*))
(A (BC*)+)
A BC*+
5
Data Structures
pop >
pop, push <
pop, push =
Illustration:
Expression: A + B * C - D
A
+
stack postfix string
*
B
-
B C*+
BC*+D-
6
Data Structures
Lesson 4: QUEUES
A queue is an ordered list in which all insertions take place at one end, called the REAR, while
all deletions take place at the other end, called the FRONT. These restrictions ensure that the
data are processed through the queue in the order in which they are received, or the First-In,
First-Out (FIFO) structure.