You are on page 1of 9

NOTES ABOUT ARRAYS (WHAT YOU SAID YOU NEED… RIGHT?

What is an array – is a linear container / data structure that holds data of the same type.

Char arr

Most data structure make use of arrays to implement there algorithms

Some of the key concepts of an array will include:

Element. Every item stored in an array is called an element.

Each location of an element has a numerical index. Elements are stored in a contiguous memory

location. An index is always less than the total number of array items. Any variable that is

declared as an array can store multiple values.

ARRAY OPERATION

We can perform the following operations in an array. Which include

 Transversal

 Insertion

 Searching

 Sorting

#include <Stdio.h>

Int main ()

Int a [5] = {2, 3, 5, 7, 11} //declaration of an array

For (Int i=0, i<5, c++)}


Print (“%d/n”, a [i]); //transversal}

Return 0;

} //end main

Insertion of an element can be done at the beginning, at the end or at a given index of an array.

#include<stdio.h > preprocessor directive

Int main {

Int array [10], n, I, item;

PrintF (“enter the size of an array”);

Scan (“%d”, &n”);

PrintF (“/n Enter elements in an array”);

For (i=o, i<n, i++)

Scanf (“%d”, &item);

N++

For (i=n; i>1;i--)

Array [i-1] =array [i-2]

Array [0]=item;

Printf (“resultant array element”);

For (i=0; i<n; I++)

Printf (“%d”, array [I] ;

}
getch ();

return 0;

& stores whatever is captured on the

Print f ( out puts whatever is on the array )A\

A stack is the most elemental data structure in computer science.

It allows us store data

Data 07
Data 06
Data 05
Data 04
Data 03
Data 02
Data 01

Abstract data types

When writing a program it is always important to focus on the problem however real world

problems are complex in nature. It therefor important to break down this in to their most

fundamental part and describe these parts in a simple way in a process called abstraction.

Through abstraction, we develop a model of the problem. This gives us the abstract of the

problem and in many case we have the data and the operations.
Temperature is low: measure

How to increase temperature

Heater:-

Regulation – (Button to set desired temperature)

Suggest

Preexisting temperature (current)

An abstract data type- is a mathematical model / logical model, of a data structure that

specifies the type of data and the operations supported by these.

Desired temperature = current temperature + heat ()

An entity specifies the operation but not how it is done

Examples of data types include

 Arrays

 Lists

 Maps

 Vectors

Problem
model

It is a linear data structure where elements are stacked on top of each other, only the last element

added can be accessed from the top since a stack is a lasting first out

Stacks have a number of operations but the main operations are three which include

Push

Pop

Peek/ top

 Push – used to add an element to a stack

 POP – removes elements from a stack

 Peek –looks at the top of the element without removing it

 Is full- check if the stack is full

 Is empty –check if the stack is empty

 Size – check the size of the stack

Element 7
Element 6
Element 5
Element 4
Element 3
Element 2
Element 1
Element

Stack data structures are useful when the order of actions is important. They ensure that the

system does move to a new action before completing the ones before
For example

(i) If you wanted to reverse a string such as “Hello World!” we will push each character into a

stack and them pop them.

(ii) THE undo and redo operations of editors

(iii) When playing some games that involve a pack and a dead is reached and you can backtrack

(iv) Programming languages use a data stack to execute code i.e. when a function is called it is

added to a call stack and is removed once completed.

#include <iostream>

#include <stack>

Using namespace std;

Int main ()

Stack<int> stack ;//create empty stack

Stack1.Push (100);

Stack 1. Push (200);

Stack 1. Push (300);

Stack 1. Push (400);

Stack 1. Push (500);

Stack 1. pop ();


Stack 1. Pop ()

Stock 1. Pop ()

While (! Stack 1. Empty ())

Cout << “The top most element is “<< stack 1. Top ()

Stack 1.pop()

Example 2

#include <iostream>

#include <stack>

Using name space std;

Void create stack (stack<int> my stack)

Stack <Int>ms=my stack;

While (! Ms. Empty)

Cout <<’/t’<<Ms. Top();

Cout <<’/n’;

Int main ()
Stack<Int>st;

St. Push (30) ;

St. Push (30);

St. Push (50);

St. push (70);

St. Push (90);

St. Push (20);

Cout <<”The stock st is”;

Create stack (st);

Cout <<”\n operation for size is St. Size ();

Cout <<”\n to remove the top element we use St. Pop ()”

St. Pop ()

Create stack (st);

Return 0 ;}

You might also like