1 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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
Problem Solving and Program Design Programming. COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 2 Problem Solving Process l Define.
1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
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 Session-I & II CSIT-121 Spring 2006 Session Targets Introducing the VC++.NET Solving problems on computer Programming in C++ Writing and Running Programs.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2: Introduction to C++.
Introduction to C Programming
1 Lab-1 CSIT-121 Fall 2004 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
1 Special Programming Workshop CSIT-120 Fall 2000 Workshop Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Chapter 2 Variables.
Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
A Sample Program #include using namespace std; int main(void) { cout
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
Data Types, Identifiers, and Expressions
Basic Elements of C++ Chapter 2.
Programming Funamental slides
Programming Funamental slides
Chapter 2 Variables.
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Unit 3: Variables in Java
Chapter 2 Primitive Data Types and Operations
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

1 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)

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

3 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

4 Lab Exercise 2-A Perform Experiment 2.1 Page 17 of the lab manual

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: $ check #2: $ 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

12 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: $ and check #2: $ can be represented as floating point numbers in a C++ program

13 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 program

14 Variables 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 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 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 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 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 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 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 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 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-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-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 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 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 Solution A customer deposits checks in his/her bank account as follows: check #1: $ check #2: $ 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 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 Solution Using data analysis results, we can complete the declarations part of the C++ program float const CHECK1= ; float const CHECK2= ; int const WDRAW=200; float credit,debit,effective_credit;

30 Lab Exercise 2-B Solution Next, we develop an algorithm to solve this problemINITIAL Read the check amounts and withdrawal amount Determine credit, debit and effective credit Display results and Exit

31 Lab Exercise 2-B Solution Final Algorithm is obtained by refining initial algorithmFINAL 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 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 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 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