Autonomous Vehicles
Autonomous Vehicles
Autonomous Vehicles
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]);
}
#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
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);
}
}
while(i<pos)
{
temp = temp->next;
i++;
}
printf("\nElement at position %d is:%d\n",pos,temp->data);
}
#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 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;
}
if (a == NULL)
return (b);
else if (b == NULL)
return (a);
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);
}
return 0;
}
ASSIGNMENT 03
man – used to display the user manual of any command that we can run on the terminal.
e.g. $ man grep
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
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.
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.
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
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
read num
a=0
b=1
i=1
do
c=`expr $a + $b`
a="$b"
b="$c"
i=`expr $i + 1`
done
read a
if [ $a -gt $b ]
then
elif [ $a -lt $b ]
else
fi
if [ `expr $a % 2` -eq 0 ]
then
echo "$a is even"
else
echo "$a is odd"
fi
if [ $flag == 0 ]
then
echo "$a is a prime number"
fi
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
for ((i=1;i<=10;i++))
do
echo "($num,$i) = `expr $num \* $i`"
done
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