Lab Exercise

Document Sample
Lab Exercise Powered By Docstoc
					Lab 2 CSIT-120
Fall 2000
•   Session II-A (September 14th)
•   Operations on Data
•   Lab Exercise 2-A
•   Data Types
•   Variables
•   Lab Exercise 2-B
•   Session II-B (September 21st) (starts on slide 24)

Lab-1 Revision

• When closing a program and starting to
  write a new program, what should we do?
• What symbols should precede comments?
• What is in a header file?
• If there is only one function in your
  program, what is its name?
• What does “cout” statement accomplish?

Operations on Data

• C++ can work with many types of data
• Programs need to do “number crunching” if
  these are to be of any use
• In previous labs we have seen how C++ can
  use I/O functions to output text strings
• In a similar way Programs and functions
  can also handle numbers

Lab Exercise 2-A

• Perform Experiment 2.1 Page 17 of the lab

Operations on Data

• We have seen that the computer has an
  ALU that can perform a lot of operations on
  the data
• We can develop programs in C++ that use
  these ALU operations for solving our real
  life problems
• In order to do so, we need to follow C++
  syntax and common sense rules
Some Rules to Know

• To do effective programming for problem
  solving, we should state the problem to be
  solved as clearly as possible.
• The problem statement is sufficient to
  determine the data requirements
• Also, it can be used to develop an algorithm
  for solving it on the computer
An Example

• As an example, consider following problem
• A customer deposits checks in his/her bank
  account as follows:
• check #1: $435.61
• check #2: $365.89
• and at the same time withdraws 200 dollars cash.
  Can you write a computer program that displays
  the credit and debit separately for this transaction
  and then display final amount of money credited
  into the account?
Statement Analysis

• We can quickly determine the data
  requirements of the program by highlighting
  the nouns used in the statement
• Mini Exercise2-1: Determine the data
  requirements of the program
• We can also determine if data items are
  input or output
Algorithm Development

• Once we know the data items, we can
  analyze the question(s) posed in the
  problem statement
• Thus we can start developing an algorithm
  to solve the problem
• Mini Exercise2-2: Analyze the problem and
  develop the steps required to solve it
Algorithm Development

• The problem is to determine debit and credit
  separately and then final credit for a
  transaction for one bank account
• It can be solved by using two temporary
  variables “debit” and “credit”
• credit = check1+check2
• debit = withdrawal
• final credit = credit - debit
Data Types

• Once we have determined the data
  requirements and the algorithm to solve our
  problem, we need some data formats in
  order to model our data
• For example, the checks submitted were not
  in whole number amount. We need a format
  in which we can represent numbers
  consisting of integer and fractional parts
Data Types

• Visual C++ provides several data formats
  (data types) for our programs
• Using these data types, we can represent our
  data in the programs
• For example, the amounts
• check #1: $435.61 and check #2: $365.89
• can be represented as floating point
  numbers in a C++ program
Data Types

• Similarly, the amount of withdrawal ($200)
  is a whole number. It can be represented as
  an integer
• The integer and floating point numbers can
  be defined as constants or variables in a


• A constant never changes its value
  throughout the program
• A variable will change its value during the
  program because the program may assign it
  some new value after computations
• Mini-Exercise2-3
• What are the constants and variables in the
  data in our example?
Data Types and Variables

• You have to declare all variables and
  constants in your program before you use
  the same
• Your program consists of functions like
  main() etc.
• Each function consists of two sections
• Declarations and Statements

Data Types and Variables

• Declarations consist of all constants and
  variables that you will use in your program
• Statements include all the “number
  crunching” directives that you want the
  computer to execute
• Let us look at the primitive data types in
  C++ and rules for declaring constants and
Data Types and Variables

• float data type is used to represent numbers
  that consist of integer and fractional parts.
• For example:
• CGPA = 2.54
• integer data type is used to represent whole
  numbers, such as
• Number of students = 5000

Data Types and Variables

• char data type can represent individual
  characters such as ‘c’ or digits such as ‘9’
• bool data type can be used to determine the
  result of a test
• For example, if signal==RED then don’t_go
• Result = (CGPA<2)
• if Result==TRUE then performance is poor

Data Types and Variables

• For constant values, it is advisable to use
  names instead of using the value directly
• For example,
• const int MY_LIMIT=500;
• if (price > MY_LIMIT) then
• if (don’t_buy) then “exit the shop”

Data Types and Variables

• The names are more meaningful and make
  the program easier to read
• If the constant is used at several places, we
  do not have to change the value at all these
  places. We can just change the declaration
• Constants are usually named in CAPITAL
  LETTERS to distinguish the same from
Data Types and Variables

• Variables are declared in the same way as
  constants, except that initializing with a
  value is optional
• For example, if you want to make your
  purchase limit flexible, you can declare it as
  a variable
• int my_limit;

Data Types and Variables

• The identifiers used to declare constants and
  variables are subject to certain C++ rules
• No spaces within the identifier
• Do not begin the identifier with a digit or
• Letters, underscores and digits allowed
• Keep the length under 31 to avoid
  portability problems
Lab Exercise 2-B

• Complete the program that can solve the
  problem of computing and showing credit,
  debit and effective credit for a transaction
• Use structured programming and put a
  comment on every declaration as well as
  major block of statements describing what it
  is and what it accomplishes

Session II-B

•   Review of Topics and Solving Exercise 2-B
•   Initializing Variables
•   Experiments
•   Mod and Div Operators
•   Experiments
•   Data Input and Output
•   Experiments
•   Lab Assignment #2 Due 9/28
Review of Topics Covered

• What primitive data types are used in C++
  to represent numerical values?
• During comparisons, the result may be
  assigned to a variable. What is the data type
  of such a variable?
• Ex: testresult = (smoke_detector ringing)
• if (testresult) then someone is smoking OR
  there is something on fire
Review of Topics

• Why do we perform data analysis on the
  given problem?
• What information is obtained from data
  analysis and how is it used in the program?
• Describe the structured method for
  algorithm development
• Why does initial algorithm only show
  WHAT is to be done?
Lab Exercise 2-B Solution

• A customer deposits checks in his/her bank
  account as follows:
• check #1: $435.61
• check #2: $365.89
• and at the same time withdraws 200 dollars
  cash. Can you write a computer program that
  displays the credit and debit for this
  transaction and display final amount of money
  credited into the account?
Lab Exercise 2-B Solution

• First we perform data analysis
• We determine that there are :
• 3 constant data items
check1, check2, withdrawal
• 3 output data items
credit, debit, effective_credit
• All are floating point numbers except
  withdrawal (whole number)
Lab Exercise 2-B Solution

• Using data analysis results, we can
  complete the declarations part of the C++
• float const CHECK1= 435.61;
• float const CHECK2= 365.89;
• int const WDRAW=200;
• float credit,debit,effective_credit;

Lab Exercise 2-B Solution

• Next, we develop an algorithm to solve this
• Read the check amounts and withdrawal
• Determine credit, debit and effective credit
• Display results and Exit

Lab Exercise 2-B Solution

• Final Algorithm is obtained by refining
  initial algorithm
• Read the check amounts and withdrawal amount
   – {check1, check2, withdrawal initialized as constants}
• Determine credit, debit and effective credit
   – {credit=check1+check2; debit=withdrawal and
     effective credit=credit-debit;}
• Display results and exit
   – {display credit, debit and effective credit}            31
Initializing Variables

• We initialize variables before trying to use
  the same on RHS of a computation or
  printing out their values
• Experiment 2.4 lab manual
• Reading from the user
• Let us demonstrate how to read something
  from the user and display it back

Mod and Div Operators

• You can perform all arithmetic operations
  on numeric data including division
• Division can be done in two ways
• Div operator (/) performs integer division
  on two integer arguments, truncating
• Mod (%) operator results in remainder after
  the division has been carried out
Mod and Div Operators

• Perform Experiment 2.6 (Demo Required)
• Lab Assignment 2(Due 9/28/2000)
• Write a C++ program to convert a given
  amount into quarters, dimes, nickles and
  pennies (Hint: convert to cents first and write an
  algorithm for doing it by hand). Your program
  should accept the amount from the user
  using cin>>amount; and finally it should
  print the number of all coin denominations

Shared By: