Title: Lab Session-IV CSIT121 Fall 2000
1Lab Session-IV CSIT121 Fall 2000
- Some Questions
- Scope of Variables
- Top Down Design Problem
- The Solution
- Lab Exercises
- Lab Exercise for Demo
2Some Questions
- What will be the result?
- 15/12 and 15.0/12.0
- What is the order of execution?
- A B - E / C
- How can we use the built-in mathematical
functions? - What is a function prototype?
3Scope of Global Variables
- include ltiostreamgt
- using namespace std
- void prettyprint()
- int length
- void main()
-
- length24
4Scope of Global Variables
- cout ltlt "I can refer to length here" ltlt
lengthltltendl - prettyprint()
-
- void prettyprint ()
- length26
- cout ltlt "I can also refer to length
here"ltltlengthltltendl -
5Scope of Variables Declared in Main Function
- include ltiostreamgt
- using namespace std
- void prettyprint()
- void main()
-
- int length24
- cout ltlt "I can refer to length here" ltlt
lengthltltendl
6Scope of Variables Declared in Main Function
- prettyprint()
-
- void prettyprint ()
-
- cout ltlt "I cannot refer to length here"ltltendl
- length26
-
7Scope of Variables Declared in Any Function
Except Main
- include ltiostreamgt
- using namespace std
- void prettyprint()
- void main()
-
- cout ltlt "I cannot refer to length here" ltltendl
- length24
8Scope of Variables Declared in Any Function
Except Main
- prettyprint()
-
- void prettyprint ()
-
- int length
- cout ltlt "I can only refer to length
here"ltltlengthltltendl - length26
-
9Top Down Design Problem
- You are required to develop a software for
solving a problem. The problem is described as
follows - A List of four numbers (A,B,C,D) is given.
Compute its mean, largest number and smallest
number
10Top Down Design
- Underline the nouns and verbs for data modeling
and algorithm development - Answer
- A List of four integers (A,B,C,D) is given.
Compute its mean, largest number and smallest
number
11Data Design
- Now we can determine input data and its type
- Answer
- int A, B, C, D
- Also the output data and its type can be
determined - float mean
- int largest, smallest
12Algorithm Design
- We develop an algorithm for this problem
- ALGORITHM
- 1. Read A, B, C, D
- 2. Determine the mean
- 3. Determine the largest number
- 4. Determine the smallest number
- 5. Display the results
13Algorithm Refinement
- Next step is to refine the algorithm
ALGORITHM 1. Read A, B, C, D 2. Determine the
mean 2.1 Mean is (ABCD)/4 3. Determine the
largest number 3.1 Compare AB and CD and
extract larger value 3.2 Compare extracted
values and report the larger one 4. Determine the
smallest number 4.1 Compare AB and CD and
extract smaller value 4.2 Compare extracted
values and report thesmallerr one 5. Display the
results
14Coding for Main Function
- Now Coding for Main() can be done
include ltiostreamgt include ltiomanipgt using
namespace std int largest_number(int, int, int,
int) int smallest_number(int, int, int,
int) void main() int A,B,C,D float mean int
largest, smallest cout ltlt"Input all numbers"
15Coding for Main Function
- cin gtgtAgtgtBgtgtCgtgtD
- mean float(ABCD) / 4.0
- largest largest_number(A,B,C,D)
- smallest smallest_number (A,B,C,D)
- coutltlt"Here are the values ltltsetw(8)ltltmeanltltsetw(
8) - ltltlargest
- ltltsetw(8)ltltsmallestltltendl
-
16Coding for Other functions
- Coding for largest_number()
int largest_number(int k, int j, int l, int
m) int temp1, temp2 if (kgtj) temp1k else
temp1j if (lgtm) temp2l else temp2m if
(temp1gttemp2) return temp1 else return temp2
17Coding for Other Functions
- Coding for smallest_number
int smallest_number(int k, int j, int l, int
m) int temp1, temp2 if (k lt j) temp1k else
temp1j if (lltm) temp2l else temp2m if
(temp1lttemp2) return temp1 else return temp2
18Lab Exercises
- Develop a program that calls the math library
function pow(x,y) for calculating x to the power
y. Then develop your own version of pow(x,y).
This version should check and report error if
x0. Also it should return value 1 if y0 and
value x if y1 without going into calculations.