Professional Documents
Culture Documents
C++ Programs ....
C++ Programs ....
Program to store temperature of two different cities for a week and display
it
#include <iostream>
int main()
int temperature[CITY][WEEK];
cout << "Enter all temperature for a week of first city and then second city. \n";
cout << "City " << i + 1 << ", Day " << j + 1 << " : ";
}
cout << "\n\nDisplaying Values:\n";
cout << "City " << i + 1 << ", Day " << j + 1 << " = " << temperature[i][j] << endl;
return 0;
}
2. Program to Store value entered by user in three dimensional array and display
it
#include<iostream>
using namespace std;
int main()
{.
int x[3][2] = {{0,1}, {2,3}, {4,5}};
return 0;
}
3. Program to swap first and last element of an integer 1-d array
#include<iostream>
using namespace std;
int main()
{
int Arr[100],n,i,temp;
for(i=0;i<n;i++)
{
Arr[n-1]=temp;
for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Output:
4. Code to PUSH Element in a stack using Array
#include <bits/stdc++.h>
public:
int a[MAX];
bool Stack::push(int x)
{
if (top >= (MAX - 1)) {
cout << "Stack Overflow";
return false;
}
else {
a[++top] = x;
cout << x << " pushed into stack\n";
return true;
}
}
int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = a[top--];
return x;
}
}
int Stack::peek()
{
if (top < 0) {
cout << "Stack is Empty";
return 0;
}
else {
int x = a[top];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
int main()
{
class Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << " Popped from stack\n";
return 0;
}
5. Code to traverse the stack using Array
#include <iostream>
#include <stack>
usingnamespacestd;
intmain()
{
stack<int>mystack;
mystack.push(5);
mystack.push(1);
mystack.push(2);
cout <<mystack.top();
return0;
}
Output :-
#include <iostream.h>
#include <conio.h>
struct node
{
int data;
node *next;
};
class linklist
{
node *head;
public:
linklist()
{head=NULL;}
void create(void);
void display(void);
};
void main()
{
clrscr();
linklist o1;
o1.create();
o1.display();
getch();
}
Output :
7. Code to insert the node at the particular position in the linked list
#include <iostream>
struct Node{
int num;
Node *next;
};
void display(){
if(head==NULL){
cout<<"List is empty!"<<endl;
return;
}
struct Node *temp=head;
while(temp!=NULL){
cout<<temp->num<<" ";
temp=temp->next;
}
cout<<endl;
}
void deleteItem(){
if(head==NULL){
cout<<"List is empty!"<<endl;
return;
}
cout<<head->num<<" is removed."<<endl;
head=head->next;
}
int main(){
display();
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
insertNode(50);
display();
deleteItem(); deleteItem(); deleteItem(); deleteItem(); deleteItem();
deleteItem();
display();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
head->data = head->next->data;
n = head->next;
head->next = head->next->next;
free(n);
return;
}
Node *prev = head;
while(prev->next != NULL && prev->next != n)
prev = prev->next;
if(prev->next == NULL)
{
cout << "\nGiven node is not present in Linked List";
return;
}
prev->next = prev->next->next;
free(n);
return;
}
void push(Node **head_ref, int new_data)
{
Node *new_node = new Node();
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}
void printList(Node *head)
{
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
cout<<endl;
}
int main()
{
Node *head = NULL;
push(&head,3);
push(&head,2);
push(&head,6);
push(&head,5);
push(&head,11);
push(&head,10);
push(&head,15);
push(&head,12);
#include<stdio.h>
int main()
{
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
10. Program to implement Tower of Hanoi problem
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
struct Stack
{
unsigned capacity;
int top;
int *array;
};
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack =
(struct Stack*) malloc(sizeof(struct Stack));
stack -> capacity = capacity;
stack -> top = -1;
stack -> array =
(int*) malloc(stack -> capacity * sizeof(int));
return stack;
}
int isFull(struct Stack* stack)
{
return (stack->top == stack->capacity - 1);
}
{
return (stack->top == -1);
}
void push(struct Stack *stack, int item)
{
if (isFull(stack))
return;
stack -> array[++stack -> top] = item;
}
else
{
push(dest, pole2TopDisk);
push(dest, pole1TopDisk);
moveDisk(s, d, pole1TopDisk);
}
}
void tohIterative(int num_of_disks, struct Stack
*src, struct Stack *aux,
struct Stack *dest)
{
int i, total_num_of_moves;
char s = 'S', d = 'D', a = 'A';
if (num_of_disks % 2 == 0)
{
char temp = d;
d = a;
a = temp;
}
total_num_of_moves = pow(2, num_of_disks) - 1;
for (i = num_of_disks; i >= 1; i--)
push(src, i);
else if (i % 3 == 2)
moveDisksBetweenTwoPoles(src, aux, s, a);
else if (i % 3 == 0)
moveDisksBetweenTwoPoles(aux, dest, a, d);
}
}
int main()
{
unsigned num_of_disks = 3;