Final Assignment It Radha
Final Assignment It Radha
Final Assignment It Radha
Output-
Q2.Write a program to find average of five student-
Source code-
#include<iostream.h>
#include<conio.h>
void main()
{
float a[4]; float sum,avg;
clrscr();
cout<<"\n enter first student no:";
cin>> a[0];
cout<<"\n enter second student no:";
cin>> a[1];
cout<<"\n enter third student no:";
cin>>a[2];
cout<<"\n enter forth student no:";
cin>>a[3];
cout<<"\n enter fifth student no:";
cin>>a[4];
sum = a[0]+a[1]+a[2]+a[3]+a[4];
avg = sum/5;
cout<<"average fifth student no is :"<<avg;
getch();
}
Output-
Q3.Write a program to search given number in array-
Source Code-
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[10],i,n,ele;
printf("Enter array size:");
scanf("%d", &n);
printf("Enter array elements:");
for (i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter element to search:");
scanf("%d", &ele);
for (i=0; i<n; i++)
{
if (arr[i] == ele)
{
printf("%d found at position %d", ele, i+1);
return 0;
}
}
printf("element not found");
}
Output-
Q4.Write a program to count the element to given array –
Source Code-
#include<iostream.h>
#include<conio.h>
void main()
{
int n,array[100];
clrscr();
cout<<"\n Enter array size:";
cin>>n;
cout<<"Enter array element:";
for(int i=1;i<=n;i++)
{
cin>>array[i];
}
cout<<"array elements is:";
for(i=1;i<=n;i++)
{
cout<<"\t"<<array[i];
}
cout<<"\nTotal array elements is:"<<i-1;
getch();
}
Output-
Q5. Write a program to print the all element in array-
Source Code-
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5], i;
printf("Enter Array Element:");
for(i=0; i<5; i++)
{
scanf("%d", &a[i]);
}
printf("\n Array Element:");
for (i=0; i<5; i++)
{
printf("%d", a[i]);
}
return 0;
//getch();
}
Output-
Q6. Write a program to print matrix-
Source Code-
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,m,n;
int matrix[10] [20];
printf("Enter number of rows:");
scanf("%d",&m);
printf("Enter number of columns:");
scanf("%d", &n);
for (i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("Enter data in [%d] [%d]:", i,j);
scanf("%d", &matrix[i][j]);
}
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
//getch();
}
Output-
Q7. Write a program to print diagonal matrix of the matrix.
Source Code-
#include<stdio.h>
#include<conio.h>
int main()
{
int array1 [10][10], i,j,m,n,sum = 0;
printf("Enter no of rows::");
scanf("%d", &m);
printf("\n Enter no of colums::");
scanf("%d", &n);
printf("\Enter value of the matrix ::\n");
for (i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("\n Enter a[%d][%d] value::",i,j);
scanf("%d", &array1 [i][j]);
}
}
printf("\n the diagonal element of a matrix are:\n\n");
if(m==n)
{
for (i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
if(i==j)
printf("\t%d", array1 [i][j]);
else
printf("\t");
}
printf("\n\n");
}
}
else
{
printf("\n matrix is not a sqare matrix:");
}
return 0;
//getch();
}
Output-
Q8.Write a program to find sum, multiplication and inverse
of matrix.
Source Code:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[3][3],b[3][3],c[3][3]={0},d[3][3]={0},k,m,n,p,q;
clrscr();
printf("Enter number of rows and columns in A:");
scanf("%d%d",&m,&n);
printf("Enter number of rows and columns in B:");
scanf("%d%d",&p,&q);
printf("Enter elements of matrix A:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter elements of matrix B:");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("Result of Matrix Addition\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
for(i=0;i<m;i++)
for(j=0;j<q;j++)
for(k=0;k<p;k++)
d[i][j]+=a[i][k]*b[k][j];
printf("\n Result of Matrix Multiplication:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",d[i][j]);
printf("\n");
getch();
}
}
Output:-
Q9.Write a program to insert an item into linear array.
Source Code:-
#include<stdio.h>
#include<conio.h>
void main()
{
int array[50],position,c,n,value;
printf("Enter number of elements in the array");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(c=0;c<n;c++)
scanf("%d",&array[c]);
printf("Please enter the location where you want to insert an new element\n");
scanf("%d",&position);
printf("Please enter the value");
scanf("%d",&value);
for(c=n-1;c>=position-1;c--)
array[c+1]=array[c];
array[position-1]=value;
printf("Resultant array is:\n");
for(c=0;c<=n;c++)
printf("%d\n",array[c]);
getch();
}
Output:-
Q10.Write a program to delete an item from the linear
array-
Source code-
#include<stdio.h>
#include<conio.h>
void main()
{
int i, size, pos;
int a[] = {2,4,6,8,12};
size = size of(a)/ size of(a[0];
printf("the array elements before deletion operation:\n");
for (i=0; i<size; i++)
printf("a[%d] = %d\n", i, a[i]);
printf("\n Enter the position from where you wish to delet the element:");
scanf("%d", &pos);
printf("\n the array element after deletion operation are:\n");
for(i=pos-1; i<size; i++)
a[i] = a[i+1];
size = size -1;
for(i=0; i<size; i++)
printf("a[%d] = %d\n", i, a[i]);
}
Output-
Q11. Write a program in bubble short-
Source Code-
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
//getch()
}
Output-
Q12.Write a program into search an item with the help of
binary search.
Source Code-
#include<stdio.h>
#include<conio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
return 0;
}
Output-
Q13.Write a program to print all the elements of linear
linked list.
Source Code:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}
*head;
void createlist(int n);
void display();
int main()
{
int n,index;
printf("enter number of nodes to create;");
scanf("%d",&n);
createlist(n);
printf("\n data in list:\n");
display();
return 0;
}
void createlist(int n)
{
struct node *newnode,*temp;
int data,i;
head=malloc(sizeof(struct node));
if(head==NULL)
{
printf("unable to allocate memory");
exit(0);
}
printf("enter data of node 1:");
scanf("%d",&data);
head->data=data;
head->next=NULL;
temp=head;
for(i=2;i<=n;i++)
{
newnode=malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("unable to allocate memory");
exit(0);
}
printf("enter data of node %d:",i);
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
temp->next=newnode;
temp=temp->next;
}
}
void display()
{
struct node*temp;
if(head==NULL)
{
printf("list is empty \n"); getch();
return;
}
temp=head;
while(temp!=NULL)
{
printf("%d,",temp->data);
temp=temp->next;
}
printf("\n");
}
Output:-
Q.14 write a program to search an item into linked list-
Source Code-
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
int main ()
{
int item; int index;
return 0;
}
Output:-
Q15. Write a program to find the location of an item into
sorted Linked list .
Source Code:-
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
return (current && current->data == key) ? position : -1; // key not found
}
int main() {
struct Node* head = NULL;
if (position != -1)
printf("Item %d found at location %d", key, position);
else
printf("Item %d not found in the sorted linked list", key);
return 0;
}
Output:-
Q16. Write a program to insert new node into linked
list as First Node .
Source Code:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{ int data;
struct Node *next;
};
void linkedlisttraversal(struct Node *ptr)
{ while(ptr!=NULL)
{ printf("yash %d\n",ptr->data);
ptr=ptr->next;
}
}
struct Node * insertatfirst(struct Node * head,int data)
{
struct Node * ptr=(struct Node *) malloc(sizeof(struct Node));
ptr->next=head;
ptr->data=data;
return ptr;
}
int main()
{ struct Node *head;
struct Node *second;
struct Node *third;
//Allocate memory for nodes in the heap
head=(struct Node *) malloc(sizeof(struct Node));
second=(struct Node *) malloc(sizeof(struct Node));
third=(struct Node *) malloc(sizeof(struct Node));
//link first and second
head->data=5;
head->next=second;
second->data=11;
second->next=third;
third->data=12;
third->next=NULL;
Output:-
Q17.Write a program to insert new node into linked list as
particular node-
Source Code-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{ int data;
};
{ while(ptr!=NULL)
{ printf("yash %d\n",ptr->data);
ptr=ptr->next;
ptr->next=head;
ptr->data=data;
return ptr;
int main()
head->data=5;
head->next=second;
second->data=11;
second->next=third;
third->data=12;
third->next=NULL;
linkedlisttraversal(head);
head=insertatfirst(head,55);
linkedlisttraversal(head);
return 0;
Output:-
Q18. Write a program to insert new node into linked
list as End Node .
Source Code:-
#include <stdio.h>
if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Adding nodes to the end of the list: \n");
while(current != NULL) {
//Prints each node by incrementing pointer
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
//Adding 1 to the list
addAtEnd(1);
display();
return 0;
}
Output:-
Q19.Write a program to insert an element into stack.
Source Code-
#include<stdio.h>
#include<conio.h>
int stack[100],choice,n,top,x,i;
voidpush();
voiddisplay();
intmain()
{
clrscr();
top=-1;
printf("\n enter the size of stack:");
scanf("%d",&n);
printf("\n STACK OPERATION USING ARRAY");
printf("\n\t 1.PUSH\n\t 2.DISPLAY\n\t 3.EXIT");
do
{
printf("\n enter the choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
display();
break;
}
case 3:
{ printf("\n\t exit point");
break;
}
default:\
{
printf("\n\t please enter a valid choice(1/2/3)");
}
}
}
while(choice!=3);
return 0;
}
voidpush()
{
if(top>=n-1)
{
printf("\n\t stack over");
}
else
{
printf("enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
voiddisplay()
{
if(top>=0)
{
printf("\n the elements in stack \n");
for(i=top;i>=0;i--)
printf("\n%d",stack[i]);
printf("\n press next choice");
}
else
{
printf("\n the stack is empty");
}
}
Output-
Q20.write a program to delete an item from stack.
Source Code-
#include<stdio.h>
#include<conio.h>
#define MAX 50
void push();
void pop();
void display();
int stack[MAX],top=-1,item;
void main()
{
int ch;
clrscr();
do
{
printf("\n1.\tpush\n2.\tpop\n3.\tdisplay\n4.\texit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
printf("invalid entry please try again...");
}
}
while(ch!=4);
getch();
}
void push()
{
if(top==MAX-1)
printf("\n stack is full.");
{
printf("enter item:");
scanf("%d",&item);
top++;
stack[top]=item;
printf("\n item inserted :%d",item);
}
}
void pop()
{
if(top==-1)
printf("stack is empty.");
else{
item=stack[top];
top--;
printf("\n item deleted:%d",item);
}
}
void display()
{int i;
if(top==-1)
printf("stack is empty");
else
{
printf("stack elements are:");
for(i=top;i>=0;i--)
printf("%d\t",stack[i]);
}
}
Output:-
Q21.write a program to insert an item into queue.
Source Code-
#include<iostream.h>
#include<conio.h>
#include<process.h>
void instque();
void display();
int queue[5];
int front=-1,rear=-1;
void main()
{
int choice;
clrscr();
do
{
cout<<"1.insert an element in queue \n";
cout<<"2.display queue \n";
cout<<"3.exit\n";
cout<<"enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
instque();
break;
case 2:
display();
break;
case 3:
exit(0);
}
}while(choice!=3);
getch();
}
void instque()
{
int info;
cout<<"enter the element to be inserted";
cin>>info;
if(front==-1)
{
front=0;
rear=0;
}
else
{rear=rear+1;
}
queue[rear]=info;
}
void display()
{
if(front==-1)
{
cout<<"the queue is empty";
}
else{
for(int i=0;i<=rear;i++)
{cout<<"queue is:"<<queue[i]<<"\t" ;
}
}
cout<<"\n";
}
Output:-
Q22.write a program to delete an item from queue.
Source Code-
#include<iostream.h>
#include<conio.h>
#include<process.h>
void delqueue();
void display();
int queue[5]={11,12,13,15,16};
int front=0,rear=4;
void main()
{ int choice;
clrscr();
do
{
cout<<"1.delete an element from queue \n";
cout<<"2.display queue\n";
cout<<"3.exit\n";
cout<<"enter your choice";
cin>>choice;
switch(choice)
{
case 1:
delqueue();
break;
case 2:
display();
break;
case 3:
exit(0);
}
}while(choice!=0);
getch();
}
void delqueue()
{
int info;
if(front!=-1)
{
info=queue[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else{
front=front+1;
}
cout<<"delete element is:"<<info<<"\n";
}
else
cout<<"queuer is empty";
}
void display()
{
if(front==-1)
cout<<"queue is empty";
else{
for(int i=front;i<=rear;i++)
{
cout<<queue[i]<<"\t";
}
}
cout<<"\n";
}
Output:-
Q23. Write a program to sort the given element using
Quick Sort.
Source Code:-
#include <stdio.h>
// main function
void main() { int j;
int data[] = {8, 7, 2, 1, 0, 9, 6};
printf("Unsorted Array\n");
printArray(data, n);
min=a[k];
loc=k;
for(int j=k+1;j<=n-1;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}
return loc;
}
void main()
{
int a[10],i,temp,n;
int loc;
clrscr();
cout<<"\nenter size of list:";
cin>>n;
cout<<"\nenter the element for list:\n";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
cout<<"before sorting element in list:";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
for(i=0;i<=n-1;i++)
{
loc=min(a,i,n);
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
cout<<"\nafter sorting element in list:";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
getch();
}
Output:-
Q26. Write a program to print the all nodes of Tree
using Preorder traversal.
Source Code:-
#include<stdio.h>
#include<stdlib.h>
// We are creating struct for the binary tree below
struct node
{
int data;
struct node *left, *right;
};
/* If not then we recur down the tree to find correct position for insertion */
if (data < node->data)
node->left = insert (node->left, data);
else if (data > node->data)
node->right = insert (node->right, data);
return node;
}
void main ()
{
/* What our binary search tree looks like really
9
/\
7 14
/\/\
5 8 11 16 */
getch();
}
Output:-
Q27. Write a program to print the all nodes of Tree
using Inorder traversal.
Source Code:-
#include <stdio.h>
#include <stdlib.h>
struct node {
int element;
struct node* left;
struct node* right;
};
/*To create a new node*/
struct node* createNode(int val)
{
struct node* Node = (struct node*)malloc(sizeof(struct node));
Node->element = val;
Node->left = NULL;
Node->right = NULL;
return (Node);
}
/*function to traverse the nodes of binary tree in Inorder*/
void traverseInorder(struct node* root)
{
if (root == NULL)
return;
traverseInorder(root->left);
printf(" %d ", root->element);
traverseInorder(root->right);
}
void main()
{
struct node* root = createNode(40);
root->left = createNode(30);
root->right = createNode(50);
root->left->left = createNode(25);
root->left->right = createNode(35);
root->left->left->left = createNode(15);
root->left->left->right = createNode(28);
root->right->left = createNode(45);
root->right->right = createNode(60);
root->right->right->left = createNode(55);
root->right->right->right = createNode(70);
printf("\n The Inorder traversal of given binary tree is -\n");
traverseInorder(root);
getch();
}
Output:-
Q28. Write a program to print the all nodes of Tree
using Postorder traversal.
Source Code:-
#include <stdio.h>
#include <stdlib.h>
struct node {
int element;
struct node* left;
struct node* right;
};
return (Node);
}
int main()
{
struct node* root = createNode(40);
root->left = createNode(30);
root->right = createNode(50);
root->left->left = createNode(25);
root->left->right = createNode(35);
root->left->left->left = createNode(15);
root->left->left->right = createNode(28);
root->right->left = createNode(45);
root->right->right = createNode(60);
root->right->right->left = createNode(55);
root->right->right->right = createNode(70);
struct node {
int key;
struct node *left, *right;
};
// Driver Code
void main()
{
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
struct node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80); clrscr();
getch();
}
Output:-
Q30. Write a program to delete an item into
BST(binary search tree)
Source Code:-
#include <stdio.h>
#include <stdlib.h>
struct Node {
int key;
struct Node *left, *right;
};
// Find successor
struct Node* succ = root->right;
while (succ->left != NULL) {
succParent = succ;
succ = succ->left;
}
// Driver Code
void main()
{
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
struct Node* root = NULL;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60); clrscr();
getch();
}
Output:-
Q31.Write a program to demonstrate breadth first search.
Source Code-
#include<stdio.h>
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v) {
for (i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r) {
visited[q[f]]=1;
bfs(q[f++]);
}
}
void main() {
int v;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++) {
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");
for (i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i); else
printf("\n Bfs is not possible");
getch();
}
Output:
Q32.Write a program demonstrate to depth first search.
Source Code-
#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
voiddfs(int v) {
int i;
reach[v]=1;
for (i=1;i<=n;i++)
if(a[v][i] && !reach[i]) {
printf("\n %d->%d",v,i);
dfs(i);
}
}
int main() {
inti,j,count=0;
printf("\n Enter number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++) {
reach[i]=0;
for (j=1;j<=n;j++)
a[i][j]=0;
}
printf("\n Enter the adjacency matrix:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
for (i=1;i<=n;i++) {
if(reach[i])
count++;
}
if(count==n)
printf("\n Graph is connected"); else
printf("\n Graph is not connected");
return 0;
}
Output: