Sunteți pe pagina 1din 36

' $

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

Tutorial & Laboratory

 
 
Programming & Data Structure: CS11001/19001
 


Section - 9/C 

DO NOT POWER ON THE MACHINE

Department of Computer Science and Engineering


I.I.T. Kharagpur
Autumn Semester: 2007 - 2008 (10.08.2007)

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 2

Download

Download the file date100807.ps from


Programming & Data Structures ... of

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

View the file using the command kghostview & or


ggv &

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 3

File Name Convention

• The file name will be CmmAA.c, where ‘C’


is for the section 9, ‘mm’ is the machine
number and ‘AA’ is the assignment number
e.g. C0904.c is the C program file from
section 9, machine number ‘09’, for the 4th
assignment.

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 4

Submission File

• Copy C0904.c to C0904.prn :


$ cp C0904.c C0904.prn
• Let there be five (5) sets of test input.
Collect five sets of output in a file by
executing ./a.out five times in the following
way.
$ ./a.out >> C0904.out

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 5

Submission File (cont.)

• Append the output file to the print file:


$ cat C0904.out >> C0904.prn

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 6

Submission File

• You may see the content of C0904.prn by:


$ cat C0904.prn | less
• If you are satisfied, submit C0904.prn by ftp.
• If you are not satisfied, remove both
C0904.out and C0904.prn,
$ rm C0904.out C0904.prn
Fix the error and repeat the process.
& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 7

 

No need to create .prn if the .c file is asked. 

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 8

Submission by ftp

$ ftp 10.3.19.55
Connected to 10.3.19.55 (10.3.19.55).
220 pclabII102 FTP server (Version wu-2.6.2-5)
ready.
Name (10.3.19.55:.....): pds09
Password: *******
230 User pds07 logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
> cd assignment04
250 CWD command successful.
ftp> put C0904.prn (or C0904.c · · ·)
& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 9

Submission by ftp

ftp> put C0904.prn


local: C0904.prn remote: C0904.prn
227 Entering Passive Mode (...............)
150 Opening BINARY mode data connection
for bat.
226 Transfer complete.
26 bytes sent in ......... secs (....... Kbytes/sec)
ftp> bye
............
$

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 10

Assignment I

Write a C program that reads the coefficients of a pair of


simultaneous equations and finds out whether there is an
unique solution or there are infinitely many solutions or
there is no solution. In case of an unique solution, print
the coordinates of the point of intersection.

a1 x + b 1 y = c 1 , (1)
a2 x + b 2 y = c 2 (2)
Assume that the type of each coefficient is double. Use
format specification %lf in scanf() and %f in printf().

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 11

Input data

Test your program on the following sets of input data.

Equation 1 Equation 2
a1 b1 c1 a2 b2 c2
3.0 4.0 7.0 1.0 −2.0 4.0
4.5 9.1 11.0 9.0 18.2 17.0
4.5 2.75 10.0 9.0 5.50 20.0

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 12

 

Assignment I: Send the .c file by ftp 

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 13

Return value of scanf()

The function scanf() returns the number of


data it has read successfully. It returns EOF if
the end-of-file is encountered while reading
data. The end-of-file for the stdin (the
keyboard) is Ctrl-D.

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 14

Return value of scanf()

#include <stdio.h>
int main(){ // scanf.c
int m, n, p, retV ;

retV = scanf("%d", &n) ;


printf("retV: %d\n", retV) ;
retV = scanf("%d%d", &n, &m) ;
printf("retV: %d\n", retV) ;
retV = scanf("%d%d%d", &n, &m, &p) ;
printf("retV: %d\n", retV) ;
return 0 ;

& %
}
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 15

scanf() to Terminate Loop

#include <stdio.h>
int main(){ // scanf1.c
double n ;

while(scanf("%lf", &n) != EOF)


printf("\tn: %f\n", n);
return 0 ;
}

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 16

Assignment II

Write a C program that reads a sequence of


floating-point numbers from the keyboard (file
stdin) and computes the arithmetic mean,
geometric mean, harmonic mean and standard
deviation of the data. The number of data is
not known a priori and you count them while
reading. [You are not allowed to use any array]

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 17

Arithmetic Mean

The arithmetic mean (Am ) of a set of data


{d1 , d2 , · · · , dn } is
1X n
Am = di .
n i=1

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 18

Geometric Mean

The geometric mean (Gm ) of a set of data


{d1 , d2 , · · · , dn } is
v
u n
Y
Gm = di .
u
n
t
i=1

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 19

Harmonic Mean

The harmonic mean (Hm ) of a set of data


{d1 , d2 , · · · , dn } is
n
Hm = Pn 1 .
i=1 di

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 20

Standard Deviation

The standard deviation (σ) of a set of data


{d1 , d2 , · · · , dn } is
v
n
u1 X
u
σ = t (d i − A m )2
n i=1
v
u1 X
u n
= t (d2i − 2di Am + A2m )
n i=1
v  
u1 X n n
u
d2i − 2Am di + nA2m 
X
=
u 
t
n i=1 i=1

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 21

What to Compute

1. The number of data - use a variable


initialized to zero, and increment it after
each read.
2. The sum of the data - initialization is same
as (1), accumulate the sum in the variable,
the variable type is different.
3. The product of the data - similar to (1), but
the initialization is different; accumulate the
& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 22

product.
4. The sum of the reciprocal and the sum of
the square of the data - similar to case (2).

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 23

Input data Set

Input Am Gm Hm σ
{1} 1.0 1.0 1.0 0.0
{100, 200} 150.0 141.42 133.33 50.0
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 5.5 4.53 3.414 2.87
{1.5, 2.5, 3.5, 4.5, 5.5, 6.5} 4.0 3.58 3.141 1.707

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 24

Assignment III

Write a C program that reads the x-y coordinates of


three distinct points A(x1 , y1 ), B(x2 , y2 ) and C(x3 , y3 ) in
order. The program prints the following output.
1. whether the third point C is on the line joining A and
B; if not whether it belongs to the upper half-space
(left half-space in case the line is parallel to the
y-axis) or the lower half-space (right half-space) and
2. if one moves from the first point (A) to the second
point (B), whether he will have to turn left or to
right to reach the third point (C) without crossing
the line of AB.
& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 25

Y
C

B
left
right
C C
O X

left half−space A
right half−space

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 26

Y
C
right half−space
left half−space
A

C C
O X
left
right
B

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 27

Y
C
B
C left

right C

O X
lower half−space
upper half−space

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 28

Y
C

A
C lower half−space

O X
right
B left
upper half−space

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 29

Y
C
C

B
right
upper half−space

O X
left

lower half−space A
C

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 30

Y
C
C

A
lower half−space
left
O X
right upper half−space

C B

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 31

Input data

Test your program on the following sets of input data.

A(x1 , y1 ) B(x2 , y2 ) C(x3 , y3 )


(1, 2) (1, −3) (1, 4)
(1, 2) (1, −3) (−1, 4)
(1, 2) (1, −3) (3, 4)
(1, −3) (1, 2) (−1, 4)
(1, −3) (1, 2) (3, 4)

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 32

Input data

A(x1 , y1 ) B(x2 , y2 ) C(x3 , y3 )


(0, 2) (1, 0) (2, −2)
(0, 2) (1, 0) (2, −1)
(0, 2) (1, 0) (−1, −1)
(1, 0) (0, 2) (2, −1)
(1, 0) (0, 2) (−1, −1)

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 33

Input data

A(x1 , y1 ) B(x2 , y2 ) C(x3 , y3 )


(0, −2) (1, 0) (2, 2)
(0, −2) (1, 0) (2, 3)
(0, −2) (1, 0) (2, −1)
(1, 0) (0, −2) (2, 3)
(1, 0) (0, −2) (2, −1)

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 34

Practice Problem I : Quadratic Equation

Write a C program to find the roots of the


quadratic equation ax2 + bx + c = 0. The input
date are a, b and c (any one, two or all three of
them may√ be zero). Take special care when the
value of b2 − 4ac is equal to zero or −ve.

& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 35

Cases of Quadratic Equation

Following are the different cases depending on


data.
1. a = b = 0 : not an equation.
2. a = 0 : not a quadratic equation.
3. b2 − 4ac = 0 : quadratic equation - equal roots.
4. b2 − 4ac > 0 : quadratic equation - unequal
roots.
5. b2 − 4ac < 0 : quadratic equation - complex
roots (real and imaginary parts are to be
computed separately).
& %
' $
PDS Tut. & Lab.: III (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 36

Sample Input Data

a b c
0 0 1.0
0 −2.0 −8.0
4.0 12.0 9.0
2.5 4.0 1.6
1.0 −2.0 −15.0
1.25 4.0 4.0

& %

S-ar putea să vă placă și