Vous êtes sur la page 1sur 37

' $

PDS Tut. & Lab.: IV (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 (17.08.2007)

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

Download

Download the file date170807.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.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 3

Types of Errors

• Compilation error.
• Linker error.
• Run-time error.
• Logical error.

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

Compilation Error

$ cc -Wall err.c
err.c: In function ‘main’:
err.c:6: parse error before "printf"

• The C program is not well-formed.


• Look at line number 5-6 for the error.

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

Compilation Error

int main()
{
int n ;

n = 10
printf("n = %d\n", n) ;
return 0 ;
}

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

Compilation Error

$ cc -Wall err.c
err.c: In function ‘main’:
err.c:6: called object is not a function
#include <stdio.h>
int main() {
int n ;

n = 10 ;
printf("%d\n",n(n-1)) ;
return 0 ;
}
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 7

Compilation Error

$ cc -Wall err.c
err.c: In function ‘main’:
err.c:12: parse error at end of input

• Error message is not very clear. There are


only 11 lines in the code.

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

Compilation Error

1 #include <stdio.h>
2 int main() {
3 int n, m, p ;
4 scanf("%d", &n) ;
5 if(n > 0) {
6 m = 2*n - 5 ;
7 if(m < 0) {
8 p = m + 10 ; n++ ;
9 }
10 printf("%d", m * n) ;
11 }
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 9

Linker Error

$ cc -Wall err.c
err.c: In function ‘main’:
err.c:9: warning: implicit declaration .. ‘facterial’
/tmp/cc4wwrDz.o: In function ‘main’:
/tmp/cc4wwrDz.o(.text+0x53): undefined reference to ‘facter
collect2: ld returned 1 exit status

• The function facterial is not defined.


• Most likely the function name is not written
correctly.

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

Linker Error

#include <stdio.h>
int factorial(int n) {
if(n == 0) return 0 ;
else return n*factorial(n-1) ;
}
int main() {
int n ;
n = 10 ;
printf("fact(%d)=%d\n",
n,facterial(n));
return 0 ;
}
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 11

Linker Error

$ cc -Wall err.c
err.c:5: warning: implicit declaration of function ‘sqrt’
/tmp/ccqeQ265.o: In function ‘main’:
/tmp/ccqeQ265.o(.text+0x28): undefined reference to ‘sqrt’
collect2: ld returned 1 exit status

• math.h not included.


• The mathematical library not linked for
compilation.

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

Linker Error

#include <stdio.h>
int main() {
float n ;
n = 10.0 ;
printf("sqrt(%f)=%f\n",
n,sqrt(n));
return 0 ;
}

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

Run-time Error

$ cc -Wall err.c
err.c: In function ‘main’:
err.c:5: warning: format argument is not a pointer (arg 2)
err.c:6: warning: control reaches end of non-void function
$ a.out
5.0
Segmentation fault

• Illegal memory reference, in this case ’&’ is


missing - scanf(”%f”, n) ;

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

Run-time Error

#include <stdio.h>
int main() {
float n ;

scanf("%f", n) ;
}

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

Run-time Error

$ a.out
1
Floating point exception
• Funny message, actually divide-by zero -
printf(”%d”,n/(n-1)) ;

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

Logical Error

• The program does not behave the way you


thought it should!

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

Logical Error

#include <stdio.h>
#include <math.h>
int main() {
float n ;
scanf("%f", &n) ;
printf("%f\n", pow(n, 1/3)) ;
return 0 ;
}

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

Logical Error

$ a.out
8.0
1.000000
$

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

Pythagorean Triple

A 3-tuple (a, b, c) of natural numbers is called a


Pythagorean triple if a2 + b2 = c2 . A
Pythagorean triple is called primitive if the
hcf(a, b) = 1.

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

Assignment IV

Write a C program that reads two positive


integers l and h as input and prints all the
primitive Pythagorean triples within the range l
to h i.e. l ≤ a, b, c ≤ h. The function to find out
the hcf (or gcd) of two unsigned integers is
provided. [Do not use pow() or array.]

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

hcf()

int hcf(int s, int l){


if(s == 0) return l;
return hcf(l%s, s);
}
The function takes two non-negative integers
(both are not equal to zero) as parameters and
returns the hcf of them.

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

Steps

1. Read a pair of unsigned integers in l and h


(the lower one should be in l).
2. You may have three nested for-loops
3. The value of c varies from l to h, the value of
a varies from l to c and the value of b varies
from l to a. The hcf(a, b) must be one.

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

Report

Test your program for primitive Pythgorean


triples within the following ranges:
(1 − 50), (50 − 200), (200 − 300).
 
Send the .c file 

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

Rational as Continued Fraction

A rational number, pq , where p, q are


non-negative integers and q 6= 0, may be
expressed as a continued fraction. Following is
an example for 61
27 .

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

Rational as Continued Fraction

61 1
=2+ 1
27 3+ 1
1+
6
This may be represented in a compact form as
2; 3, 1, 6, where the first 2 indicates the integral
part of the improper fraction.

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

Assignment V

Write a C program to read the numerator and


the denominator (6= 0) of a rational number
(both non-negative) and print it as a continued
fraction in compact form. You may use the
following algorithm.

Do not use any array.

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

1. Read both the numerator and the denominator.


2. Print both the inputs.
3. Do the following steps in a loop as long as the
denominator is not equal to zero.
(a) Find the quotient and the remainder of
(numerator ÷ denominator).
(b) Print the quotient as an element of the continued
fraction.
(c) Calculate the new numerator and the new
denominator.

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

Test the program with at least ten (10) data


sets. The data set should contain the following
(numerator, denominator) pairs,
{(0, 5), (1, 5), (3, 4), (39, 12)}.
 
Send the .c file 

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

ASCII to Integer

The ascii values for ‘0’ to ‘9’ are 48 to 57 (in


decimal). One may read a digit as a character
and extract its numerical value as follows.

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

ASCII to Integer

#include <stdio.h>
int main(){
char a ;

scanf("%c", &a) ;
printf("’%c’: %d\n", a, a - ’0’) ;
return 0 ;
}
& %
' $
PDS Tut. & Lab.: IV (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 31

ASCII to Integer

$ a.out
6
’6’: 6
$

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

Practice Problem IIa

Write a C program to convert a string of decimal


digits (not more than 9 digits) read as a string of
ascii characters, to its binary value.
Input: “1234” - read one character at a time.
Output: “0100 1011 001”- equivalent binary
value (the leftmost bit is least significant).

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

1. Read, one character at a time from the input


string of the decimal characters (from the
most significant digit). Print every character
you read. The input is terminated by the
newline character (‘\n’).
2. Accumulate the extracted integer value in a
unsigned int variable.
3. Use the usual divide-by-2 procedure to extract
the bits of the number from the accumulated
value.

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

Practice Problem III

Write a C program that reads an integer n (> 1)


and finds out whether it is a prime number. If n is
composite and odd, the program prints a pair of
factors (> 1 and < n) of n. Note that -
• 2 is the first prime number.
• A composite number n must have a factor

f ≤ n.

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

Practice Problem IV

A pair of prime numbers of the form 2n + 1 and


2n + 3, n ∈ N are called twin primes. Write a C
program to print all the twin primes within a
given range.

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

Practice Problem V

A variable of type int has a 32-bit representation.


Let the number be 3433.
0 ··· 0 1 1 0 1 0 1 1 0 1 0 0 1
0 · · · 0 211 210 0 28 0 26 25 0 23 0 0 20

211 + 210 + 28 + 26 + 25 + 23 + 20
= 2048 + 1024 + 256 + 64 + 32 + 8 + 1
= 3433

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

Practice Problem V (cont.)

Write a C program that reads an integer within


the range of int and count the number of 1’s
present in its binary representation. There are
seven (7) 1’s in the binary representation of 3433.
Can your program be modified to count the
number of 1’s in a variable of type float (32-bit)?

& %

Vous aimerez peut-être aussi