1 lab 2 csit-120 fall 2000 session ii-a (september 14th) operations on data lab exercise 2-a data...

34
1 Lab 2 CSIT-120 Lab 2 CSIT-120 Fall 2000 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)

Post on 22-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

1

Lab 2 CSIT-120 Lab 2 CSIT-120 Fall 2000Fall 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)

2

Lab-1 RevisionLab-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?

3

Operations on DataOperations 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

4

Lab Exercise 2-ALab Exercise 2-A

• Perform Experiment 2.1 Page 17 of the lab manual

Operations on DataOperations 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 KnowSome 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 ExampleAn 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 AnalysisStatement 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 DevelopmentAlgorithm 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 DevelopmentAlgorithm 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 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

12

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

13

Data TypesData 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 program

14

VariablesVariables

• 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?

15

Data Types and VariablesData 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

16

Data Types and VariablesData 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 variables

17

Data Types and VariablesData 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

18

Data Types and VariablesData 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

19

Data Types and VariablesData 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 don’t_buy=TRUE;

• if (don’t_buy) then “exit the shop”

20

Data Types and VariablesData 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 variables

21

Data Types and VariablesData 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;

22

Data Types and VariablesData 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 underscores

• Letters, underscores and digits allowed

• Keep the length under 31 to avoid portability problems

23

Lab Exercise 2-BLab 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

24

Session II-BSession 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

25

Review of Topics CoveredReview 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

26

Review of TopicsReview 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?

27

Lab Exercise 2-B SolutionLab 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?

28

Lab Exercise 2-B SolutionLab 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)

29

Lab Exercise 2-B SolutionLab Exercise 2-B Solution

• Using data analysis results, we can complete the declarations part of the C++ program

• float const CHECK1= 435.61;

• float const CHECK2= 365.89;

• int const WDRAW=200;

• float credit,debit,effective_credit;

30

Lab Exercise 2-B SolutionLab Exercise 2-B Solution

• Next, we develop an algorithm to solve this problem

INITIALINITIAL

• Read the check amounts and withdrawal amount

• Determine credit, debit and effective credit

• Display results and Exit

31

Lab Exercise 2-B SolutionLab Exercise 2-B Solution

• Final Algorithm is obtained by refining initial algorithm

FINALFINAL• 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}

32

Initializing VariablesInitializing 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

33

Mod and Div OperatorsMod 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 remainder

• Mod (%) operator results in remainder after the division has been carried out

Mod and Div OperatorsMod 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