Download presentation
Presentation is loading. Please wait.
Published bySuzanna Tyler Modified over 9 years ago
2
1 Lab Session-IV CSIT121 Spring 2002 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercises Lab Exercise for Demo
3
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 What should we do in order to use the built-in mathematical functions? What is a function prototype?
4
3 Scope of Global Variables #include using namespace std; void prettyprint(); int length; void main() { length=24;
5
4 Scope of Global Variables cout << "I can refer to length here" << length<<endl; prettyprint(); } void prettyprint () {length=26; cout << "I can also refer to length here"<<length<<endl; }
6
5 Scope of Variables Declared in Main Function #include using namespace std; void prettyprint(); void main() { int length=24; cout << "I can refer to length here" << length<<endl;
7
6 Scope of Variables Declared in Main Function prettyprint(); } void prettyprint () { cout << "I cannot refer to length here"<<endl; length=26; }
8
7 Scope of Variables Declared in Any Function Except Main #include using namespace std; void prettyprint(); void main() { cout << "I cannot refer to length here" <<endl; length=24;
9
8 Scope of Variables Declared in Any Function Except Main prettyprint(); } void prettyprint () { int length; cout << "I can only refer to length here"<<length<<endl; length=26; }
10
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
11
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
12
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
13
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
14
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 (A+B+C+D)/4 3. Determine the largest number 3.1 Compare A&B and C&D and extract larger value 3.2 Compare extracted values and report the larger one 4. Determine the smallest number 4.1 Compare A&B and C&D and extract smaller value 4.2 Compare extracted values and report thesmallerr one 5. Display the results
15
14 Coding for Main Function Now Coding for Main() can be done #include 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 <<"Input all numbers";
16
15 Coding for Main Function cin >>A>>B>>C>>D; mean = float(A+B+C+D) / 4.0; largest = largest_number(A,B,C,D); smallest = smallest_number (A,B,C,D); cout<<"Here are the values” <<setw(8)<<mean<<setw(8) <<largest <<setw(8)<<smallest<<endl; }
17
16 Coding for Other functions Coding for largest_number() int largest_number(int k, int j, int l, int m) { int temp1, temp2; if (k>=j) temp1=k; else temp1=j; if (l>=m) temp2=l; else temp2=m; if (temp1>=temp2) return temp1; else return temp2; }
18
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 <= j) temp1=k; else temp1=j; if (l<=m) temp2=l; else temp2=m; if (temp1<=temp2) return temp1; else return temp2; }
19
18 Lab Exercise for Demo Develop a program using top down design approach that accepts 5 values from the user and then calls a function to determine and print the odd numbers from this list. Use if--else clause as shown below for determining odd numbers: if ((number%2)!= 0) cout<<“odd”;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.