You are on page 1of 7

Data Structures

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[].

SINGLE DIMENSIONAL ARRAY

Single or One-Dimensional Array – array with a single index.

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

It stores a maximum of 7 integers.

Individual value of grade[7]


grade[0]=95;
grade[1]=88;
grade[2]=93;
grade[3]=91;
grade[4]=90;
grade[5]=87;
grade[6]=93;

Syntax for accepting values

/*This code will accept 5 integers from the /*This code will print the numbers from 0
user.*/ to 10*/

int grade[5],x; for (x=0;x<=10;x++)


{
printf(“Enter 5 numbers:\n”) printf(“\n %d”,num[x])
}
for(x=0;x<5;x++)
{
scanf(“%d”,&grade[x]);
}

Syntax for Displaying Values


/*This code will print the value of num[5]*/

printf(“%d”, num[5]);

1
Data Structures

Example 1: (Single Dimensional Array)

/*This array program gets the sum of 10 numbers inside the array*/

#include<stdio.h>
main(){

int x, sum=0, num[5];

printf("Enter 5 numbers:");

for(x=0;x<5;x++)
{;
scanf("%d",&num[x]);
sum = sum + num[x];
}
printf("%d",sum);
}

Example 2: (Single Dimensional Array)

/*This array program finds the smallest and largest number*/

#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

Two-Dimensional Array – simplest form of the multi-dimensional array. It is an


array with two indexes.

Syntax:

Data type array name[row][col];

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 Programs: (Two Dimensional Array)

Example 1
/*This array program accepts 9 numbers from the user*/
#include<stdio.h>
main()
{
int x, y, num[3][3];
clrscr();

/*Syntax for accepting values*/


printf(“Input 9 numbers\n”);
for(x=0;x<3;x++)
{
for (y=0;y<3;y++)
scanf(“%d”,&num[x][y]);
}

/*Syntax for displaying values*/


for(x=0;x<3;x++)
{
for (y=0;y<3;y++)
printf(“%d”,&num[x][y]);
printf(“\n”);
}

Constant Two-Dimensional Array


- An array with predefined values.

Example Programs (Constant Two-Dimensional Array)

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.

Basic Stack Operations:


1. Push – adds an item at the top of the stack. After the push, the new item becomes the top. If there
is not enough room, then the stack is in an overflow state and the item cannot be added.
2. Pop – remove the item at the top of the stack and return it to the user. Because the top item has
been removed, the next older item in the stack becomes the top.
When the last item in the stack is deleted, it must be set to its empty state. If pop is called when
the stack is empty, then it is in an underflow state.
3. Stack Top – stack top copies the item at the top of the stack, that is, it returns the date in the top
element to the user but does not delete it.

Formats for arithmetic expression:


1. Infix notation – the operator comes between the two operands.
Infix: A + B
2. Prefix notation - the operator comes before the two operands.
Prefix: +AB
3. Postfix notation – the operator comes after the two operands.
Postfix: AB+

Arithmetic precedence:
/,*
+, -

Manual Transformation (Infix to Postfix)

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*)+)

3. Remove all parentheses.

A BC*+

Stack Transformation (Infix to Postfix)


1. Copy the operand to the postfix expression.
2. Push the operator to the stack.
3. If the next operator is higher than the operator in the stack, push.
4. If the next operator is lower than the operator in the stack, pop and then push.

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.

4 Basic Queue Operations


1. Enqueue – queue insert. After the data have been inserted into the queue, the new
element becomes the rear.
2. Dequeue – queue delete operation. The data at the front of the queue are returned to
the user and removed from the queue. If there are no data in the queue when the
dequeue is attempted, the queue is in an underflow state.
3. Queue front – examine the data at the front. It returns the data to the user without
changing the contents of the queue.
4. Queue rear – examine the data at the rear of the queue. It returns the data to the user
without changing the contents of the queue.

You might also like