Lab Session-II CS121 Summer

Download Report

Transcript Lab Session-II CS121 Summer

Lab Session-IV CSIT121
Spring 2002
•
•
•
•
•
•
Some Questions
Scope of Variables
Top Down Design Problem
The Solution
Lab Exercises
Lab Exercise for Demo
1
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?
2
Scope of Global Variables
•
•
•
•
•
•
•
#include <iostream>
using namespace std;
void prettyprint();
int length;
void main()
{
length=24;
3
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;
4
Scope of Variables Declared in
Main Function
•
•
•
•
•
•
•
#include <iostream>
using namespace std;
void prettyprint();
void main()
{
int length=24;
cout << "I can refer to length here" <<
length<<endl;
5
Scope of Variables Declared in
Main Function
•
•
•
•
•
prettyprint();
}
void prettyprint ()
{
cout << "I cannot refer to length
here"<<endl;
• length=26;
•}
6
Scope of Variables Declared in
Any Function Except Main
•
•
•
•
•
•
#include <iostream>
using namespace std;
void prettyprint();
void main()
{
cout << "I cannot refer to length here"
<<endl;
• length=24;
7
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;
8
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
9
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
10
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
11
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
12
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
13
Coding for Main Function
• Now Coding for Main() can be done
#include <iostream>
#include <iomanip>
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";
14
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;
15
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;
}
16
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;
}
17
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”;
18