Beruflich Dokumente
Kultur Dokumente
Lecture Outline
CS1010E Lecture 6
Computational Problem Solving and
Functional Abstraction
Semester 1 2015 / 2016
C Code
#include <stdio.h>
int main(void){
int i;
for (i = 1; i <= 9; i++)
printf("%d ",i);
return 0;
}
10/3/2015
C Code
section
global _start
_start:
#include <stdio.h>
int main(void){
int i;
.text
;must be declared for using gcc
;tell linker entry point
mov ecx,10
mov eax, '1'
l1:
section
num resb 1
Binary code
1000100101001
1101010101010
0101001010101
0100101101011
1110101010010
1001001010010
1010010101010
0101001010010
1010101010010
1010010100101
1010101001010
Pseudo-Code
C Code
10/3/2015
Problem:
Write a C Program that
computes the taxi fare
from distance travelled and
prints it out.
$3.40
$0.22
$0.22
10/3/2015
Wishful thinking
Assume you already a function which
computes the taxi fare given distance travelled!
Give function meaningful name compTaxiFare
What is input required distance
What is output fare
This is functional abstraction
Functional Abstraction
Functional Abstraction
=
Black Box
No need to know how a car works to drive it!
WHAT
HOW
10/3/2015
#include <stdio.h>
double compTaxiFare(int d);
#include <stdio.h>
int main(void) {
int d;
double fare;
If we assume compTaxiFare
is written then we already
have our program!
Step 1
Step 2
Step 3
10/3/2015
1.
Good Abstraction
Functional abstraction
separates specification
from implementation
Specification = What
Implementation = How
10/3/2015
Stage1
Stage2
Stage3
10/3/2015
double compTaxiFare(int d) {
double fare;
Yet again, if we assume fareStage2
if (d <= 1000)
already have our program!
fare = 3.40;
else if (1000 < d && d <= 10000)
fare = fareStage2(d);
else
fare = fareStage3(d);
return fare;
}
#include <stdio.h>
double computeTaxiFare(int d);
double fareStage2(int d);
double fareStage3(int d);
int main() {
int d;
double fare;
printf("Enter dist: ");
scanf("%d",&d);
fare = compTaxiFare(d);
printf("Taxi fare = %f\n",fare);
return 0;
10/3/2015
Implementing fareStage2
Try out simple examples
Example distance = 3,300 m
Implementing fareStage2
Try out simple examples
Example distance = d
Generalize !
fare = $3.40 +
$0.22 +
(((3300-1001)/400)*$0.22)
= $4.72
fare = $3.40 +
$0.22 +
(((d-1001)/400)*$0.22)
fareStage2
Implementing fareStage3
Try out simple examples
Example distance = 14500 m
double fareStage2(int d) {
return 3.40+0.22+(((d-1001)/400)*0.22);
}
10/3/2015
Implementing fareStage3
Try out simple examples
Example distance = d
Generalize !
Implementing fareStage3
Try out simple examples
Example distance = d
Calculated by fareStage2
fare = $3.40 +
$0.22 + (((9000-1001)/400)*$0.22) +
$0.22 + (((d-10001)/350)*$0.22)
fare = $3.40 +
$0.22 + (((9000-1001)/400)*$0.22) +
$0.22 + (((d-10001)/350)*$0.22)
Implementing fareStage3
Try out simple examples
fareStage3
Example distance = d
for 1st 10000m -> fareStage2(10000)
at 10001m -> $0.22
remaining (d-10001)m -> ((d-10001)/350)*$0.22
double fareStage3(int d) {
return fareStage2(10000)+
0.22+(((d-10001)/350)*0.22);
}
fare = fareStage2(10000)+
$0.22 + (((d-10001)/350)*$0.22)
10
10/3/2015
#include <stdio.h>
double computeTaxiFare(int d);
double fareStage2(int d);
double fareStage3(int d);
int main() {
int d;
double fare;
printf("Enter dist: ");
scanf("%d",&d);
fare = compTaxiFare(d);
printf("Taxi fare = %f\n",fare);
return 0;
Problem Solved !
return 3.40+0.22+(((d-1001)/400)*0.22);
}
double fareStage3(int d) {
return fareStage2(10000)+0.22+(((d-10001)/350)*0.22);
}
Good Abstraction
Good Abstraction
Water molecule
represented as 3 balls
11
10/3/2015
Building a house
House
Bricks
Rooms
"Building" a program
Program
Primitives
Functions
Divide and
Conquer
Walls
Bricks
Primitives
12
10/3/2015
Normal
Limousine
Chrysler
Flag-Down
(inclusive of 1st km
or less)
$3.40
$3.90
$5.00
Every 400m
thereafter or less up
to 10km
$0.22
$0.22
$0.33
$0.22
$0.22
$0.33
int main() {
int d,taxi;
double fare;
printf("Enter taxi company and dist: ");
scanf("%d%d",&taxi,&d);
Dont work anymore !
fare = compTaxiFare(d);
printf("Taxi fare = %f\n",fare);
return 0;
}
Read in taxi company 1 is normal, 2 is limousine, 3 is Chrysler
if (d <= 1000)
fare = 3.40;
else if (1000 < d && d <= 10000)
fare = fareStage2(d);
else
All the highlighted "magic
fare = fareStage3(d);
numbers" will not work across
return fare;
}
13
10/3/2015
if (d <= 1000)
passed in by
fare = flagdown;
else if (1000 < d && d <= 10000)
fare = fareStage2(d,flagdown,incre);
else
fare = fareStage3(d,flagdown,incre);
return fare;
}
double fareStage2(int d, double flagdown, double incre){
return flagdown+incre+(((d-1001)/400)*incre);
}
double fareStage3(int d, double flagdown, double incre) {
return fareStage2(10000,flagdown,incre)+incre+
(((d-10001)/350)*incre);
}
14
10/3/2015
if (d <= 1000)
numbers" will not work
fare = flagdown;
else if (1000 < d && d <= 10000)
fare = fareStage2(d,flagdown,incre);
else
fare = fareStage3(d,flagdown,incre);
return fare;
!
Encode stage distances and distance per increment as
Constants !
}
double fareStage2(int d, double flagdown, double incre){
return flagdown+incre+(((d-1001)/400)*incre);
}
double fareStage3(int d, double flagdown, double incre) {
return fareStage2(10000,flagdown,incre)+incre+
(((d-10001)/350)*incre);
}
STAGE1
STAGE2
BLOCK1
BLOCK2
1000
10000
400
350
15
10/3/2015
Good Abstraction
4.
#include <stdio.h>
#define STAGE1 1000
#define STAGE2 10000
#define BLOCK1 400
#define BLOCK2 350
double compTaxiFare(int d, double flagdown, double incre);
double fareStage2(int d, double flagdown, double incre);
double fareStage3(int d, double flagdown, double incre);
int main() {
int d,taxi;
double fare;
printf("Enter taxi company and dist: ");
scanf("%d%d",&taxi,&d);
if (taxi == 1) /* normal cab */ {
printf("Here !\n");
fare = compTaxiFare(d,3.40,0.22);
}
else if (taxi == 2) /* limousine */
fare = compTaxiFare(d,3.90,0.22);
else /* Chrysler */
fare = compTaxiFare(d,5.0,0.33);
printf("Taxi fare = %f\n",fare);
return 0;
}
Good Abstraction
5.
16
10/3/2015
return;
}
#include <stdio.h>
#include <stdio.h>
int main(void) {
int a,b,c,temp;
int main(void) {
int a,b,c;
Example program
that does swapping
without using the
swap function
scanf("%d%d%d",&a,&b,&c);
scanf("%d%d%d",&a,&b,&c);
Example program
after using the swap
function
return 0;
}
17
10/3/2015
Summary
Computational Problem solving using the
ladder of abstraction
Good abstraction
Separate specification from implementation
Makes code easier to read
Hides irrelevant detail
Allows code to be more general
Captures common patterns and allow for reuse
Makes it more natural to think of solution in terms
of task and subtasks
18