You are on page 1of 16

Program-1

-To Find Mean and Median of Numbers stored in an array.

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n;
cin >> n;
int* arr = new int[n];
int sum = 0;
for(int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
sort(arr, arr + n);
double median;
double mean = sum / (double) n;
if(n % 2 == 0) {
int n1 = arr[n / 2 - 1];
int n2 = arr[n / 2];
median = (n1 + n2) / 2.0;
}
else {
median = arr[n / 2];
}
cout << "Mean is - " << mean << endl << "Median is - " << median;
return 0;
}

Output-

Mohammed Hanzla Hussain,2019UCO1707,COE-2


Program-2

-To insert one element and delete an element from an array.

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n;
cin >> n;
int arr[100];
for(int i = 0; i < n; i++) {
cin >> arr[i];
}

int choice;
cin >> choice;
int k, i;

switch (choice) {
case 1:
cout << "Enter number to be added and its index";
cin >> k;
cin >> i;
for(int j = 99; j > i; j--) {
arr[j] = arr[j - 1];
}
arr[i] = k;
n++;
break;

case 2:
cout << "Enter number to be deleted";
cin >> k;
int j;
for(j = 0; j < n; j++) {
if(arr[j] == k) {
break;
}
}
for(int l = j; l < 100; l++) { Outputs-
arr[l] = arr[l + 1];
}
n--;
break;

default:
cout << "Invalid choice";
break;
}

for(int l = 0; l < n; l++) {


cout << arr[l] << " ";
}

return 0;

Mohammed Hanzla Hussain,2019UCO1707,COE-2


Program-3

-To search for a number in an array.

#include <iostream>
#include <algorithm>
using namespace std;

bool isPresent(int* arr, int si, int ei, int k) {


if(si > ei) {
return false;
}

int i = (si + ei) / 2;


if(arr[i] == k) {
return true;
}
else if(arr[i] < k) {
return isPresent(arr, i + 1, ei, k);
}
else {
return isPresent(arr, si, i - 1, k);
}
}

int main() {
int n;
cin >> n;
int* arr = new int[n];
for(int i = 0; i < n; i++) {
cin >> arr[i];
}

sort(arr, arr + n);

int k;
cin >> k;

bool ans = isPresent(arr, 0, n - 1, k);


if(ans) { Output-
cout << true;
}
else {
cout << false;
}

delete [] arr;

return 0;
}

Mohammed Hanzla Hussain,2019UCO1707,COE-2


Program-4

-To sort an array.

#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;
int* arr = new int[n];
for(int i = 0; i < n; i++) {
cin >> arr[i];
}

for(int i = 0; i < n; i++) {


for(int j = 0; j < n - i - 1; j++) {
if(arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

for(int i = 0; i < n; i++) {


cout << arr[i] << " ";
}

return 0;
}

Output-

Mohammed Hanzla Hussain,2019UCO1707,COE-2


Program-5
-To merge two sorted arrays.
#include <iostream>
#include <queue>
using namespace std;

int main() {
int n1, n2;
priority_queue<int, vector<int>, greater<int> > pq;
cin >> n1;
int* arr1 = new int[n1];
for(int i = 0; i < n1; i++) {
cin >> arr1[i];
pq.push(arr1[i]);
}
cin >> n2;
int* arr2 = new int[n2];
for(int i = 0; i < n2; i++) {
cin >> arr2[i];
pq.push(arr2[i]);
}

int* ans = new int[n1 + n2];


int i = 0;
while(!pq.empty()) {
ans[i] = pq.top();
pq.pop();
i++;
}

for(int i = 0; i < n1 + n2; i++) {


cout << ans[i] << " ";
}

return 0;
}

Output-

Mohammed Hanzla Hussain,2019UCO1707,COE-2


Program-6

-To store the marks obtained by 10 students in 5 courses in a two-


dimensional array.

#include <iostream>
using namespace std;

int main() {
int marks[10][5];
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 10; j++) {
cout << "Marks of course " << i + 1 << " of student " << j + 1 << " ";
cin >> marks[i][j];
}
}

return 0;
}

Outputs-

Mohammed Hanzla Hussain,2019UCO1707,COE-2


Program-7

-To implement a linked list.


#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* head = NULL;
void insert(int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = head;
head = new_node;
}
void display() {
struct Node* ptr;
ptr = head;
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The linked list is: ";
display();
return 0;
}

Mohammed Hanzla Hussain,2019UCO1707,COE-2


Program-8

-To insert and delete node from linked list.

#include <iostream>

using namespace std;

struct Node{
int num;
Node *next;
};

struct Node *head=NULL;

void insertNode(int n){


struct Node *newNode=new Node;
newNode->num=n;
newNode->next=head;
head=newNode;
}

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;
}

Mohammed Hanzla Hussain,2019UCO1707,COE-2


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;
}

Mohammed Hanzla Hussain,2019UCO1707,COE-2


Program-9

-To print elements of linked list in reverse order without disturbing it.

#include <iostream>
using namespace std;

class Node
{
public:
int data;
Node* next;
};
void printReverse(Node* head)
{
if (head == NULL)
return;
printReverse(head->next);
cout << head->data << " ";
}
void push(Node** head_ref, char new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
Node* head = NULL;
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
printReverse(head);
return 0;
}

Mohammed Hanzla Hussain,2019UCO1707,COE-2


Program-10

-To reverse a linked list.

#include <iostream>
using namespace std;

struct Node{
int data;
Node *link;
};

Node *head=NULL;

void reverseList()
{
Node *p,*c,*n;
p=NULL;
c=head;
while(c!=NULL)
{

n=c->link;
c->link=p;
p=c;
c=n;
}
head=p;
}
void insertEnd (int d)
{

Node *ptr = new Node();


ptr->data=d;
ptr->link=NULL;

if(head==NULL)

Mohammed Hanzla Hussain,2019UCO1707,COE-2


head=ptr;
else
{
Node *temp = head;
while(temp->link != NULL)
{
temp=temp->link;
}
temp->link=ptr;

void displayList()
{
Node *ptr=head;
while(ptr!=NULL)
{
cout<<ptr->data<<" ";
ptr=ptr->link;
}
cout<<"\n";
}
int main()
{
insertEnd(1);
insertEnd(2);
insertEnd(3);
insertEnd(4);
insertEnd(5);

displayList();
reverseList();
displayList();
return 0;
}

Mohammed Hanzla Hussain,2019UCO1707,COE-2


Program-11

-To add two polynomials using linked list.


#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int coe,exp;
struct node *next;
};
class polynomial
{
struct node *start,*ptrn,*ptrp;
public:
void get_poly();
void show();
void add(polynomial p1,polynomial p2);
};
void polynomial::get_poly()
{
char c='y';
ptrn=ptrp=start=NULL;
while(c=='y' || c=='Y')
{
ptrn=new node;
ptrp->next=ptrn;
if(start==NULL)
start=ptrn;
ptrp=ptrn;
cout<<"
Enter the coefficient: ";
cin>>ptrn->coe;
cout<<"Enter the exponent: ";
cin>>ptrn->exp;
ptrn->next=NULL;
cout<<"Enter y to add more nodes: ";

Mohammed Hanzla Hussain,2019UCO1707,COE-2


cin>>c;
}
return;
}
void polynomial::show()
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
cout<<ptr->coe<<"X^"<<ptr->exp<<" + ";
ptr=ptr->next;
}
cout<<" ";
}
void polynomial::add(polynomial p1,polynomial p2)
{
struct node *p1ptr,*p2ptr;
int coe,exp;
ptrn=ptrp=start=NULL;
p1ptr=p1.start;
p2ptr=p2.start;
while(p1ptr!=NULL && p2ptr!=NULL)
{
if(p1ptr->exp==p2ptr->exp) // If coefficients are equal
{
coe=p1ptr->coe+p2ptr->coe;
exp=p1ptr->exp;
p1ptr=p1ptr->next;
p2ptr=p2ptr->next;
}
else if(p1ptr->exp>p2ptr->exp)
{
coe=p1ptr->coe;
exp=p1ptr->exp;
p1ptr=p1ptr->next;
}

Mohammed Hanzla Hussain,2019UCO1707,COE-2


else if(p1ptr->exp<p2ptr->exp)
{
coe=p2ptr->coe;
exp=p2ptr->exp;
p2ptr=p2ptr->next;
}
ptrn=new node;
if(start==NULL)
start=ptrn;
ptrn->coe=coe;
ptrn->exp=exp;
ptrn->next=NULL;
ptrp->next=ptrn;
ptrp=ptrn;
}
if(p1ptr==NULL)
{
while(p2ptr!=NULL)
{
coe=p2ptr->coe;
exp=p2ptr->exp;
p2ptr=p2ptr->next;
ptrn=new node;
if(start==NULL)
start=ptrn;
ptrn->coe=coe;
ptrn->exp=exp;
ptrn->next=NULL;
ptrp->next=ptrn;
ptrp=ptrn;
}
}
else if(p2ptr==NULL)
{
while(p1ptr!=NULL)
{
coe=p1ptr->coe;

Mohammed Hanzla Hussain,2019UCO1707,COE-2


exp=p1ptr->exp;
p1ptr=p1ptr->next;
ptrn=new node;
if(start==NULL)
start=ptrn;
ptrn->coe=coe;
ptrn->exp=exp;
ptrn->next=NULL;
ptrp->next=ptrn;
ptrp=ptrn;
}
}
}
int main()
{
clrscr();
polynomial p1,p2,sum,diff;
cout<<"First Polynomial.";
p1.get_poly();
cout<<"
Second polynomial.
";
p2.get_poly();
clrscr();
cout<<"
The First polynomial is: ";
p1.show();
cout<<"
The second polynomial is: ";
p2.show();
cout<<"
The sum of two polynomials is: ";
sum.add(p1,p2);
sum.show();
getch();
return 0;
}

Mohammed Hanzla Hussain,2019UCO1707,COE-2

You might also like