1 lab 2 csit-120 spring 2001 session ii-a (feb 13th) operations on data lab exercise 2-a data types...

35
1 Lab 2 CSIT-120 Lab 2 CSIT-120 Spring 2001 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

Post on 20-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

1

Lab 2 CSIT-120 Lab 2 CSIT-120 Spring 2001Spring 2001

• Session II-A (Feb 13th)• Operations on Data • Lab Exercise 2-A• Data Types• Variables• Lab Exercise 2-B• Session II-B (Feb 20th)

Page 2: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

2

RevisionRevision

• When closing a program and starting to write a new program in Visual C++, what should we do?

• What symbols should precede comments?• What does keyword “const” mean?• How do we read user input?• What are the two main parts of a C++

program?

Page 3: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

Page 4: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

4

Lab Exercise 2-ALab Exercise 2-A

• Perform Experiment 2.1 Page 17 of the lab manual

Page 5: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

Page 6: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

Page 7: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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. We are asked to 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?

Page 8: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

Statement AnalysisStatement Analysis

• We can determine the data requirements by highlighting the nouns used in the statement and extracting the data items from the nouns

• Mini Exercise2-1: Determine the data requirements of the program

• We can also determine if the data items are input or output

Page 9: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

Algorithm DevelopmentAlgorithm Development

• Once we know the data items, we can analyze the question(s) posed in the problem statement (verbs) to determine “functionality” of the desired program

• Thus we can start developing an algorithm to solve the problem

• Mini Exercise2-2: Analyze the problem and develop the algorithm required to solve it

Page 10: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

Page 11: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

Page 12: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

Page 13: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

Page 14: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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?

Page 15: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

Page 16: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

Page 17: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

Page 18: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

• Result = (CGPA<2)

• if Result==TRUE then performance is poor

Page 19: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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”

Page 20: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

Page 21: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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;

Page 22: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

Page 23: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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 comments on every declaration as well as major block of statements describing what it is and what it accomplishes

Page 24: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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 2/27

Page 25: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

Page 26: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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?

Page 27: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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?

Page 28: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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)

Page 29: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

29

Lab Exercise 2-B SolutionLab Exercise 2-B Solution

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

• double const CHECK1= 435.61;

• double const CHECK2= 365.89;

• int const WDRAW=200;

• double credit,debit,effective_credit;

Page 30: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

Page 31: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

31

Lab Exercise 2-A SolutionLab Exercise 2-A Solution

• Next we refine and complete the algorithm

FINALFINAL

• Read the inputs(Read check1, check2 and withdrawal)

• Perform computations(credit = check1+check2 debit=withdrawal

effective_credit=credit-debit)

• Display results

Page 32: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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

• Use cin>>YourVariable to read some value from the user and store it in YourVariable

Page 33: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

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 arguments, truncating remainder IF BOTH ARE INTEGERS

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

Page 34: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

34

Mod and Div ExampleMod and Div Example

• You have 28 candies to be distributed equally in 5 kids. How many candies does each kid get and how many candies will be left with you after the distribution?

• Answer:

• Each one gets: 28 Div 5 candies

• Leftovers: 28 Mod 5

Page 35: 1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)

Mod and Div OperatorsMod and Div Operators

• Perform Experiment 2.6• Lab Assignment 1(Due 2/27/2001)• Develop a program that computes the biweekly pay stub

for an employee. It asks the user to input the gross salary. It then deducts social security at the rate of 2% and union fees at the rate of 1%. It withholds federal tax at the rate of 15% and state tax at the rate of 8%. All deductions and the net pay must be shown in a nice format. Show data analysis and use a lot of comments in your program.