tutorial & laboratory
TRANSCRIPT
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)
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 &
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.
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].
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.
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.
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
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)
PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 9'
&
$
%
Quick Sort : An Example
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
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
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
PDS Tut. & Lab.: X (CS 11001/19001): Section 1 Dept. of CS&Engg., IIT Kharagpur 13'
&
$
%
Dynamic Allocation of 1-D Array
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.
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.
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.
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.
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;
}
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
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.
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;
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.
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
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.
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
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.
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", ...
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.
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
.
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.
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 *******
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].
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.
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
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
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.
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