Professional Documents
Culture Documents
Assignment on
Submitted To
Examiner
Department of CSE
National University
Gazipur, Dhaka
Submitted By
Name:
Roll:
Registration:
Semester:
Batch:
#include<stdio.h>
#include<string.h>
#include<conio.h>
char firstString[100],secondString[100];
int main()
{
int i,j,selectedInput,lenght,delPosition,repPosition;
char ch1,ch2;
printf("Enter 1 for insertion\n");
printf("Enter 2 for deletion\n");
printf("Enter 3 for getting length\n");
printf("Enter 4 for Replacement\n");
printf("Enter 5 for concatenation\n");
scanf("%d",&selectedInput);
scanf("%c",&ch1);
if(selectedInput==1)
{
printf("Enter a String\n");
gets(firstString);
printf("Enter a Character to insert\n");
scanf("%c",&ch2);
lenght=strlen(firstString);
firstString[lenght]=ch2;
firstString[++lenght]='\0';
printf("String after insertion %c\ final string:
%s\n",ch2,firstString);
}
else if(selectedInput==2)
{
printf("Enter a String\n");
gets(firstString);
printf("Inter a position for deletion=");
scanf("%d",&delPosition);
lenght=strlen(firstString);
if(delPosition<0 || delPosition>lenght)
{
printf("Invalid position");
}
else if(delPosition==lenght)
{
firstString[delPosition-1]='\0';
}
else
{
int position=delPosition-1;
else if(selectedInput==4)
{
printf("Enter a String\n");
gets(firstString);
printf("Enter a position for replacement=");
scanf("%d",&repPosition);
scanf("%c",&ch1);
printf("Enter replace character\n");
scanf("%c",&ch2);
lenght=strlen(firstString);
if(repPosition<0 || repPosition>lenght)
{
printf("Invalid position");
}
else
{
char prvChar;
prvChar=firstString[repPosition-1];
firstString[repPosition-1]=ch2;
printf("After replacing %c to %c final
string:%s\n",prvChar,ch2,firstString);
}
else if(selectedInput==5)
{
int pos=0;
gets(firstString);
printf("Enter second String\n");
gets(secondString);
char concatenatedString[100];
for( i=0; i<strlen(firstString); i++)
{
concatenatedString[pos++]=firstString[i];
}
printf("concatenatedString=
%s",concatenatedString);
return 0;
}
B. Write a program to sort an array of integers using
merge sort algorithm
main()
{
int i, n, a[10];
printf("How many elements in the array? ");
scanf("%d", &n);
printf("Enter array: ");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
divide(a, 0, n-1);
#include<stdio.h>
#include<conio.h>
int main() {
int i, j, num, temp, arr[20];
return 0;
}
D. writes a program to search an element in an array
using binary search algorithm
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], search, first, last, middle;
printf("Enter total number of elements :");
scanf("%d",&n);
printf("Enter %d number :", n);
for (i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("Enter a number to find :");
scanf("%d", &search);
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < search)
{
first = middle + 1;
}
else if(arr[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.",search);
}
getch();
}
F. write a program to multiply two matrices
#include<stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10], i, j, k;
int sum = 0;
//Multiplication Logic
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 2; j++) {
sum = 0;
for (k = 0; k <= 2; k++) {
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
return (0);
}
Part-2
#include<stdio.h>
#include<conio.h>
void main()
{
int choice;
int cont = 1;
clrscr();
while(cont == 1)
{
//Display menu to the user
printf("\n1. Insert at front\n");
printf("\n2. Insert at end\n");
printf("\n3. Insert at any position\n");
printf("\n4. Display linked list\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert_front();
break;
case 2:
insert_end();
break;
case 3:
insert_any();
break;
case 4:
display();
break;
}
getch();
}
temp->data = data_value;
temp->link = header->link;
header->link = temp;
}
temp->data = data_value;
temp->link = ptr->link;
ptr->link = temp;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int choice;
int cont = 1;
clrscr();
while(cont == 1)
{
//Display menu to the user
printf("\n1. Insert at end\n");
printf("\n2. Delete from front\n");
printf("\n3. Delete from end\n");
printf("\n4. Delete from anywhere\n");
printf("\n5. Display linked list\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert_end();
break;
case 2:
delete_front();
break;
case 3:
delete_end();
break;
case 4:
delete_any();
break;
case 5:
display();
break;
}
getch();
}
temp->data = data_value;
temp->link = ptr->link;
ptr->link = temp;
}
if(header->link == NULL)
{
printf("\nEmpty Linked List. Deletion not
possible.\n");
}
else
{
printf("\nEnter the data of the node to be
deleted: ");
scanf("%d", &key);
ptr = header;
while((ptr->link != NULL) && (ptr->data != key))
{
ptr1 = ptr;
ptr = ptr->link;
}
if(ptr->data == key)
{
ptr1->link = ptr->link;
free(ptr);
printf("\nNode with data %d deleted.\n",
key);
}
else
{
printf("\nValue %d not found. Deletion not
possible.\n", key);
}
}
}
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int Data;
struct Node *next;
}*top;
void popStack()
{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}
void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
/*
*/
#include <stdio.h>
#include<conio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
void main()
int choice;
while (1)
printf("4.Quit \n");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
} /*End of switch*/
} /*End of while*/
} /*End of main()*/
insert()
int add_item;
if (rear == MAX - 1)
else
{
if (front == - 1)
front = 0;
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
} /*End of insert()*/
delete()
return ;
else
front = front + 1;
}
} /*End of delete() */
display()
int i;
if (front == - 1)
else
printf("Queue is : \n");
printf("\n");
} /*End of display() */
Part-3
# include <stdio.h>
# include <stdlib.h>
void main() {
int choice;
char ans = 'N';
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
switch (choice) {
case 1:
do {
new_node = get_node();
printf("\nEnter The Element ");
scanf("%d", &new_node->data);
case 2:
printf("\nEnter Element to be searched :");
scanf("%d", &key);
case 3:
if (root == NULL)
printf("Tree Is Not Created");
else {
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
}
} while (choice != 4);
}
/*
Get new Node
*/
node *get_node() {
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
/*
This function is for creating a binary search tree
*/
void insert(node *root, node *new_node) {
if (new_node->data < root->data) {
if (root->lchild == NULL)
root->lchild = new_node;
else
insert(root->lchild, new_node);
}
/*
This function displays the tree in postorder fashion
*/
void postorder(node *temp) {
if (temp != NULL) {
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d", temp->data);
}
}
C. Write a Program to traverse a binary tree in in-
order process.
#include<stdio.h>
#include<conio.h>
void main()
{
node*tree=NULL;
char
c[]={'A','B','C','D','E','F','\0','G','\0','\0','\0','\0'
,'\0','\0','\0','\0','\0','\0','\0','\0','\0'};
tree=insert(c,0);
inorder(tree);
}