# Programming Examples

## Stack ADT in Data Structures

```#include<iostream>
#include<stack>
using namespace std;
main(){
stack<int> stk;
if(stk.empty()){
cout << "Stack is empty" << endl;
}else{
cout << "Stack is not empty" << endl;
}
//insert elements into stack
stk.push(10);
stk.push(20);
stk.push(30);
stk.push(40);
stk.push(50);
cout << "Size of the stack: " << stk.size() << endl;
//pop and dispay elements
while(!stk.empty()){
int item = stk.top(); // same as peek operation
stk.pop();
cout << item << " ";
}
}```

## Negative Binomial distribution in Data Structures

```#include <iostream>
#include <random>
using namespace std;
int main(){
const int nrolls = 10000; // number of rolls
const int nstars = 100; // maximum number of stars to distribute
default_random_engine generator;
negative_binomial_distribution<int> distribution(3,0.5);
int p[10]={};
for (int i=0; i<nrolls; ++i) {
int number = distribution(generator);
if (number<10)
p[number]++;
}
cout << "negative_binomial_distribution (3,0.5):" << endl;
for (int i=0; i<10; ++i)
cout << i << ": " << string(p[i]*nstars/nrolls,'*') << endl;
}```

## Geometric Distribution in Data Structures

```#include <iostream>
#include <random>
using namespace std;
int main(){
const int nrolls = 10000; // number of rolls
const int nstars = 100; // maximum number of stars to distribute
default_random_engine generator;
geometric_distribution<int> distribution(0.3);
int p[10]={};
for (int i=0; i<nrolls; ++i) {
int number = distribution(generator);
if (number<10)
p[number]++;
}
cout << "Geometric_distribution (0.3):" << endl;
for (int i=0; i<10; ++i)
cout << i << ": " << string(p[i]*nstars/nrolls,'*') << endl;
}```

## Binomial Distribution in Data Structures

```#include <iostream>
#include <random>
using namespace std;
int main(){
const int nrolls = 10000; // number of rolls
const int nstars = 100; // maximum number of stars to distribute
default_random_engine generator;
binomial_distribution<int> distribution(9,0.5);
int p[10]={};
for (int i=0; i<nrolls; ++i) {
int number = distribution(generator);
p[number]++;
}
cout << "binomial_distribution (9,0.5):" << endl;
for (int i=0; i<10; ++i)
cout << i << ": " << string(p[i]*nstars/nrolls,'*') << endl;
}```

## Bernoulli Distribution in Data Structures

```#include <iostream>
#include <random>
using namespace std;
int main(){
const int nrolls=10000;
default_random_engine generator;
bernoulli_distribution distribution(0.7);
int count=0; // count number of trues
for (int i=0; i<nrolls; ++i)
if (distribution(generator))
count++;
cout << "bernoulli_distribution (0.7) x 10000:" << endl;
cout << "true: " << count << endl;
cout << "false: " << nrolls-count << endl;
}```

## Matrix multiplication algorithm in Data Structures

```#include<iostream>
using namespace std;
int main() {
int product[10][10], r1=3, c1=3, r2=3, c2=3, i, j, k;
int a[3][3] = {{2, 4, 1},
{2, 3, 9},
{3, 1, 8}
};
int b[3][3] = {{1, 2, 3},
{3, 6, 1},
{2, 4, 7}
};
if (c1 != r2) {
cout<<"Column of first matrix should be equal to row of second matrix";
} else {
cout<<"The first matrix is:"<<endl;
for(i=0; i<r1; ++i) {
for(j=0; j<c1; ++j)
cout<<a[i][j]<<" ";
cout<<endl;
}
cout<<endl;
cout<<"The second matrix is:"<<endl;
for(i=0; i<r2; ++i) {
for(j=0; j<c2; ++j)
cout<<b[i][j]<<" ";
cout<<endl;
}
cout<<endl;
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j) {
product[i][j] = 0;
}
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k) {
product[i][j]+=a[i][k]*b[k][j];
}
cout<<"Product of the two matrices is:"<<endl;
for(i=0; i<r1; ++i) {
for(j=0; j<c2; ++j)
cout<<product[i][j]<<" ";
cout<<endl;
}
}
return 0;
}```

## Operations on Queue in Data Structures

```#include<iostream>
#include<queue>
using namespace std;
main(){
queue<int> que;
if(que.empty()){
cout << "Queue is empty" << endl;
}else{
cout << "Queue is not empty" << endl;
}
//insert elements into queue
que.push(10);
que.push(20);
que.push(30);
que.push(40);
que.push(50);
cout << "Size of the queue: " << que.size() << endl;
//delete and dispay elements
while(!que.empty()){
int item = que.front(); // read element from first position
que.pop();
cout << item << " ";
}
}```

## Stack Primitive Operations in Data Structures

```#include<iostream>
#include<stack>
using namespace std;
main(){
stack<int> stk;
if(stk.empty()){
cout << "Stack is empty" << endl;
}else{
cout << "Stack is not empty" << endl;
}
//insert elements into stack
stk.push(10);
stk.push(20);
stk.push(30);
stk.push(40);
stk.push(50);
cout << "Size of the stack: " << stk.size() << endl;
//pop and dispay elements
while(!stk.empty()){
int item = stk.top(); // same as peek operation
stk.pop();
cout << item << " ";
}
}```

## Principles of Recursion in Data Structures

```#include<iostream>
#include<stack>
using namespace std;
main(){
stack<int> stk;
if(stk.empty()){
cout << "Stack is empty" << endl;
}else{
cout << "Stack is not empty" << endl;
}
//insert elements into stack
stk.push(10);
stk.push(20);
stk.push(30);
stk.push(40);
stk.push(50);
cout << "Size of the stack: " << stk.size() << endl;
//pop and dispay elements
while(!stk.empty()){
int item = stk.top(); // same as peek operation
stk.pop();
cout << item << " ";
}
}```

## Passing Selective Functions in Python

```from time import *

a = 1445945763

print(eval('gmtime(a)', {'__builtins__': None}, {'a': a, 'gmtime': gmtime}))```