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

Lec 2 Array Stack

Uploaded by

aamirshahad3321
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lec 2 Array Stack

Uploaded by

aamirshahad3321
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Data Structures

Arrays
Declaring Arrays

Syntax:
• type arrayName [ arraySize ];
This is called a single-dimension array. The arraySize must be an integer constant greater than zero
and type can be any valid C++ data type.

Example:
To declare a 10-element array called salary of type double, use this statement −
•double salary[10];
Initializing Arrays

•You can initialize C++ array elements either one by one or using a single statement as follows −
•double salary[5] = {1000.0, 20000.0, 34000, 170000.0, 5000000.0};
•The number of values between braces { } can not be larger than the number of elements that we declare for the
array between square brackets [ ]. Following is an example to assign a single element of the array −
•If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you
write −
•double salary[] = {1000.0, 20000.0, 34000, 170000.0, 5000000.0};
•You will create exactly the same array as you did in the previous example.
•salary[4] = 50000.0;
• The above statement assigns element number 5 th in the array a value of 50000.0. Array with 4th index will be
5th, i.e., last element because all arrays have 0 as the index of their first element which is also called base
index.
Accessing Array Elements

An element is accessed by indexing the array name. This is done by


placing the index of the element within square brackets after the name
of the array.
For example :
double sal = salary[9];
• Here variable sal is provided the value of 10th element of array named
salary
Input Array elements
• Input Array elements one at a time
• cin>>salary[3];
• To input whole elements in array use for loop
for(int i=0;i<10;i++)
cin>>salary[i];
Output Array elements
• To input whole elements in array use for loop
for(int i=0;i<10;i++)
cout<<salary[i];
Program to find sum and average of
array
#include <iostream>
using namespace std;
int main()
{
double numbers[] = {7, 5, 6, 12, 35, 27};
double sum = 0;
double count = 0;
double average;
for(int i=0;i<6;i++){
sum += n; //Calculating sum
++count; // Counting
}
cout << "\nTheir Sum = " << sum << endl;
average = sum / count;
cout << "Their Average = " << average << endl;
return 0;
}
Adding Two Arrays
#include <iostream>
using namespace std;
int main()
{
double a[10];
double b[10];
double c[10];
cout<<“Enter elements of first Array”<<endl;
for(int i=0;i<10;i++){
cin>>a[i];
}
Adding Two Arrays(cont.)
cout<<“Enter elements of second Array”<<endl;
for(i=0;i<10;i++){
cin>>b[i];
}
for(i=0;i<10;i++){
c[i]=a[i]+b[i];
cout<<c[i];
}
return 0;
}
Data Structures
Stack
Stack
• The stack is a linear data structure that uses a principle known as
LIFO (Last In First Out) or FILO (First In Last Out). Real-life examples of
a stack are a deck of cards, piles of books, piles of money, and many
more.
Stack(cont.)
• As shown in the above figure, we see that push and pop
are carried out from the same end. This makes the stack to
follow LIFO order. The position or end from which the items
are pushed in or popped out to/from the stack is called the
“Top of the stack”.
• Initially, when there are no items in the stack, the top of
the stack is set to -1. When we add an item to the stack,
the top of the stack is incremented by 1 indicating that the
item is added. As opposed to this, the top of the stack is
decremented by 1 when an item is popped out of the stack.
Basic Operations

• push – Adds or pushes an element into the stack.


• pop – Removes or pops an element out of the stack.
• peek – Gets the top element of the stack but doesn’t
remove it.
• isFull – Tests if the stack is full.
• isEmpty – Tests if the stack is empty.
Stack illustration
Stack illustration(cont.)
• The above illustration shows the sequence of operations that are
performed on the stack. Initially, the stack is empty. For an
empty stack, the top of the stack is set to -1.
• Next, we push the element 10 into the stack. We see that the top
of the stack now points to element 10.
• Next, we perform another push operation with element 20, as a
result of which the top of the stack now points to 20.
• Now in the last figure, we perform a pop () operation. As a result
of the pop operation, the element pointed at the top of the stack
is removed from the stack. Hence in the figure, we see that
element 20 is removed from the stack. Thus the top of the stack
now points to 10.
Implementation

#include<iostream>
using namespace std;

class stack{
private:
int * arr;
int top,i;
public:
stack(){
arr=new int[10];
top=-1;
}
Implementation(cont.)

void push(int x){


if(top>9){
cout<<"stack over flow"<<endl;
}
top++;
arr[top]=x;
}
Implementation(cont.)

void pop(){
if(top==-1){
cout<<"stack is empty"<<endl;
}
top--;
}
Implementation(cont.)

int topval(){
if(top==-1){
cout<<"empty"<<endl;
return -1;
}
return arr[top];
}
Implementation(cont.)

void show(){
if(top==-1){
cout<<"stack is empty"<<endl;
}
for(i=top;i>=0;i--){
cout<<"value of stack "<<i<<" is "<<arr[i]<<endl;
}
}

};
Implementation(cont.)

int main(){
stack st;
st.push(1);
st.push(2);
st.push(4);
cout<<"TOP= "<<st.topval()<<endl;
// st.pop();
// cout<<"TOP= "<<st.topval()<<endl;
st.show();

You might also like