Practical File

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

Mewar University

Chittorgarh (Raj)

Computer Lab Assignment

Submitted by

Ravi Soni

M.Tech First Year

Ravi Soni Page 1


Object oriented Programming

1. WAP to print diagonal matrix.


2. WAP to sort an array using bubble sort.
3. WAP to sort an array using insertion sort.
4. WAP to sort an array using selection sort.
5. WAP to convert lower-case sentence in to uppercase sentence.
6. WAP to calculate area of triangle.
7. WAP to print a diamond of stars (*).
8. WAP to convert decimal into Octal and Hexadecimal.

Shell Program
1. Write a shell program to find the average of students marks using for loop and while loop.
2. Write a shell program to convert temperature in centigrade to Fahrenheit.
3. Write a shell script that accepts a file name starting and line number as arguments and
display all line.
4. Write a shell script to print months in a year using switch case.
5. Write a shell script to compute all arithmetic operations on which are given by user.
6. Write a shell script to find the area of the triangle.
7. Write a shell script that accept two integers as its argument and computes the value of first
number raised to the power of the second number.

/* Program to print diagonal matrix. */

Ravi Soni Page 2


#include<conio.h>
#include<stdio.h>
void main()
{
int a[4][4],i,j;
clrscr( );
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(i==j)
a[i][j]=7;
else
a[i][j]=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf(“%d”,a[i][j]);
printf(“\n”);
}
getch();
}
Output :

/* Program to sort an array using Bubble Sort, Insertion Sort & Selection Sort */

Ravi Soni Page 3


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
long int sort[7];
void menu()
{
clrscr();
printf("SORTING TECHNIQUES\n\n");
printf("\n1.INPUT DATA (NUMERIC)\n2.SELECTION SORTING\n");
printf("3.BUBBLE SORTING\n4.INSERTION SORTING\n5.EXIT");
printf("\n\nEnter your choice:");
}
void swap(int arr[],int i,int j)
{
int temp;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
void bubble(int arr[],int n)
{
int flag;
int i,j;
for(i=0;i<n-1;i++)
{
flag=0;
for(j=0;j<n-i-1;j++)

Ravi Soni Page 4


{
sort[1]++;
if(arr[j]>arr[j+1])
{
flag=1;
swap(arr,j,j+1);
}
}
if(!flag)
break;
}
return;
}
void selection(int arr[],int n)
{
int min,pos;
int i,j;
for(i=0;i<n-1;i++)
{
min=arr[i];
pos=i;
for(j=i+1;j<n;j++)
{
sort[0]++;
if(min>arr[j])
{
min=arr[j];
pos=j;
}

Ravi Soni Page 5


}
swap(arr,i,pos);
}
}
void insertion(int arr[],int n)
{
int i,j;
int pivot;
for(i=0;i<n-1;i++)
{
pivot=arr[i+1];
j=i;
sort[2]++;
while(arr[j]>pivot&&j>=0)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=pivot;
}
return;
}
void input(int arr[] , int n)
{
int i;
printf(" \n1.RANDOM NUMBERS\n2.USER ");
printf("\n\nEnter the choice:");
scanf("%d",&i);
if(i==2)

Ravi Soni Page 6


{
printf("\nNow enter the elements:");
for(i=0;i<n;i++)
{
printf("\nEnter %d element: ",i+1);
scanf("%d",&arr[i]);
}
}
else
{
for(i=0;i<n;i++)
arr[i]=random(30000);
}
return;
}
void display(int arr[],int n)
{
int i;
clrscr();
printf("\nThe elments are:\n");
for(i=0;i<n;i++)
{
if(i%9==0)
printf("\n");
if(i%180==0&&i!=0)
{
printf("\nPress any key to continue...\n");
getch();
clrscr();

Ravi Soni Page 7


}
printf("%-8d",arr[i]);
}
}
void printcomp(long int comp)
{
printf("The no of comarisons performed are : %ld",comp);
printf("\n\nPress any key to see the sorted data...");
getch();
}
void main()
{
int n=0;
int ch,i;
int arr[1000];
int crr[1000];
while(1)
{
menu();
scanf("%d",&ch);
clrscr();
switch(ch)
{
case 1:
printf("Enter the no. of elements:");
scanf("%d",&n);
input(arr,n);
for(i=0;i<n;i++)
crr[i]=arr[i];

Ravi Soni Page 8


break;
case 2: sort[0]=0;
selection(arr,n);
printcomp(sort[0]);
break;
case 3: sort[1]=0;
bubble(arr,n);
printcomp(sort[1]);
break;
case 4: sort[2]=0; insertion(arr,n);
printcomp(sort[2]);
break;
case 5: exit(0);
}
printf("\nThe current status of array:\n");
display(arr,n);
for(i=0;i<n;i++)
arr[i]=crr[i];
printf("\nPress any key to return to main menu ....");
getch();
}
}

Output:

Ravi Soni Page 9


Ravi Soni Page 10
/* Write a C Program to convert the lower case letters to upper case and vice-versa */

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
void main()
{
char sentence[100];
int count, ch, i;
clrscr();
printf("Enter a sentence\n");
for(i=0; (sentence[i] = getchar())!='\n'; i++)
{ ; }
count = i;
printf("\nInput sentence is : %s ",sentence);
printf("\nResultant sentence is : ");
for(i=0; i < count; i++)
{
ch = islower(sentence[i]) ? toupper(sentence[i]) :
tolower(sentence[i]); putchar(ch);
}
getch();
}

Ravi Soni Page 11


Output :

/* Program to convert decimal into Octal and Hexadecimal. */


#include<stdio.h>
#include<conio.h>
#include<string.h>
void db();
void doc();
void dh();
void main()
{
int n;
char c;
begin:
clrscr();
printf("\t\t\t****MAIN MENU****\n");
printf("Enter your choise.\n");
printf("1. Decimal to Binary.\n");
printf("2. Decimal to Octal.\n");
printf("3. Decimal to Hexadecimal.\n");
scanf("%d",&n);
if(n<1 || n>3)
printf("Invalid Choise\n");
if(n==1)
db();

Ravi Soni Page 12


else if(n==2)
doc();
else if(n==3)
{
long a;
clrscr();
printf("Conversion from Decimal to hexadecimal\n");
printf("Enter the decimal number.\n");
scanf("%ld",&a);
dh(a);
}
getch();
}
void db()
{
int dec,bin,n,i=0,a[10];
clrscr();
printf("Conversion from Decimal to Binary\n");
printf("Input decimal no.");
scanf("%d",&dec);
do
{
a[i]=dec%2;
dec=dec/2;
i++;
} while(dec!=0);
for(n=i-1;n>=0;n--)
printf("%d",a[n]);
}

Ravi Soni Page 13


void doc()
{
int n,i,a[10];
clrscr();
printf("Conversion from Decimal to Octal\n");
printf("Enter a Decimal number\n");
scanf("%d",&n);
i=0;
printf("Octal equavalent of %d is ",n);
while(n!=0)
{
a[i]=n%8;
n=n/8;
i++;
}
i--;
for(;i>=0;i--)
printf("%d",a[i]);
}
void dh(long n)
{
long i;
if(n>0)
{
i=n%16;
n=n/16;
dh(n);
if(i>=10)
{

Ravi Soni Page 14


switch(i)
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
}
}
else
printf("%ld",i);
}
}

Output:

Ravi Soni Page 15


Ravi Soni Page 16
/* Program to calculate area of the triangle */
#include<iostream.h>

#include<conio.h>

#include<math.h>

void main()

int a,b,c,s;

clrscr();

cout<<"Please enter the sides of the triangle:";

cin>>a>>b>>c;

s=(a+b+c)/2;

s=sqrt(s*(s-a)*(s-b)*(s-c));

cout<<"The area is : "<<s;

getch();

Output:

Ravi Soni Page 17


/* Program to print a diamond of stars (*).*/
#include<stdio.h>

#include<conio.h>

void main()

int i,j,k,n;

clrscr();

n=4;

for(i=0;i<5;i++)

for(k=n;k>=0;k--)

printf(" ");

for(j=i+1;j>0;j--)

printf(" *");

printf("\n");

n--;

n=0;

for(i=4;i>0;i--)

for(k=n+2;k>0;k--)

Ravi Soni Page 18


printf(" ");

for(j=i;j>0;j--)

printf(" *");

printf("\n");

n++;

getch();

Output:

Ravi Soni Page 19


Write a shell script that accepts a file name starting and ending numbers as Arguments and
displays all the lines between the given line numbers.

Shell Script

#!/bin/csh

head -$argv[3] $argv[1] | tail -n $argv[2]

--------------------------------------------------------------

Write a shell script that accepts two integers as its arguments and computes the value of first
number raised to the power of the second number.

Shell Script

#!/bin/csh

set c = 1

set result = 1

while($c <= $argv[2])

@ result *= $argv[1]

@ c = $c + 1

end

echo "$argv[1] raised to the power of $argv[2]=$result"

--------------------------------------------------------------

Ravi Soni Page 20


Write a shell program to convert temperature in centigrade to Fahrenheit.

Shell Script

echo "Converting between the different temperature scales"


echo "1. Convert Celsius temperature into Fahrenheit"
echo "2. Convert Fahrenheit temperatures into Celsius"
echo -n "Select your choice (1-2) : "
read choice
 
if [ $choice -eq 1 ]
then
echo -n "Enter temperature (C) : "
read tc

# formula Tf=(9/5)*Tc+32

tf=$(echo "scale=2;((9/5) * $tc) + 32" |bc)


echo "$tc C = $tf F"
elif [ $choice -eq 2 ]
then
echo -n "Enter temperature (F) : "
read tf

# formula Tc=(5/9)*(Tf-32)

tc=$(echo "scale=2;(5/9)*($tf-32)"|bc)
echo "$tf = $tc"
else
echo "Please select 1 or 2 only"
exit 1
fi

--------------------------------------------------------------

Ravi Soni Page 21


Write a shell program to find the average of students’ marks

Shell Script

for i in $*
do
temp_total=`expr $temp_total + $i`
done
avg=`expr $temp_total / $number_of_args`
echo "Average of all marks is $avg"

--------------------------------------------------------------

Write a shell script to find the area of the triangle

Shell Script

echo -n "Enter base of a triangle : "


read b
 
echo -n "Enter height of a triangle : "
read h
# calculates it and display back
area=$(echo "scale=2;(1/2) * $b * $h"|bc)
echo "Area of a triangle is $area"
a=$1
op="$2"
b=$3
 

--------------------------------------------------------------

Ravi Soni Page 22


Write a shell script to compute all arithmetic operations on which are given by user.

Shell Script

if [ $# -lt 3 ]
then
echo "$0 num1 opr num2"
echo "opr can be +, -, / , x"
exit 1
fi
 
case "$op" in
+) echo $(( $a + $b ));;
-) echo $(( $a - $b ));;
/) echo $(( $a / $b ));;
x) echo $(( $a * $b ));;
*) echo "Error ";;
esac

--------------------------------------------------------------

Write a shell script that accepts two integers as its arguments and computes the value of first
number raised to the power of the second number.

Shell Script

#!/bin/csh

set c = 1

set result = 1

while($c <= $argv[2])

@ result *= $argv[1]

Ravi Soni Page 23


@ c = $c + 1

end

echo "$argv[1] raised to the power of $argv[2]=$result"

Ravi Soni Page 24

You might also like