Vous êtes sur la page 1sur 28

School Of Computer Engineering

Computing Lab
Lab Records

Name : Ahmad Alsharef

Roll No : 1864012
Lab Record 1

1. Write a program to take a group of words as an input from the


user and arrange it in a dictionary manner.

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
char name[10][8],temp[9];
int i,j,n;
printf("enter the value n:\n");
cin >> n;
for (i=0;i<n;i++)
scanf("%s",name[i]);

for (i=0;i<n-1;i++)
{
for (j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("after sorting string is:\n");
for(i=0;i<n;i++) printf(" %s ",name[i]);
return 1;
}

Output :
enter the value n:
4
Delhi
Mumbai
Chennai
Goa
after sorting string is:
Chennai Delhi Goa Mumbai
2. Write a Program to find the middle element of any pattern and
its neighbor elements.

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
int n;
cout<<" Enter the size of the matrix";
cin>>n;
int median;
cout<<" Enter the ";
for (int i=0;i<n;i++)
for (int j=0;i<n;i++)
{
cout<<" Enter the value a[%d][%d]",i,j;
cin>>a[i][j]
}
if (n % 2 != 0) median=a[n/2+1][n/2+1];
if (n%2 == 0)
median= (a[(n-2)/2][n-1] +a[n/2][0])/2.0;
cout<< "Median : "<< median << endl;
return 0;
}
Output :
Enter the size of the matrix : 3
Enter the value a[0][0] : 1
Enter the value a[0][1] : 2
Enter the value a[0][2] : 3
Enter the value a[1][0] : 4
Enter the value a[1][1] : 5
Enter the value a[1][2] : 6
Enter the value a[2][0] : 7
Enter the value a[2][1] : 8
Enter the value a[2][2] : 9
Median :5
lab Record 2

1. Write a program to create the Binary search tree and perform


all its traversal operations.

#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left, *right;
};
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

void inorder(struct node *root)


{
if (root != NULL)
{
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);

}
}
void postorder(struct node *root)
{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d ", root->key);
}
}
void preorder(struct node *root)
{
if (root != NULL)
{
printf("%d ", root->key);
preorder(root->left);
preorder(root->right);
}
}
struct node* insert(struct node* node, int key)
{
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
return node;
}

int main()
{
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);

printf("Preorder :\n");
preorder(root);
printf("\nInorder : \n");
inorder(root);
printf("\nPostorder :\n");
postorder(root);

return 0;
}
Lab Record 3

1. Initialize some variables in R workspace and display what data


type of variable you created.

>a=2
>typeof(a)
output: "double"

>b=10L
>typeof(b)
output: "integer"

>c="Bhubaneswar"
>typeof(c)
output: "character"

>d=2i
>typeof(d)
output: "complex"

2. write a script to calculate Si.

>print("Enter principal value : ")


>p=10
>print("Enter Rate value : ")
>r=10
>print("Enter amount value : ")
>q=10
>si=p*q*r/100
>print(si)
output: 10

3. find out the area of circle.

>print("enter the radius of the circle")


>r=2
>area=3.14*r*r
>print(area)
output: 12.56
4. Write a script to findout the sum of digits of a three digit
number.

>readline(prompt="insert the number")


>num=as.integer(num)
>sum=0L
>sum=sum+round(num%%10)
>num=round(num/10)
>sum=sum+round(num%%10)
>num=round(num/10)
>sum=sum+round(num%%10)
>num=round(num/10)
>print(sum)
output: insert the number: 123
6

5. Write a script to convert a binary number to decimal number


and binary number should be in 3 bit.

>val=0L
>base=1L
>num=readline(prompt="Enter the 3 digits binary number: ")
>num=as.integer(num)
>rem=num%%10
>val=val+rem*base
>num=round(num/10)
>base=base*2
rem=num%%10
>val=val+rem*base
>num=round=(num/10)
>base=base*2
>print("Result")
>print(val)
output: Enter the 3 digits binary number
Result 5
Lab Record 4

1. Input 2 numbers and display the largest.

a=as.integer(readline(prompt="Enter the value of a"))


b=as.integer(readline(prompt="Enter the value of b"))
if (a>b)
{
print("a is the largest")
}
else
{
print("b is the largest")
}

output :
Enter the value of a 12
Enter the value of b 11
"a is the largest"

2. Input three numbers and find out the greatest.

a=as.integer(readline(prompt="Enter the value of a"))


b=as.integer(readline(prompt="Enter the value of b"))
c=as.integer(readline(prompt="Enter the value of c"))
if(a>b&a>c)
{print("a is the largest")}
else if(b>a&b>c)
{ print("b is the largest")}
else
{print("c is the largest")}

output :
Enter the value of a 2
Enter the value of b 1
Enter the value of b 13
"c is the largest"
3. Input three numbers and find out the second largest number.

a=as.integer(readline(prompt="Enter the value of a"))


b=as.integer(readline(prompt="Enter the value of b"))
c=as.integer(readline(prompt="Enter the value of c"))
if((a>b&a<c)||(a>c&a<b))
{ print("a is the second largest number")}
else
if((b>a&b<c)||(b>c&b<a))
{ print("b is the second largest number")}
else
{ print("c is the second largest number")}

output :
Enter the value of a 22
Enter the value of b 20
Enter the value of b 18
"b is the second largest number"

4. Input one number and check whether it is odd or even and


whether it is positive or negative.
a=as.integer(readline(prompt("Insert a"))
if (a%%2=0) {print("a is even")}
else {print("a is odd")}
if (a>0) {print("a is positive")}
else {print("a is negative")}
output :
Insert a -5
a is odd
a is negative
Lab Record 5

1. Input a number and check whether it is prime or not.

num=as.integer(readline(prompt="Insert the number"))


prime=0
if (num==1) prime=0
for (i in 2:(n-1))
{if ((num%%i==0)
{prime=0
break}
}
if(prime==1) print("number is prime")
else print("number is not prime")

Output :
Insert the number : 9
number is not prime

2. Input a number and check whether the number is armstrong or


not.

num = as.integer(readline(prompt="Insert a number: "))


sum = 0
temp = num
while(temp > 0) {
digit = temp %% 10
sum = sum + (digit ^ 3)
temp = floor(temp / 10)
}
if(num == sum) {
print(paste(num, "is armstrong "))
} else {
print(paste(num, "is not armstrong "))
}

Output :
Insert a number : 370
"370 is armstrong"
3. Write R program to write :
*
**
***
**** .

for (i in 1:4)
{
for(j in 1:i) {print("*")}
print("\n")
}

Output :
*
**
***
****

4. Write R program to write :


1
01
101
0101

for (i in 1:4)
{
for(j in 1:i)
{
if((i+j)%%2==0) print("1")
else print("0")
}
print("\n")
}

Output :
1
01
101
0101
5. write R program to draw :
1
12
123
1234
12345 .

for(i in 1:5)
{
for (j in 1:5-i) print(" ")
for (k in 1:i) print(i)
}
output :
1
12
123
1234
12345

6. Write R program to draw


1
121
12321
1234321
123454321

for(i in 1:5)
{
k<-i

for (j in 1:5-i) print(" ")


for (k in 1:i) print(i)
while(k!=1)
{
print(k-1)
k<-k-1
}
print("\n")
}
Output :
1
121
12321
1234321
123454321
7. Write R program to check whether a number is odd or even using
switch case.

a=as.integer(readline("Insert a number:"))
c<-(a%%2)
switch(c,print("Even"),print("Odd"))
output :
Insert a number : 4
Even

8. Findout the sum and average of prime numbers between 2 and 50.

count<-0
sum<-0
for(n in 2:50)
{
for (i in 2:n/2)
{
if((n%%i)==0)
{
sum=sum+i
count<-count+1
}
}
}
avg=sum/count
print("count : ",count,"\n")
print("sum : ",sum,"\n")
print("avg : ",avg,"\n")
Output:
count : 15
sum : 328
avg : 21.86667

9. Write R program to check whether number is perfect or not.

i=1
sum=0
num=as.integer(readline(prompt="Insert a number"))
while(i<num)
{
if(num%%i==0)
{
sum=sum+i
i++
}
}
if (sum==num) print("Number is perfect")
else print("Number is not perfect")
Output :
Insert a number : 12
Number is not perfect

10. Write a program to input a number to convert it from :


1. Decimal to Binary.
2. Binary to Decimal.

Binary <-function(d)
{
bsum<-0
bexp<-1
while(d>0)
{digit<-d%%2
bsum<-bsum+digit*bexp
bexp<-bexp*10}
return(bsum)
}
Decimal<-function(b)
{
dsum<-0
dexp<-1
while(b>0)
{digit<-b%%10
b<-floor(b/10)
dsum<-dsum+digit*bexp
bexp<-bexp*2}
return(dsum)
}
d<-readline("Insert Decimal : ")
d<-as.numeric(d)
b<-binary(d)
print("Binary: ",b)
d<decimal(b)
print("Decimal: ",d)
Output :
Insert Decimal : 6
Binary: 110
Decimal: 6
Lab Record 6

1. Write R program to display all arithmetic operation line Add,


Subtraction, Multiplication, Division in 4 separate functions
like function1, function2, function3, function4 by
illustrating a=of all the methodology of passing argument and
return type.

addition<-function(a,b)
{
c=a+b
return(c)
}
subtract<-function(a,b)
{
c=a-b
return(c)
}
multiply<-function(a,b)
{
c=a*b
return (c)
}
division<-function(a,b)
{
if(b!=0)
c=a/b
else print("error")
return(c)
}
a<-as.integer(readline(prompt="a: "))
b<-as.integer(readline(prompt="b: "))
print("addition:\n")
print(addition(a,b))
print("subtract:\n")
print(subtract(a,b))
print("multiply:\n")
print(multiply(a,b))
print("division:\n")
print(division(a,b))

Output:
a: 3
b: 3
addition:
6
subtract:
0
multiply:
9
division:
1

2. Write a program to find out the sum of n natural numbers using


a recursive function.

sum<-function(a)
{
if(a>0)
{
return(a+sum(a-1))
}
else { return(a) }
}
print(sum(100))

3. Write a program to find out the factional of n natural numbers


using a recursive function.
sum<-function(a)
{
if(a>0)
{
return(a+sum(a-1))
}
else { return(a) }
}
print(sum(100))

4. Write a program to find out the factorial of a number using


recursion function.

fact<-function(a)
{
if(a>1) {return(a*fact(a-1))}
else {return(a)}
}
print(fact(6)
Output :
720
Lab Record 7

1. Write R program to check whether the matrix is square or not.

cat( "Random Dimensions (Between 1 and 4) of the Matrix will be


generated\n")
a <- sample(1:4, 1)
b <- sample(1:4, 1)
cat("Rows Count : ",a,"\n")
cat("Columns Count : ",b,"\n")

M<-matrix(1:a*b, nrow=a)

if ( nrow(M)==ncol(M))
{
cat("Square")
} else
{
cat("Not Square")
}

Output :
Random Dimensions (Between 1 and 4) of the Matrix will be
generated
Rows Count : 4
Columns Count : 1
Not Square

2. Input two matrices and find out sum and mull.

cat("Random Dimensions (1..4) Matrices will be generated\n")


d <- sample(1:4, 1)
M1=matrix(sample.int(15, size = d*d), nrow = d, ncol = d)
M2=matrix(sample.int(15, size = d*d), nrow = d, ncol = d)
cat("Matrix1 is :\n")
M1
cat("Matrix2 is :\n")
M2
Summation=M1+M2
Multipliciation=M1*M2
cat("Summation is :\n")
Summation
cat("Multipliciation is :\n")
Multipliciation
Output :
Random Dimensions (1..4)Matrices will be generated
Matrix1 is :
[,1] [,2] [,3]
[1,] 6 10 1
[2,] 2 4 15
[3,] 12 8 14
Matrix2 is :
[,1] [,2] [,3]
[1,] 14 9 5
[2,] 1 10 6
[3,] 15 2 3
Summation is :
[,1] [,2] [,3]
[1,] 20 19 6
[2,] 3 14 21
[3,] 27 10 17
Multiplication is :
[,1] [,2] [,3]
[1,] 84 90 5
[2,] 2 40 90
[3,] 180 16 42

3. Input a matrix and find out the mid element and its neighbors.

cat( "Random Dimensions (Between 3 and 5) Matrix will be


generated\n")
roww <- sample(3:5, 1)
coll <- sample(3:5, 1)
M=matrix(sample.int(25, size = roww*coll), nrow = roww, ncol =
coll)
M
m=M[ceiling(roww/2),ceiling(coll/2)]
l=M[ceiling(roww/2),ceiling(coll/2)-1]
r=M[ceiling(roww/2),ceiling(coll/2)+1]
t=M[ceiling(roww/2)-1,ceiling(coll/2)]
b=M[ceiling(roww/2)+1,ceiling(coll/2)]
cat("\nMiddle is :",m)
cat("\nLeft is :",l)
cat("\nRight is :",r)
cat("\nTop is :",t)
cat("\nBottom is :",b)
Output:
Random Dimensions (Between 3 and 5) Matrix will be generated
[,1] [,2] [,3] [,4] [,5]
[1,] 14 25 1 6 22
[2,] 10 11 12 20 21
[3,] 2 3 24 5 4
[4,] 9 15 18 19 7
[5,] 8 13 17 16 23

Middle is : 24
Left is : 3
Right is : 5
Top is : 12
Bottom is : 18

4. Input one matrix and find out its min and max number.

cat( "Random Dimensions (Between 1 and 3) Matrix will be


generated\n")
roww <- sample(1:3, 1)
coll <- sample(1:3, 1)
M=matrix(sample.int(25, size = roww*coll), nrow = roww, ncol =
coll)
cat("Matrix is :\n")
M
cat("\nMax is : ",max(M))
cat("\nMin is : ",min(M))

Output :
Random Dimensions (Between 1 and 3) Matrix will be generated
Matrix is :
[,1] [,2] [,3]
[1,] 14 4 23
[2,] 3 17 22
[3,] 5 19 15
Max is : 23
Min is : 3
Lab Record 8
Box Plots

1. Apply different measures of variability over a common


dataset(variance, standard deviation, mean, max, median,
absolute deviation).

> data=read.csv("C:/Users/ALM/Downloads/LungCapData.csv")

> var(data$Age)

[1] 16.03802

> sd(data$Age)

[1] 4.00475

> mean(data$Age)

[1] 12.3269

> median(data$Age)

[1] 13

> max(data$Age)

[1] 19

> mad(data$Age)

[1] 4.4478

2. Segmentation of customers (<5000, =5000, >5000)[customer


dataset]
 Outlier detection
 missing values handling of gender, distance, mobility, and
age.
 Normalization.
 K-means clustering algo for performing segmenation.
>cus=read.csv("C:/Users/ALM/Downloads/customersegmentation.csv")

> head(cus)
Channel Region Fresh Milk Grocery Frozen Detergents_Paper Delicassen

1 2 3 12669 9656 7561 214 2674 1338

2 2 3 7057 9810 9568 1762 3293 1776

3 2 3 6353 8808 7684 2405 3516 7844

4 1 3 13265 1196 4221 6404 507 1788

5 2 3 22615 5410 7198 3915 1777 5185

6 2 3 9413 8259 5126 666 1795 1451

> length(cus)

[1] 8

> class(cus)

[1] "data.frame"

> hist(cus$Region)

> boxplot(cus$Region, horizontal = TRUE)


> cus1=cus[cus<5000]

> length(cus1)

[1] 2666

> head(cus1)

[1] 2 2 2 1 2 2

> class(cus1)

[1] "integer"

> boxplot(cus1, horizontal = T, main="boxblot <5000")

> hist(cus1)

> cus3=cus[cus>5000]

> length(cus3)

[1] 854

> head(cus3)

[1] 12669 7057 6353 13265 22615 9413

> class(cus3)

[1] "integer"
> boxplot(cus3, horizontal = T, main="boxblot >5000")

> hist(cus3)

# Normalization
>x=(cus-min(cus)/(max(cus)-min(cus)))

>View(x)

> results<- kmeans(cus, 3)

> results

K-means clustering with 3 clusters of sizes 330, 50, 60


Cluster means:
Channel Region Fresh Milk Grocery Frozen

1 1.260606 2.554545 8253.47 3824.603 5280.455 2572.661

2 1.960000 2.440000 8000.04 18511.420 27573.900 1996.680

3 1.133333 2.566667 35941.40 6044.450 6288.617 6713.967

Detergents_Paper Delicassen

1 1773.058 1137.497

2 12407.360 2252.020

3 1039.667 3049.467

Clustering vector:
[1] 1 1 1 1 3 1 1 1 1 2 1 1 3 1 3 1 1 1 1 1 1 1 3 2 3 1 1 1

[29] 2 3 1 1 1 3 1 1 3 1 2 3 3 1 1 2 1 2 2 2 1 2 1 1 3 1 3 1

[57] 2 1 1 1 1 2 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1

[85] 1 2 2 3 1 3 1 1 2 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 2 1 2

[113] 1 1 1 1 1 1 1 1 1 1 1 1 3 3 1 1 1 3 1 1 1 1 1 1 1 1 1 1

[141] 1 3 3 1 1 2 1 1 1 3 1 1 1 1 1 2 1 1 1 1 1 1 1 2 1 2 1 1

[169] 1 1 1 2 1 2 1 1 3 1 1 1 1 3 1 3 1 1 1 1 1 1 1 1 1 1 1 1

[197] 3 1 1 1 2 2 3 1 1 2 1 1 1 2 1 2 1 1 1 1 2 1 1 1 1 1 1 1

[225] 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 3 3 3 1 1 1 1 1 1 1 1 1 2

[253] 1 3 1 3 1 1 3 3 1 1 3 1 1 2 2 1 2 1 1 1 1 3 1 1 3 1 1 1

[281] 1 1 3 3 3 3 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 2 1 1 2 1 2 1

[309] 1 2 1 3 2 1 1 1 1 1 1 2 1 1 1 1 3 3 1 1 1 1 1 2 1 2 1 3

[337] 1 1 1 1 1 1 1 2 1 1 1 3 1 2 1 2 1 2 1 1 1 1 1 1 1 1 1 1

[365] 1 1 1 1 1 1 3 1 1 1 1 1 1 3 1 1 3 1 3 1 2 1 1 1 1 1 1 1

[393] 1 3 1 1 1 1 1 1 1 3 3 3 1 1 3 2 1 1 1 1 1 1 1 1 1 1 2 1

[421] 1 1 3 1 1 1 1 3 1 1 1 1 1 1 1 3 3 2 1 1

Within cluster sum of squares by cluster:

[1] 28184319111 26382784712 25765310355

(between_SS / total_SS = 49.0 %)


Available components:

[1] "cluster" "centers" "totss"

[4] "withinss" "tot.withinss" "betweenss"

[7] "size" "iter" "ifault

> table(cus$Channel,results$cluster)

1 2 3

1 244 2 52

2 86 48 8

>boxplot(results)

> plot(cus$Region,col=results$cluster)
3. Four attributes handling of Gender, Distance, Mobility, Age
[Case Study dataset].
 Outlier detection.
 missing values handling of gender, distance, mobility, and
age.
 linear Regression between attributes effect of gender and
mobility over the dataset.

> case=read.csv("C:/Users/ALM/Downloads/casestudy.csv")

> class(case)

[1] "data.frame"

> head(case)

Y gender distance mobility

1 1 female 18 car

2 1 female 220 car

3 1 female 22 car

4 0 male 35 car

5 0 female 2 car

6 1 male 15 car

age

1 74

2 76

3 54

4 60

5 62

6 40

> length(case)

[1] 5
> boxplot(case,main="CaseStudy BoxPlot")

> case[case=="na"]<-NA

> which(is.na(case$gender))

[1] 10 13 62 63 66 200 201 210 267 281 292 297 404

> which(is.na(case$distance))

[1] 10 13 62 63 66 200 201 210 253 267 281 292 404

> which(is.na(case$mobility))
[1] 10 13 62 63 66 109 139 145 161 182 186 187 200 201 205 207

[17] 210 226 227 229 237 240 246 251 252 259 267 268 269 271 274 278

[33] 281 283 292 294 298 303 304 316 319 329 333 336 357 363 366 388

[49] 389 390 399 404 412 413 420 421 423 431 432 434 443 446 456 458

[65] 459 468 478 482 484 489 507

> which(is.na(case$age))

[1] 10 13 62 63 66 200 201 210 267 281 292 404

> mean(as.numeric(case$age),na.rm = TRUE)

[1] 32.85516
> case=na.omit(case) #remove NAs

> plot(as.numeric(case$gender),as.numeric(case$mobility))

mod <- lm(as.numeric(gender)~as.numeric(mobility))

> abline(mod)

Vous aimerez peut-être aussi