mCSL216 NEW
mCSL216 NEW
OPEN UNIVERSITY
LABORATORY RECORD
Month &Year : ………………………………………………….….
Name : …………………………………………………………………
Study Center : 1402, SH College, Thevara, Kochi-13
Course : …………….……
Course Title : ………………………………………………………..…….….
…………………………………………………………..……..
Source Code
#include<stdio.h>
#include<stdlib.h>
int w[10], p[10], v[10][10], n, i, j, cap, x[10] = {0};
int max(int i, int j) {
return ((i > j) ? i : j);
}
int knap(int i, int j) {
int value;
if (v[i][j] < 0) {
if (j < w[i])
value = knap(i - 1, j);
else
value = max(knap(i - 1, j), p[i] + knap(i - 1, j - w[i]));
v[i][j] = value;
}
return v[i][j];
}
int main() {
int profit, count = 0;
printf("\nEnter the number of elements\n");
scanf("%d", &n);
printf("Enter the profit and weights of the elements\n");
for (i = 1; i <= n; i++) {
printf("For item no %d\n", i);
scanf("%d%d", &p[i], &w[i]);
}
Output
Ques 2
Implement multiplication of two matrices A[4, 4] and B[4, 4] and calculate
following: (i) How many times the innermost and the outermost loop will run?
(ii) Total number of multiplication and additions in computing the
multiplication of given matrices.
Source Code
#include<stdio.h>
int main()
{
int m, n, p, q, i, j, k;
int a[10][10], b[10][10], res[10][10];
if (n != p)
{
printf("Matrix is incompatible for multiplication\n");
}
else
{
printf("Enter the elements of Matrix-A:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", & a[i][j]);
}
}
return 0;
}
Output
Ques 3:
Implement Huffman's coding algorithm and run on the problem instance given
below:
Letters: A B I M S X Z
Frequency: 10 7 15 8 10 5 2
Source Code
#include <iostream>
#include <vector>
#include <queue>
#include <string>
class Huffman_Codes
{
struct New_Node
{
char data;
size_t freq;
New_Node* left;
New_Node* right;
New_Node(char data, size_t freq) : data(data),
freq(freq),
left(NULL),
right(NULL)
{}
~New_Node()
{
delete left;
delete right;
}
};
struct compare
{
bool operator()(New_Node* l, New_Node* r)
{
return (l->freq > r->freq);
}
};
New_Node* top;
if(root->data == '$')
{
print_Code(root->left, str + "0");
print_Code(root->right, str + "1");
}
if(root->data != '$')
{
cout << root->data <<" : " << str << "\n";
print_Code(root->left, str + "0");
print_Code(root->right, str + "1");
}
}
public:
Huffman_Codes() {};
~Huffman_Codes()
{
delete top;
}
void Generate_Huffman_tree(vector<char>& data, vector<size_t>& freq, size_t size)
{
New_Node* left;
New_Node* right;
priority_queue<New_Node*, vector<New_Node*>, compare > minHeap;
while(minHeap.size() != 1)
{
left = minHeap.top();
minHeap.pop();
right = minHeap.top();
minHeap.pop();
top = new New_Node('$', left->freq + right->freq);
top->left = left;
top->right = right;
minHeap.push(top);
}
print_Code(minHeap.top(), "");
}
};
int main()
{
int n, f;
char ch;
Huffman_Codes set1;
vector<char> data;
vector<size_t> freq;
cout<<"Enter the number of elements \n";
cin>>n;
cout<<"Enter the characters \n";
OUTPUT 3
Ques 4
Implement Selection sort algorithm to sort the following list of numbers
55, 25, 15, 40, 60, 35, 17, 65, 75, 10
Calculate the following:
(i) Number of exchange operations performed.
(ii) Number of times comparison operation performed.
Source Code
#include <stdio.h>
void selection_sort();
int a[30], n;
void main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
selection_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
}
void selection_sort()
{
int i, j, min, temp;
int comp=0,swap=0;
for (i=0; i<n; i++)
{
min = i;
for (j=i+1; j<n; j++)
{
comp=comp+1;
if (a[j] < a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
swap=swap+1;
}
printf("Number of comparisons:%d \n Number of exchanges:%d",comp,swap);
}
OUTPUT 4
Ques 5
Examine implemented the performance of Quick soot algorithm on the set of
elements.
12 20 22 16 25 18 8 10 6 15
for the following list in terms of Comparisons, exchange operations and the
loop will iterate.
Source code
#include <stdio.h>
int scount = 0;
int count = 0;
// function to swap elements
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
scount++;
}
return 0;
}
OUTPUT 5
MCSL216 (PART II)
Ques 1:
Write a Javascript program to print current date and time
Source Code
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>Current Date & time</h1>
<p id="p1"></p>
<script>
var date = new Date();
document.getElementById("p1").innerHTML = date;
</script>
</body>
</html>
OUTPUT
Ques 2:
write a program to calculate addition subtraction multiplication and division
Source Code
<!doctype html>
<html>
<head>
<script>
var numOne, numTwo, res, temp;
function fun()
{
numOne = parseInt(document.getElementById("one").value);
numTwo = parseInt(document.getElementById("two").value);
if(numOne && numTwo)
{
temp = document.getElementById("res");
temp.style.display = "block"; res = numOne + numTwo;
document.getElementById("add").value = res; res = numOne - numTwo;
document.getElementById("subtract").value = res; res = numOne * numTwo;
document.getElementById("multiply").value = res; res = numOne / numTwo;
document.getElementById("divide").value = res;
}
}
</script>
</head>
<body>
<p id="input">Enter First Number: <input id="one"> <br/><br/>
Enter Second Number: <input id="two">
</p>
<p>
<button onclick="fun()">Add, Subtract, Multiply, Divide</button>
</p>
<p id="res" style="display:none;">
Addition Result = <input id="add"><br/><br/>
Subtraction Result = <input id="subtract"><br/><br/>
Multiplication Result = <input id="multiply"><br/><br/>
Division Result = <input id="divide">
</p>
</body>
</html>
OUTPUT
Question:3
Write a javascript code to change the contents of an element like header tag or
paragraph tag or division tag etc when user clicks on it.
Source Code
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h3> Javascript can change the content of an html element. </h3>
<p> Click the following text to see the effect. </p>
<p id = "para">Click me</p>
<script>
document.getElementById("para").onclick = function()
{
fun()
};
function fun()
{
Source Code
<html>
<head>
<script>
function VALIDATION()
{
var name = document.forms.RegForm.Name.value;
var email = document.forms.RegForm.EMail.value;
var phone = document.forms.RegForm.Telephone.value;
var password = document.forms.RegForm.Password.value;
var uname =document.forms.RegForm.Uname.value;
<style>
div {
box-sizing: border-box;
width: 100%;
border: 100px solid black;
float: left;
align-content: center;
align-items: center;
}
form {
margin: 0 auto;
width: 600px;
}
</style>
</head>
<body>
<h1 style="text-align: center;">REGISTRATION FORM</h1>
<form name="RegForm" onsubmit="return VALIDATION()" method="post">
</form>
</body>
</html>
OUTPUT