tutorial & laboratory

37
PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 1 Tutorial & Laboratory Programming & Data Structure: CS11001/19001 Section - 1/A DO NOT POWER ON THE MACHINE Department of Computer Science and Engineering I.I.T. Kharagpur Spring Semester: 2009 - 2010 (24.03.2010)

Upload: others

Post on 12-Apr-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 1'

&

$

%

Tutorial & Laboratory

�� ��Programming & Data Structure: CS11001/19001

��

��Section - 1/A

DO NOT POWER ON THE MACHINE

Department of Computer Science and Engineering

I.I.T. Kharagpur

Spring Semester: 2009 - 2010 (24.03.2010)

Page 2: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 2'

&

$

%

Download

Download the file date240310.ps from

Programming & Data Structures ... of

http://www.facweb.iitkgp.ernet.in/∼goutam

View the file using the command kghostview & orggv &

Page 3: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 3'

&

$

%

Laboratory Test II: 7th April, 2010

• Lab. Test I + 1-D array, recursive function,

string, 2-D array, basic structure.

• Do not carry any book or note book.

• Time: Two hours.

• Marks: 35�� ��The test will start after the tutorial.

Page 4: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 4'

&

$

%

Quick Sort Algorithm (ascending order)

Let there there be n data elements stored in an

1-D array, a[l · · · h].

1. If l = h(n = 1), it is already sorted!

2. If n > 1,

(a) Partition the data at index m in the array

in such a way that each data in the part

a[l · · · m] is less than or equal to each

data in the part a[m+1 · · · h].

Page 5: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 5'

&

$

%

(b) Sort a[l · · · m] and a[m+1 · · · h]

recursively.

After the partition there will be no datamovement from one part to the other.

Page 6: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 6'

&

$

%

Partitioning the Data

1. Select a data element p as the pivot element.

For a simple implementation we may choose

a[l] as the pivot.

2. Transfer all the elements that are less than

p, to the lower index side of the array and

also transfer elements greater than or equal

to p, to the higher index side.

Page 7: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 7'

&

$

%

Partition Algorithm (non-descending order)

partition(a, l, h)

p ← a[l]

i ← l − 1

j ← h + 1

while true

do j ← j − 1 while a[j] > p

do i ← i + 1 while a[j] < p

if i < j exchange(a[i], a[j])

else return j

Page 8: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 8'

&

$

%

Quick Sort Algorithm

quickSort(a, l, h)

if (l < h)

p ← partition(a, l, h)

quicksort(a, l, p)

quicksort(a, p+1, h)

Page 9: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 9'

&

$

%

Quick Sort : An Example

Page 10: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 10'

&

$

%

# #

0 1 2 3 4 5 6 7

# #

# #

0 1 2 3 4 5 6 7

# #

# #

0 1 2 3 4 5 6 7

# #

Call 0

x

p

x

x

21 3 9 11 5 9

3 9 11 5 9

9 11 5

Unsorted Array

1339

21

9

39 13

21

i=−18

j=8

j=7

j=7

3

x[i] <−−> x[j]

3913 21

i=0

Page 11: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 11'

&

$

%

# #

0 1 2 3 4 5 6 7

# #

x

11 5

p93 9

j=7

2113 39 21

i=0

# #

0 1 2 3 4 5 6 7

# #

x

5 913 3 21939 11

i=6

# #

0 1 2 3 4 5 6 7

# #

x x[i] <−−> x[j]

13 3 9 11 5 21

i=6i=2

9 39

Page 12: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 12'

&

$

%

# #

0 1 2 3 4 5 6 7

# #

x

p2113 2151193

j=6j=2

# #

0 1 2 3 4 5 6 7

# #

x

p2113 2151193

9 39

9 39

j=5

# #

0 1 2 3 4 6 7

# #

x

39

5

partition index

3 9 115913 21

No data crossing

i=6

Page 13: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 13'

&

$

%

Dynamic Allocation of 1-D Array

Page 14: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 14'

&

$

%

Request for Run-Time Memory Allocation

• The volume of data may not be known

before the run-time.

• It provides flexibility in building data

structures.

• The space is allocated in global data area

(not on the stack) called heap.

Page 15: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 15'

&

$

%

void *malloc(size t n)

The C library function malloc() is used torequest for an allocation of n bytes ofcontiguous memory space in the global dataarea. After a successful allocation the functionreturns a generic pointer void * that can becasted to any required type.

Page 16: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 16'

&

$

%

void *malloc(size t n)

If malloc() fails to allocate the requestedspace, it returns a NULL pointer. The interfaceof the function is defined in the header filestdlib.h, so it is to be included.

Page 17: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 17'

&

$

%

1-D Array of n Elements

We can use a variable of type int * and a callto malloc() to create an 1-D array of nelements of type int, where n is an input data.

Page 18: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 18'

&

$

%

Dynamic 1-D Array

#include <stdio.h>

#include <stdlib.h>

int main()

{

int *p, n, i, val = 1 ;

printf("Enter a +ve integer: ");

scanf("%d", &n);

p = (int *)malloc(n*sizeof(int));

for(i=0; i<n; ++i) {

p[i] = val; val = 2*val;

}

Page 19: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 19'

&

$

%

for(i=0; i<n; ++i) printf("%d ",p[i]);

putchar(’\n’);

return 0;

} // dynamic1D.c

Page 20: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 20'

&

$

%

Student’s Data

Let us consider data related to every student ofa college (say IIT). There may be a largenumber of data items, but for simplicity weonly consider the name, roll number, sgpa andgrade points in different semesters, and thecgpa upto the last semester. The correspondingproduct data type or the structure in C maylook like the following one.

Page 21: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 21'

&

$

%

Student Data

#define NAME 50#define ROLL 9struct sgpa {

float sgpa;int gp;

};typedef struct {

char name[NAME], roll[ROLL] ;int sem ;struct sgpa sgpa[10];float cgpa ;

} student;

Page 22: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 22'

&

$

%

Assignment XII

The student data is available in a data file(stdData). Write a C program to readstudent-data in a dynamically created array oftype student, from the stdin (keyboard). Thedata will be redirected from the data file.

Page 23: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 23'

&

$

%

student *sP

sp = (student *)malloc(n*sizeof(student));

sP 012

n−1

semname[50]

roll[9] sgpa[10]

cgpa

Page 24: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 24'

&

$

%

Assignment XII

The program computes the cgpa upto the lastsemester for each student and stores it in thecorresponding cgpa field. It sorts the studentrecords (indirectly) to prepare a merit list(non-ascending order on the cgpa) using thequick-sort algorithm. Finally it prints thesorted list of students along with their rollnumbers and cgpas.

Page 25: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 25'

&

$

%

Input Data: stdData

70

08CE1012 Sukesh Jain 3 20 8.5 25 8.0

08CS1020 Ansuman Roy 3 22 8.0 23 7.5

08PH1010 Ruma Ahamed 3 25 9.0 20 8.5

.................

08EE1007 Simranjit S Mann 3 23 8.5 22 8.5

08EC1023 P V Verma 3 20 8.0 25 9.0

08CH1016 P V Tiwari 3 20 8.0 25 9.0

Page 26: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 26'

&

$

%

Different Functions

The functionstudent *readStudent(int *nsP);reads the number of students and updates thelocation *nsP (in the caller), creates an array oftype student of size n; reads in each element ofthe array, the roll number, name, currentsemester number and (credit point, sgpa) pairsupto the last semester. Finally it returns thestarting address of the array.

Page 27: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 27'

&

$

%

Reading Roll No. & Name

�� ��scanf("%s %[∧0-9]%d", ...

Page 28: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 28'

&

$

%

Different Functions

The functionvoid calcCgpa(student *sP, int n);takes the starting address of the student arrayand the number of students as parameters andcomputes the cgpa of each student and stores itin the cgpa field of each record in the array.

Page 29: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 29'

&

$

%

cgpa Calculation

If the number of the current semester is sem

then

cgpa =

sem−2∑

i=0

gpi × sgpai

sem−2∑

i=0

gpi

.

Page 30: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 30'

&

$

%

Different Functions

The functionint *initIndex(int n);takes the number of students n as parameter,dynamically creates an n-element array of typeint, initializes the ith location of the array to i,the initial index of the ith student in thestudent array.

Page 31: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 31'

&

$

%

1

19

1

19

sP

indP0

0

19

10

08CE1012 Sukesh Jain ******08CS1020 Ansuman Roy *****

08SI2024 Sagar Jyoti *******

Page 32: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 32'

&

$

%

Different Functions

The functionvoid writeStudent(student sP[], intindP[], int n);takes the starting address of the student array,the starting address of the index array and thenumber of students as parameters. It prints thestudents array following the sequence of theindex array i.e. the index of the student record(in the array) printed in the ith line, is stored inindP[i].

Page 33: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 33'

&

$

%

Different Functions

The functionvoid quickSort(student sP[], intindP[], int low, int high);takes four parameters. The last two parametersare the low and the high indices of indP[]within which data in sP[] are to be sortedindirectly in non-ascending order.

Page 34: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 34'

&

$

%

Unsorted Data

1 1

sP

indP0

01

008CS1020 Ansuman Roy

2345

76

234567

234567

7.4808CH3017 Manoj Meena 6.82

08NA1002 Karan Rao 7.828.3608CH1016 N Prasad

8.1408PH1010 Rusha Ahamed6.4708SI2024 Sargar Jyoti

08GG2010 Anant Raju

08CE1012 Sukesh Jain

7.82

8.41

Page 35: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 35'

&

$

%

Sorted Data

1

sP

indP0

01 08CS1020 Ansuman Roy

2345

76

234567

7.4808CH3017 Manoj Meena 6.82

08NA1002 Karan Rao 7.828.3608CH1016 N Prasad

8.1408PH1010 Rusha Ahamed6.4708SI2024 Sargar Jyoti

08CE1012 Sukesh Jain 8.41

7.8208GG2010 Anant Raju

37506124 Student Array Unchanged

Sorted Sequence indicesin the Index Array

Page 36: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 36'

&

$

%

Different Functions

The functionstatic int partition(student sP[], intindP[], int low, int high);the index array indP[] is partitioned on thecgpa values of the sP[] array (in non-ascendingorder). But the order of data in sP[] areunchanged. The key-word static indicatesthat the function is local to the file.

Page 37: Tutorial & Laboratory

PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 37'

&

$

%

Input Data and Report

Test your program with the data in the filestdData. After you are satisfied, send the .cfile.$ ./a.out < stdData