Sei sulla pagina 1di 23

ASSIGNMENT 00

1) Write a program to sort the array elements by array in to user defined function sort().
#include <stdio.h>
void sort(int m, int x[ ]);
int main()
{
int a[100],n,i,j,tmp;
printf("Array size: ");
scanf("%d",&n);
printf("Elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort (n, a);
return 0;
}
void sort(int n, int a[ ])
{
int i,j,tmp;
for ( i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
{
if (a[j] > a[i])
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
printf("\n\nAscending : ");
for (i = 0; i < n; i++)
{
printf(" %d ", a[i]);
}
for ( i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
{
if (a[j] < a[i])
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
printf("\n\nDescending : ");
for (i = 0; i < n; i++)
{
printf(" %d ", a[i]);
}

2) Write a program to reverse a string uing pointers.


#include <stdio.h>
int main()
{
char str[100];
char rev[100];
char *sptr = str;
char *rptr = rev;
int i = -1;
printf("\nEnter a string: ");
scanf("%s", str);
while(*sptr)
{
sptr++;
i++;
}
while(i >= 0)
{
sptr--;
*rptr = *sptr;
rptr++;
i--;
}
*rptr = '\0';
rptr = rev;
while(*rptr)
{
*sptr = *rptr;
sptr++;
rptr++;
}
printf("\nReverse of the string is: %s ", str);
return 0;
}
3) Write a program to search an element in the array usimg linear search.
#include <stdio.h>
void inputArray(int * arr, int size);
int search(int * arr, int size, int toSearch);
int main()
{
int array[100],i;
int size, toSearch, searchIndex;
printf("Enter size of array: ");
scanf("%d", &size);
printf("Enter %d Elements:",size);
for(i=0;i<size;i++)
{
scanf("%d",array+i);
}
printf("Enter element to search: ");
scanf("%d", &toSearch);
searchIndex = search(array, size, toSearch);
if(searchIndex == -1)
printf("%d does not exists in array.", toSearch);
else
printf("%d is found at %d position.", toSearch, searchIndex + 1);
return 0;
}
int search(int * arr, int size, int toSearch)
{
int index = 0;
int * arrEnd = (arr + size - 1);
while(arr <= arrEnd && *arr != toSearch)
{
arr++;
index++;
}
if(arr <= arrEnd)
return index;
return -1;
}

4) Write a program to search an element in the array usimg binary search.

#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
{
scanf("%d",&array[c]);
}
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last)
{
if (array[middle] < search)
first = middle + 1;
else if (array[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 isn't present in the list.\n", search);
return 0;
}

5) Create a book structure containing book id, title, author name and price. Write a C
program to pass a structure as a function argument and print the book details.

#include<stdio.h>
struct Book
{
int book_id;
char title[50];
char author_name[50];
int price;
};
void display(struct Book b);
int main()
{
struct Book b1;
printf("Enter book id:");
scanf ("%d", &b1.book_id);
printf("Enter title:");
scanf("\n%[^\n]%*c", &b1.title);
printf("Enter author name:");
scanf ("\n%[^\n]%*c", &b1.author_name);
printf("Enter price:");
scanf ("\n%d", &b1.price);
display(b1);
return 0;
}
void display(struct Book b)
{
printf("\nDisplaying information\n");
printf("\nBook id: %d", b.book_id);
printf("\nTitle: %s", b.title);
printf("\nAuthor name: %s", b.author_name);
printf("\nprice: %d", b.price);
}
ASSIGNMENT 01

1) Write a program to implement singly linked list(menu driven).


#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
}*head = NULL;
void insert_at_beg(struct Node *head);
void insert_at_pos(struct Node *head);
void insert_at_end(struct Node *head);
void del(struct Node *head);
void traverse(struct Node *head);
void search(struct Node *head,int pos);
void printList(struct Node *head)
{
struct Node *temp = head;
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void push(struct Node** head, int new_data)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = *head;
*head = new_node;
}
void main()
{
int ch1,pos,n,i,ch2;
push(&head, 10);
push(&head, 5);
push(&head, 25);
push(&head, 20);
while(1)
{
printf("\n*******menu*******\n");
printf("1.Insertion\n2.Deletion\n3.Traversing\n4.Searching\n");
printf("enter your choice:\n");
scanf("%d",&ch1);
switch(ch1)
{
case 1: printf("\nWhere do you want to insert\n");
printf("1.At beginning\n2.At a specific position\n3.At end\n");
scanf("%d",&ch2);
if(ch2==1)
insert_at_beg(head);
else if(ch2==2)
insert_at_pos(head);
else if(ch2==3)
insert_at_end(head);
break;

case 2: del(head);
break;
case 3: traverse(head);
break;
case 4: printf("\nEnter position to be searched:");
scanf("%d",&pos);
search(head,pos);
break;
default: exit(0);

}
}

void insert_at_beg(struct Node *head)


{
int new_data;
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
printf("Enter data to be inserted:");
scanf("%d",&new_data);
new_node->data = new_data;
new_node->next = head;
head = new_node;
printList(head);
printf("\none node inserted!!!\n");
}
void insert_at_pos(struct Node *head)
{
int new_data,pos,count=1;
struct Node *temp = head,*current = head;
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
printf("Enter data to be inserted:");
scanf("%d",&new_data);
printf("Enter the position where it is to be inserted:");
scanf("%d",&pos);
while(temp!=NULL)
{
count++;
temp=temp->next;
}
if(pos<1 || pos>count)
{
printf("\nInvalid position!");
return;
}
int i=1;
while(i<pos)
{
current = current->next;
i++;
}
new_node->data=new_data;
new_node->next = current->next;
current->next = new_node;
printList(head);
printf("\n one node inserted\n");
}

void insert_at_end(struct Node *head)


{
int new_data;
struct Node *temp=head,*new_node;
new_node = (struct Node*) malloc(sizeof(struct Node));
printf("\nEnter data to be inserted:");
scanf("%d",&new_data);
new_node->data = new_data;
while(temp!=NULL)
{
temp=temp->next;
}
temp->next=new_node;
new_node->next=NULL;
printList(head);
printf("\none node inserted!!!\n");
}

void del(struct Node *head)


{
struct Node *temp=head,*prev;
int del_data;
printf("Enter the data which is to be deleted:");
scanf("%d",&del_data);
while(temp->next->data!=del_data)
{
temp=temp->next;
}
struct Node *t1=temp->next;
temp->next = temp->next->next;
free(t1);
}
void traverse(struct Node *head)
{
struct Node *temp=head;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
printf("\n");
}

void search(struct Node *head,int pos)


{
int i=1,count=1;
struct Node *temp=head;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
if(pos<1 || pos>count)
{
printf("\nInvalid position!");
return;
}

while(i<pos)
{
temp = temp->next;
i++;
}
printf("\nElement at position %d is:%d\n",pos,temp->data);
}

2) Write a program to reverse a singly linked list.

#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
static void reverse(struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node *head)
{
struct Node *temp = head;
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
int main()
{
struct Node* head = NULL;
int n,i,data;
printf("Enter size of list: ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&data);
push(&head,data);
}
printf("Given linked list\n");
printList(head);
reverse(&head);
printf("\nReversed Linked list \n");
printList(head);
getchar();
}
3) Write a program to implement Doubly Linked List.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
struct Node* prev;
}*head = NULL;
void push(struct Node** head, int new_data)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
if(*head==NULL)
{
new_node->prev = NULL;
new_node->next = NULL;
}
if(*head!=NULL)
{
new_node->next =(*head);
(*head)->prev = new_node;
new_node->prev = NULL;
}
*head = new_node;
}

void printList(struct Node *head)


{
struct Node *temp = head;
if(temp == NULL)
{
printf("Empty list");
return;
}
else
{
while(temp !=NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
printf("\n");
}

void main()
{
int n,i,data;
printf("Enter size of list: ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&data);
push(&head,data);
}
printList(head);
}
4) Write a program to merge two singly linked list in sorted order.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
void push(struct Node** head, int new_data)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
if(*head==NULL)
{
new_node->next = NULL;
}
if(*head!=NULL)
{
new_node->next =(*head);
}
*head = new_node;
}
void SortedInsert(struct Node** head, struct Node* newNode)
{
struct Node dummy;
struct Node* current = &dummy;
dummy.next = *head;
while (current->next != NULL && current->next->data < newNode->data)
current = current->next;
newNode->next = current->next;
current->next = newNode;
*head = dummy.next;
}

Node* Sort(struct Node** head)


{
struct Node* result = NULL;
struct Node* current = *head;
struct Node* next;
while (current != NULL)
{
next = current->next;
SortedInsert(&result, current);
current = next;
}
return(result);
}

Node* sortedMerge(struct Node* a, struct Node* b)


{
Node* result = NULL;

if (a == NULL)
return (b);
else if (b == NULL)
return (a);

if (a->data <= b->data) {


result = a;
result->next = sortedMerge(a->next, b);
}
else {
result = b;
result->next = sortedMerge(a, b->next);
}
return (result);
}

void printList(struct Node *head)


{
struct Node *temp = head;
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}

int main()
{
struct Node* head1=NULL;
struct Node* head2=NULL;
struct Node* head3=NULL;
int n,m,i,data;
printf("Enter size of list1: ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&data);
push(&head1,data);
}

printf("Enter size of list2: ");


scanf("%d",&m);
printf("Enter elements: ");
for(i=0;i<m;i++)
{
scanf("%d",&data);
push(&head2,data);
}
printf("list1:");
printList(head1);
head1=Sort(&head1);
printf("\nSorted list1:");
printList(head1);
printf("\nlist2\n");
printList(head2);
head2=Sort(&head2);
printf("\nSorted list2:");
printList(head2);
head3 = sortedMerge(head1, head2);
printf("\nsorted merged list:");
printList(head3);

return 0;
}
ASSIGNMENT 03

1) Hands on the linux commands.

man – used to display the user manual of any command that we can run on the terminal.
e.g. $ man grep

mkdir - mkdir command in Linux allows the user to create directories


e.g. mkdir file

cd –cd command in linux known as change directory command. It is used to change current
working directory.
e.g. $ cd [directory_name]

pwd –pwd stands for Present/Print Working Directory. It prints the path of the working
directory, starting from the root.

passwd –passwd command in Linux is used to change the user account passwords

mv – mv stands for move. mv is used to move one or more files or directories from one place
to another in file system. It has two distinct functions:
(i) It rename a file or folder.
(ii) It moves group of files to different directory.
e.g. mv source destination

ls –ls is a Linux shell command that lists directory contents of files and directories

rm –rm command is used to delete or remove files and directory.

cat- It reads data from file and give their content as output.It helps us to
create,view,concatenate files.
e.g. $cat filename

date – date command is used to display the system date and time

cal – cal command is a calendar command in Linux which is used to see the calendar of a
specific month or a whole year.
cal [ [ month ] year]

echo – echo command in linux is used to display line of text/string that are passed as an
argument .
e.g. echo -e "Hello World"
alias – alias command instructs the shell to replace one string with another string while
executing the commands.
cut –The cut command in UNIX is a command for cutting out the sections from each line of
files and writing the result to standard output.

expr – The expr command in Unix evaluates a given expression and displays its
corresponding output

grep – The grep filter searches a file for a particular pattern of characters, and
displays all lines that contain that pattern.

chmod – In Linux-like operating systems, the chmod command is used to change the access
mode of a file.

fg – fg command in linux used to put a background job in foreground.

ps – ps command is used to list the currently running processes and their PIDs along with
some other information depends on different options.

sort – SORT command is used to sort a file, arranging the records in a particular order. By
default, the sort command sorts file assuming the contents are ASCII

ssh – ssh stands for “Secure Shell”. It is a protocol used to securely connect to a remote
server/system. ssh is secure in the sense that it transfers the data in encrypted form between
the host and the client

which – which command in Linux is a command which is used to locate the executable file
associated with the given command by searching it in the path environment variable.

who – who command is used to find out the following information :


1. Time of last system boot
2. Current run level of the system
3. List of logged in users and more.

cp - cp stands for copy. This command is used to copy files or group of files or directory. It
creates an exact image of a file on a disk with different file name. cp command require at least
two filenames in its arguments.

2) What is Linux Kernel? Hands on the Linux kernel versions and Linux flavors.

The Linux kernel is an operating system (OS) kernel defined as Unix-like in nature. It used in
different operating systems, mostly in the form of different Linux distributions.
The Linux kernel was the first truly complete and prominent example of free and open-
source software that prompted its wide adoption and received contributions from
thousands of developers.
Linux distributions:.

 Ubuntu
 Fedora
 Linux Mint
 openSUSE
 PCLinuxOS
 Debian
 Mandriva
 Sabayon/Gentoo

3) Hands on Windows Versions.

 MS-DOS
 Windows 1.0 - 2.0
 Windows 3.0 – 3.1
 Windows 95
 Windows 98
 Windows ME - Millennium Edition
 Windows NT 31. - 4.0
 Windows 2000
 Windows XP
 Windows Vista
 Windows 7
 Windows 8
 Windows 10
 Windows Server
 Windows Home Server
 Windows CE
 Windows Mobile
 Windows Phone 7-10

4) What is Shell Scripting and what are the different variables present in Linux shell
script.

Usually shells are interactive that mean, they accept command as input from users and
execute them. However some time we want to execute a bunch of commands routinely, so
we have type in all commands each time in terminal.
As shell can also take commands as input from file we can write these commands in a file and
can execute them in shell to avoid this repetitive work. These files are called Shell
Scripts or Shell Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell
script is saved with .sh file extension eg. myscript.sh
A shell script have syntax just like any other programming language. If you have any prior
experience with any programming language like Python, C/C++ etc. it would be very easy to
get started with it.
A shell script comprises following elements –
 Shell Keywords – if, else, break etc.
 Shell commands – cd, ls, echo, pwd, touch etc.
 Functions
 Control flow – if..then..else, case and shell loops etc

2) Write a shell sript to print first 10 numbers of fibbonacci series.

echo "enter a number"

echo -n "a = "

read num

a=0

b=1

i=1

while [ $i -le $num ]

do

echo -n " $a,"

c=`expr $a + $b`

a="$b"

b="$c"

i=`expr $i + 1`

done

3) Compare numbers in linux shell script using if-else.


echo "enter number for comparison"

echo -n "a = "

read a

echo -n "b = "


read b

if [ $a -gt $b ]

then

echo "$a is greater then $b "

elif [ $a -lt $b ]

then echo "$a is less then $b"

else

echo "$a and $b are equle"

fi

8) Check a given number is even or odd.

echo "enter number for even odd"


echo -n "a = "
read a

if [ `expr $a % 2` -eq 0 ]
then
echo "$a is even"
else
echo "$a is odd"
fi

9) Check a given number is prime or not.

echo "enter number for check prime or not"


echo -n "a = "
read a
flag=0
half=`expr $a / 2`
for (( i = 2; i<=$half; i++ ))
do
if [ `expr $a % $i` -eq 0 ]
then
flag=1
echo "$a is not a prime number"
break
fi
done

if [ $flag == 0 ]
then
echo "$a is a prime number"
fi

10) Find the greatest among 5 numbers using if-else.

echo -n "Enter a = "


read a
echo -n "Enter b = "
read b
echo -n "Enter c = "
read c
echo -n "Enter d = "
read d
echo -n "Enter e = "
read e

max=$a

if [ $b -gt $max ]
then
max=$b
fi

if [ $c -gt $max ]
then
max=$c
fi

if [ $d -gt $max ]
then
max=$d
fi
if [ $e -gt $max ]
then
max=$e
fi

echo "Greates Number is $max"

12) Print a table using loops.

echo "enter a number"


echo -n "num = "
read num

for ((i=1;i<=10;i++))
do
echo "($num,$i) = `expr $num \* $i`"
done

13) Print a pattern using for loop.

n=5
i=1

while [ $i -le $n ]
do
echo
j="$i"
k=1
while [ $k -le $n ]
do
echo -n "$j"
k=`expr $k + 1`
done

echo
i=`expr $i + 1`
done

14) Write a shell script to print message.


echo " This is first OS Lab"
DATE=`date +%d/%m/%Y`
echo "Today's Date is $DATE"
Time=`date +%H:%M:%S`
echo "Now the Time is $Time"

Potrebbero piacerti anche