You are on page 1of 3

Programming and Data Structure Laboratory (CS19001)

Spring 2022-23

Assignment for Week 9 (May 22, 2023)

Total Marks: 100 Duration: 3 hours

INSTRUCTIONS
a) All programs should be written in C.
b) The file containing your solution to problem ‘x’ should be named roll-week9-probx.c where ‘roll’ is your roll
number.
c) Use indentation and comments as necessary. You are allowed to consult your books, notes or manual pages.
d) Upload the files separately on Moodle.

PROBLEMS

1. Write the following functions in C. [10+10+15=35]


a) A function bubble_sort to sort n number of integers stored in an array A in non-decreasing
(increasing) order. Use the function prototype as follows:
void bubble_sort (int A[], int n, int *time);

b) A function quick_sort to sort n number of integers stored in an array A in non-decreasing


(increasing) order. Use the function prototype as follows:
void quick_sort (int A[], int n, int *time);

c) Write a main function that reads a number N, dynamically allocates an integer array X with N
elements, fills up the array with randomly generated integer values, and then sort the list using both
bubble sort and quick sort. Run the program for values of N = 100, 1000, and 10000.

Generate the output in the following format:


Number of elements = 100
Time taken using bubble sort = ***
Time taken using quick sort = ***
----------------------------------------
Number of elements = 1000
Time taken using bubble sort = ***
Time taken using quick sort = ***
----------------------------------------
Number of elements = 10000
Time taken using bubble sort = ***
Time taken using quick sort = ***
----------------------------------------
How to calculate running time of a program?
#include <time.h> // Include this header file

clock_t tstart, tend; // Define these variables


double runTime; // Variable to store the run time
tstart = clock(); // Clock timer starts

// ****** PROGRAM CODE HERE ********

tend = clock(); // Clock timer ends

runTime = (tend - tstart) / (double) CLOCKS_PER_SEC;


// Calculate run time in seconds
How to generate a random number?
By calling the function rand(), which returns an integer.

2. Define a structure with name STUD_REC to maintain records about students in a class:
typedef struct {
int rollno; // Assume that the roll number is an integer number
char name[40];
float cgpa;
} STUD_REC;

Write the following functions in C.


a) A function bin_search to search an array stud of size n for the presence of a student with a given
roll number roll_key. Use the following function prototype:
STUD_REC bin_search (STUD_REC stud[], int n, int roll_key)

b) A function sel_sort to sort an array stud of size n in non-decreasing (ascending) order of rollno. Use
the following function prototype:
void sel_sort (STUD_REC stud[], int n)

c) Write a main function that reads a number N, allocates space for an array of N elements of type
STUD_REC, sorts the elements using the function sel_sort (selection sort), and then searches for a
student with a given roll number in the sorted list using the function bin_search.

The outputs must be generated in suitably formatted form. [13+14+8=35]

3. Define a structure with name _POINT to maintain information about the co-ordinates of a point in a two-
dimensional plane:
typedef struct {
float x_coord;
float y_coord;
} _POINT;

Also define a structure with name _POLYGON to represent a polygon with n vertices (n ≤ 50), where
information about the coordinates of all the vertices are maintained:
typedef struct {
int n;
_POINT vert[50];
} _POLYGON;

Write the following functions in C. [10+15+5=30]


a) A function find_peri to compute and return the perimeter of a given polygon p. Use the
following function prototype:
float find_peri (_POLYGON p);

b) A function find_area to compute and return the area of a given polygon p. Use the following
function prototype:
float find_area (_POLYGON p);

c) Write a main function that reads a polygon and prints its perimeter and area using the above
functions. Generate the output in suitable formatted form.

You might also like