DS Lab Programs
DS Lab Programs
DS Lab Programs
WEEK-2
WEEK 3:
PROGRAM
#include <stdio.h>
typedef struct complex
{
float real;
float imag;
} complex;
complex add(complex n1, complex n2);
int main()
{
complex n1, n2, result;
printf("For 1st complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n1.real, &n1.imag);
printf("\nFor 2nd complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n2.real, &n2.imag);
result = add(n1, n2);
printf("Sum = %.1f + %.1fi", result.real, result.imag);
return 0;
}
complex add(complex n1, complex n2)
{
complex temp;
temp.real = n1.real + n2.real;
temp.imag = n1.imag + n2.imag;
return (temp);
}
Output
For 1st complex number
Enter the real and imaginary parts: 2.1
-2.3
For 2nd complex number
Enter the real and imaginary parts: 5.6
23.2
Sum = 7.7 + 20.9i
Sample Viva Questions.
1.Write a Structure of a book (Name, author, book no, no of pages and price)?
typedef struct
{
char name[20], author[20];
int bookno,pages;
float price;
}BOOK;
PROGRAM
#include <stdio.h>
typedef struct complex
{
float real;
float imag;
} complex;
complex mul(complex n1, complex n2);
int main()
{
complex n1, n2, result;
printf("For 1st complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n1.real, &n1.imag);
printf("\nFor 2nd complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n2.real, &n2.imag);
result = mul(n1, n2);
printf("Product = %.1f + %.1fi", result.real, result.imag);
return 0;
}
complex mul(complex n1, complex n2)
{
complex temp;
temp.real = a.real*b.real - a.img*b.img;
temp.img = a.img*b.real + a.real*b.img;
return (temp);
}
Output
For 1st complex number
Enter the real and imaginary parts: 2
3
For 2nd complex number
Enter the real and imaginary parts: 4
5
Product = -7 + 22i
#include<stdio.h>
#include<string.h>
#define MAX 2
struct student
{
char name[20];
int roll_no;
float marks;
};
int main()
{
struct student arr_student[MAX];
int i;
for(i = 0; i < MAX; i++ )
{
printf("\nEnter details of student %d\n\n", i+1);
printf("Enter name: ");
scanf("%s", arr_student[i].name);
printf("Enter roll no: ");
scanf("%d", &arr_student[i].roll_no);
printf("Enter marks: ");
scanf("%f", &arr_student[i].marks);
}
printf("\n");
printf("Name\tRoll no\tMarks\n");
for(i = 0; i < MAX; i++ )
{
printf("%s\t%d\t%.2f\n", arr_student[i].name, arr_student[i].roll_no, arr_student[i].marks);
}
return 0;
}
Expected Output:
Enter details of student 1
Enter name: Jim
Enter roll no: 1
Enter marks: 44
Enter details of student 2
Enter name: Tim
Enter roll no: 2
Enter marks: 76
Name Roll no Marks
Jim 1 44.00
Tim 2 76.00
Sample Viva Questions.
2. What are the different ways of accessing the structure members using structure variable?
PROGRAM
#include<stdio.h>
struct time{
unsigned int hours: 5; // Size restricted to 5 bits
unsigned int minutes:6; // Size restricted to 6 bits
unsigned int seconds:6; // Size restricted to 6 bits
};
int main()
{
struct time t = {11, 30, 10}; // Here t is an object of the structure time
printf("Welcome to Data Structures!\n\n");
printf("The time is %d : %d : %d\n", t.hours, t.minutes, t.seconds);
printf("The size of time is %ld bytes.\n", sizeof(struct time));
return 0;
}
Output:
Welcome to Data Structures!
2. union add
{
int a:2;
char b:9;
}a1;
main()
{ printf(“%d”, sizeof(a1));
}
What is the output of the above code?
WEEK 5:
1. Write a C Program to store the information (name, roll no, and branch) of a student
using unions.
PROGRAM
#include <stdio.h>
typedef union
{
struct student
{
char name[50];
int roll;
float marks;
} s;
}u;
int main() {
printf("Enter information:\n");
printf("Enter name: ");
scanf("%s",u. s.name);
printf("Enter roll number: ");
scanf("%d", &u.s.roll);
printf("Enter marks: ");
scanf("%f", &u.s.marks);
printf("Displaying Information:\n");
printf("Name: ");
printf("%s",u. s.name);
printf("Roll number: %d\n", u.s.roll);
printf("Marks: %.1f\n",u. s.marks);
return 0;
}
Output
Enter information:
Enter name: XYZ
Enter roll number: 23
Enter marks: 34.5
Displaying Information:
Name: XYZ
Roll number: 23
Marks: 34.5
PROGRAM
#include <stdio.h>
typedef struct Complex
{
float real;
float imag;
} complex;
void addNumbers(complex c1, complex c2, complex *result);
int main()
{
complex c1, c2, result;
printf("For first number,\n");
printf("Enter real part: ");
scanf("%f", &c1.real);
printf("Enter imaginary part: ");
scanf("%f", &c1.imag);
printf("For second number, \n");
printf("Enter real part: ");
scanf("%f", &c2.real);
printf("Enter imaginary part: ");
scanf("%f", &c2.imag);
addNumbers(c1, c2, &result);
printf("\nresult.real = %.1f\n", result.real);
printf("result.imag = %.1f", result.imag);
return 0;
}
void addNumbers(complex c1, complex c2, complex *result)
{
result->real = c1.real + c2.real;
result->imag = c1.imag + c2.imag;
}
Output
For first number,
Enter real part: 1.1
Enter imaginary part: -2.4
For second number,
Enter real part: 3.4
Enter imaginary part: -3.2
result.real = 4.5
result.imag = -5.6
1. Write a c program to implement singly linked list for the following operations.
a) Insertion b)Deletion c)Search
PROGRAM
#include<stdio.h>
#include<stdlib.h>
struct linked_list
{
int number;
struct linked_list *next;
};
typedef struct linked_list node;
node *head=NULL, *last=NULL;
void create_linked_list();
void print_linked_list();
void insert_at_last(int value);
void insert_at_first(int value);
void insert_after(int key, int value);
void delete_item(int value);
void search_item(int value);
int main()
{
int key, value;
//Create a linked list
printf("Create Linked List\n");
create_linked_list();
print_linked_list();
//Insert value at last position to existing Linked List
printf("\nInsert new item at last\n");
scanf("%d", &value);
insert_at_last(value);
print_linked_list();
//Insert value at first position to existing Linked List
printf("\nInsert new item at first\n");
scanf("%d", &value);
insert_at_first(value);
print_linked_list();
//Insert value after a defined value to existing Linked List
printf("\nEnter a KEY (existing item of List), after that you want to insert a value\n");
scanf("%d", &key);
printf("\nInsert new item after %d KEY\n", key);
scanf("%d", &value);
insert_after(key, value);
print_linked_list();
//Search an item from Linked List
printf("\nEnter an item to search it from List\n");
scanf("%d", &value);
search_item(value);
//Delete value from List
printf("\nEnter a value, which you want to delete from list\n");
scanf("%d", &value);
delete_item(value);
print_linked_list();
return 0;
}
void create_linked_list()
{
int val;
while(1)
{
printf("Input a number. (Enter -1 to exit)\n");
scanf("%d", &val);
if(val==-1)
break;
insert_at_last(val);
}
}
void insert_at_last(int value)
{
node *temp_node;
temp_node = (node *) malloc(sizeof(node));
temp_node->number=value;
temp_node->next=NULL;
//For the 1st element
if(head==NULL)
{
head=temp_node;
last=temp_node;
}
else
{
last->next=temp_node;
last=temp_node;
}
}
PROGRAM
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, t;
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]);
for (c = 0; c < (n - 1); c++) // finding minimum element (n-1) times
{
position = c;
for (d = c + 1; d < n; d++)
{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
t = array[c];
array[c] = array[position];
array[position] = t;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
Output:
Enter number of elements
5
Enter 5integers
46381
Sorted list in ascending order:
13468
Sample Viva Questions.
2. An array contains the following elements 65, 25 ,12 ,22, 11. What is the output at 3rd pass
using selection sort.
11,12,22,25,65
2. Write a C program to sort the elements using Quick sort.
PROGRAM
#include<stdio.h>
void quicksort(int [10],int,int);
int main(){
int x[20],size,i;
printf("\nEnter size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("\nSorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
getch();
return 0;
}
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;
if(first<last){
pivot=first; i=first; j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i]; x[i]=x[j]; x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
Output:
Enter number of elements
5
Enter the integers
46381
Te sorted array is
13468
2. No of passes required for sorting the following elements using bubble sort. 5, 1, 4 ,28, 9, 76
5
WEEK 8:
#include<stdio.h>
void main( )
{
int
a[10],i,j,k,n;
printf("How many elements you want to sort?\n");
scanf("%d",&n);
printf("\nEnter the Elements into an array:\n");
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
k=a[i];
for(j= i-1; j>=0 && k<a[j]; j--)
a[j+1]=a[j];
a[j+1]=k;
}
printf("\n\n Elements after sorting: \n");
for(i=0;i<n;i++)
printf("%d\n", a[i]);
OUTPUT:
How many elements you want to sort ? :
6
2. If the list is already sorted, what is the time complexity of insertion sort?
O(n)
16
2. Write a C program to search an element in a list of elements using linear search. If the element
found display the position, otherwise print “element not present”
PROGRAM
#include<stdio.h>
int linear(int [ ],int,int);
void main( )
{
int a[20], pos = -1, n, k, i;
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter elements for an array:");
for(i=0; i<n ;i++)
scanf("%d",&a[i]);
printf("\nEnter the element to be searched:");
scanf("%d",&k);
pos=linear(a,n,k);
if(pos != -1)
printf("\n Search successful element found at position %d",pos);
Else
printf("\n Search unsuccessful, element not found");
getchar( );
}
int linear(int a[ ],int n,int k)
{
int i;
for(i=0;i<n;i++)
{
f(a[i]==k)
return(i);
}
return -1;
}
Output:-
Enter the n value : 5
Enter elements for an array : 11 2 23 14 55
Enter the element to be searched : 14
Search successful element found at position : 3
17
Sample Viva Questions.
1. What is the best and worst case time complexity of Linear Search Algorithm?
O(1) and O(n)
WEEK 9:
1. Write a C program to search an element in a list of elements using Binary search. If
the element found display the position, otherwise print “element not present”.
PROGRAM
#include<stdio.h>
int bsearch(int [ ],int,int);
void main( )
{
int a[20],pos,n,k,i;
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter elements for an array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the key value:");
scanf("%d",&k);
pos=bsearch(a,n,k);
if(pos!= -1)
printf("Search successful, element found at position %d",pos);
else
printf("Search unsuccessful, element not found");
}
int bsearch(int a[ ],int n, int k)
{
int lb=0,ub,mid;
ub=n-1;
while(ub>=l)
{
mid=(lb+ub)/2;
i(k<a[mid])
ub=mid-1;
18
else if(k>a[mid])
lb=mid+1;
else if(k==a[mid])
return(mid);
}
return -1;
}
OUTPUT
19
WEEK 10:
1. Write a C program convert infix to postfix notation and postfix evaluation using stack
PROGRAM
#include<stdio.h>
char stack[20];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
}
main()
{
char exp[20];
char *e, x;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c", x);
}
20
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c",pop());
}
}
OUTPUT:
Enter the expression :: a+b*c
abc*+
21
WEEK 11:
1. Write a C program implement Queue using arrays for the following operations.
i) Enqueue ii) Dequeue iii) Peek iv) Display
PROGRAM
#include<stdio.h>
#include<conio.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
//clrscr();
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
22
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
getch();
}
OUTPUT:
Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1
Enter no 1:10
Enter the Choice:1
Enter no 2:54
Enter the Choice:1
Enter no 3:98
Enter the Choice:1
Enter no 4:234
Enter the Choice:3
Queue Elements are:
10
54
98
234
23
Sample Viva Questions.
Stack follows the principle of LIFO( last in first out) and queue follows the principle of FIFO
(first in first out).
Front=0, rear=n-1
WEEK 12:
1. Write a C program open a new file and implement the following I/O functions
a. fprintf(), fscanf()
b. getw(), putw()
c. getc(), putc()
A. fprintf(), fscanf()
PROGRAM
#include <stdio.h>
int main()
{ int i, sum2=0;
FILE *f2;
f2 = fopen("data3.txt","w");
for(i=1;i<5;i++) /* write integers to files in binary and text format*/
fprintf(f2,"%d\n",i);
fclose(f2);
f2 = fopen("data3.txt","r");
while(fscanf(f2,"%d",&i)!=EOF)
{
sum2+=i;
printf("text file: i=%d\n",i);
}
fprintf(stdout,"text sum=%d\n",sum2);
fclose(f2);
}
Output:
Text file: i=1
Text file: i=2
Text file: i=3
Text file: i=4
Text sum=10
24
Sample Viva Questions.
Objective:
Student will be able to understand the concept of Files
Outcome:
Student gains the ability to learn how to manipulate data in a file using file I/O as per
the requirements.
PROGRAM
#include <stdio.h>
main()
{
int i,sum1=0;
FILE *f1;
f1 = fopen("data1.txt","w");
for(i=10;i<5;i++)
{
putw(i,f1);
}
fclose(f1);
f1 = fopen("data1.txt","r");
while((i=getw(f1))!=EOF)
{
sum1+=i;
printf("binary file: i=%d\n",i);
}
printf("binary sum=%d",sum1);
fclose(f1);
}
Output:
Binary file: i=1
Binary file: i=2
Binary file: i=3
Binary file: i=4
Binary sum=10
25
Sample Viva Questions.
1. What is the difference between scanf( ) and getw( ) functions and why are they used?
scanf is used to read any type of input from the keyboard whereas getw reads only
integers.
2. What is the difference between printf( ) and putw( ) functions and why are they used?
printf is used to print any type of data to the monitor whereas putw displays only
integers.
PROGRAM
#include <stdio.h>
int main()
{
char ch;
FILE *fp;
if (fp = fopen("test.c", "r"))
{
ch = getc(fp);
while (ch != EOF)
{
putc(ch, stdout);
ch = getc(fp);
}
fclose(fp);
return 0;
}}
PROGRAM
26
#include<stdio.h>
int main()
{
char ch;
FILE *fp,*fp1;
fp1=fopen("data.txt","w");
if(fp=fopen("file1.c","r"))
{
ch = getc(fp);
while(ch != EOF)
{
putc(ch,fp1);
ch = getc(fp);
}
fclose(fp);
fclose(fp1);
printf("Data copied successfully \n");
return 0;
}
}
Output: Data copied successfully
PROGRAM
#include<stdio.h>
int main(int argc,char *argv[])
{
char ch;
FILE *fp,*fp1;
fp1=fopen(argv[1],"a");
if(fp=fopen(argv[2],"r"))
{
ch = getc(fp);
while(ch != EOF)
{
putc(ch,fp1);
27
ch = getc(fp);
}
fclose(fp);
printf(“Files merged\n”):
return 0;
}
}
2. Why should argc be of type int and argv of type char? Explain.
argc contains the number of arguments passed and argv stores the arguments passed
to the main function.
WEEK 14:
PROGRAM
vi mainprg.c
#include<stdio.h>
#include"arith.h"
int main()
{
int a,b,c;
printf("Enter the values of a and b\n");
scanf("%d%d",&a,&b);
c=add(a,b);
printf("The sum of %d + %d = %d\n",a,b,c);
c=sub(a,b);
printf("The difference of %d - %d = %d\n",a,b,c);
c=mul(a,b);
printf("The product of %d * %d = %d\n",a,b,c);
c=div(a,b);
printf("The quotient of %d / %d = %d\n",a,b,c);
printf("END OF MULTIFILE PROGRAMMING\n");
return 0;
}
vi add.c
#include"arith.h"
28
int add(int x,int y)
{
return x+y;
}
vi sub.c
#include"arith.h"
int sub(int x,int y)
{
return x-y;
}
vi mul.c
#include"arith.h"
int mul(int x,int y)
{
return x*y;
}
vi div.c
#include"arith.h"
int div(int x,int y)
{
return x/y;
}
Create object file
[faculty@localhost ~]$ cc -c add.c
[faculty@localhost ~]$ cc -c sub.c
[faculty@localhost ~$ cc -c mul.c
[faculty@localhost ~]$ cc -c div.c
Compiling and linking all files
[faculty@localhost ~]$ cc mainprg.c add.o sub.o mul.o div.o
[faculty@localhost ~]$./a.out
29