Lab Session-IV CSIT121 Fall 2000 - PowerPoint PPT Presentation

About This Presentation
Title:

Lab Session-IV CSIT121 Fall 2000

Description:

void prettyprint(); void main() int length=24; ... void prettyprint(); void main() cout 'I cannot refer to length here' endl; length=24; ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 19
Provided by: sunyfr
Category:

less

Transcript and Presenter's Notes

Title: Lab Session-IV CSIT121 Fall 2000


1
Lab Session-IV CSIT121 Fall 2000
  • Some Questions
  • Scope of Variables
  • Top Down Design Problem
  • The Solution
  • Lab Exercises
  • Lab Exercise for Demo

2
Some 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?

3
Scope of Global Variables
  • include ltiostreamgt
  • using namespace std
  • void prettyprint()
  • int length
  • void main()
  • length24

4
Scope 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

5
Scope 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

6
Scope of Variables Declared in Main Function
  • prettyprint()
  • void prettyprint ()
  • cout ltlt "I cannot refer to length here"ltltendl
  • length26

7
Scope 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

8
Scope of Variables Declared in Any Function
Except Main
  • prettyprint()
  • void prettyprint ()
  • int length
  • cout ltlt "I can only refer to length
    here"ltltlengthltltendl
  • length26

9
Top 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

10
Top 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

11
Data 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

12
Algorithm 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

13
Algorithm 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
14
Coding 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"
15
Coding 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

16
Coding 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
17
Coding 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
18
Lab 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.
Write a Comment
User Comments (0)
About PowerShow.com