Académique Documents
Professionnel Documents
Culture Documents
Computing Lab
Lab Records
Roll No : 1864012
Lab Record 1
#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
#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 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
>a=2
>typeof(a)
output: "double"
>b=10L
>typeof(b)
output: "integer"
>c="Bhubaneswar"
>typeof(c)
output: "character"
>d=2i
>typeof(d)
output: "complex"
>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
output :
Enter the value of a 12
Enter the value of b 11
"a 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.
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"
Output :
Insert the number : 9
number is not prime
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 :
*
**
***
****
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
for(i in 1:5)
{
k<-i
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
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
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
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
sum<-function(a)
{
if(a>0)
{
return(a+sum(a-1))
}
else { return(a) }
}
print(sum(100))
fact<-function(a)
{
if(a>1) {return(a*fact(a-1))}
else {return(a)}
}
print(fact(6)
Output :
720
Lab Record 7
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
3. Input a matrix and find out the mid element and its neighbors.
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.
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
> 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
> head(cus)
Channel Region Fresh Milk Grocery Frozen Detergents_Paper Delicassen
> length(cus)
[1] 8
> class(cus)
[1] "data.frame"
> hist(cus$Region)
> length(cus1)
[1] 2666
> head(cus1)
[1] 2 2 2 1 2 2
> class(cus1)
[1] "integer"
> hist(cus1)
> cus3=cus[cus>5000]
> length(cus3)
[1] 854
> head(cus3)
> 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
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
> 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)
1 1 female 18 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))
> which(is.na(case$distance))
> 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
> which(is.na(case$age))
[1] 32.85516
> case=na.omit(case) #remove NAs
> plot(as.numeric(case$gender),as.numeric(case$mobility))
> abline(mod)