Data Types & Variables

Similar documents
IT 1033: Fundamentals of Programming Data types & variables

CS242 COMPUTER PROGRAMMING

Fundamentals of Programming Data Types & Methods

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Arrays IT 1033: Fundamentals of Programming

Topic 2: C++ Programming fundamentals

The C++ Language. Arizona State University 1

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Exercise: Inventing Language

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Introduction to Programming EC-105. Lecture 2

C++ Programming: From Problem Analysis to Program Design, Third Edition

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Fundamentals of Programming CS-110. Lecture 2

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Exercise: Using Numbers

Connecting with Computer Science, 2e. Chapter 15 Programming II

Lab # 02. Basic Elements of C++ _ Part1

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

UNIT- 3 Introduction to C++

CSc Introduction to Computing

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Fundamentals of Programming. Budditha Hettige Department of Computer Science

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Tokens, Expressions and Control Structures

Lecture 2 Tao Wang 1

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Full file at

Integer Data Types. Data Type. Data Types. int, short int, long int

WARM UP LESSONS BARE BASICS

C++ For Science and Engineering Lecture 15

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

C: How to Program. Week /Mar/05

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Getting started with C++ (Part 2)

Exception with arguments

Object Oriented Programming 2012

2.1-First.cpp #include <iostream> // contains information to support input / output using namespace std;

Objectives. In this chapter, you will:

Chapter 2 - Introduction to C Programming

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Programming in C++ 6. Floating point data types

BSM540 Basics of C Language

Chapter 7. Additional Control Structures

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

BITG 1233: Introduction to C++

Fundamentals of Programming

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Week 3 Lecture 2. Types Constants and Variables

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

C# MOCK TEST C# MOCK TEST I

Peer Instruction 1. Elementary Programming

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

BASIC ELEMENTS OF A COMPUTER PROGRAM

Homework Assignment #1

Programming. C++ Basics

Creating a C++ Program

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

CS2141 Software Development using C/C++ C++ Basics

CS149: Elements of Computer Science. Fundamental C++ objects

LECTURE 02 INTRODUCTION TO C++

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

OBJECT ORIENTED PROGRAMMING

UNIT-2 Introduction to C++

printf( Please enter another number: ); scanf( %d, &num2);

Fundamentals of Programming

Data Structures and Programming with C++

Software Design & Programming I

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Computer Programming : C++

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

First of all, it is a variable, just like other variables you studied

Lecture 3 Tao Wang 1

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Chapter-8 DATA TYPES. Introduction. Variable:

C++ Quick Guide. Advertisements

Review of Important Topics in CS1600. Functions Arrays C-strings

Introduction to Programming in Turing. Input, Output, and Variables

VARIABLES AND TYPES CITS1001

Transcription:

Fundamentals of Programming Data Types & Variables Budditha Hettige

Exercise 3.1 Write a C++ program to display the following output.

Exercise 3.2 Write a C++ program to calculate and display total amount of the given unit price and quantity of an item.

Data types

Data Types Use various variables to store various information Variables are reserved memory locations to store values When you create a variable you reserve some space in memory Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory

C++ Data Types Seven basic C++ data types

Modifiers Several of the basic types can be modified using one or more of these type modifiers signed unsigned short long

Character types char They can represent a single character, such as 'A' or '$'. The most basic type is char, which is a one-byte character.

Numerical integer types int They can store a whole number value, such as 7 or 1024.

Floating-point types float They can represent real values, such as 3.14 or 0.01

Size of a data type Use sizeof() function to check the size

Answer Result which can vary from machine to machine/ Compilers

Data type selection

Memory Allocation

Define Variables in C++ Tell the compiler where and how much to create the storage for the variable Data Type <spase> Variable Name int number; char letter; float emp_salary

Variable name( rules) Can use only letters, digits and underscore The first character must be a letter or underscore Case sensitive Cannot use keywords No limits on length

C++ Variables Better not to begin a variable name with underscore. To form a name from two or more words, separate them with underscore Example int student_age; float employe_salary; char grade ;

Local & Global Variables data_size age

Variables in a program

Exercise 3.3 Write a C++ program to calculate and display total amount of the given unit price and quantity of an item.

Answer

Type Casting A way to convert a variable from one data type to another data type Use cast operator (type_name) expression (int) float_value

C++ Memory concept Variable names correspond to location in the computer s memory Every variable has a name, a type, a size and a value A memory cell is never empty. But its initial contents may be meaningless to your program. The current contents of a memory cell are destroyed whenever new information is placed in that cell.

Input / Output

Standard Streams cin is the standard input, normally the keyboard. To input data from keyboard use the word cin, followed by the Extraction operator (>>) cin >> x; Wait for a value to be entered at the keyboard and (when enter is pressed) will put that value into variable x.

Exercise 3.4 Write a C++ program to read two numbers from keyboard and display the total.

Answer

Exercise 3.5 Create a C++ program to calculate and display total amount of given unit price and quantity of the some item.

Answer

Output To output data onto the screen, use the word cout, followed by the insertion operator (<<).

Change the output Format Adjusting field widths Use the width() member function to set the field width to display a value. cout.width(10)

Example

Floating-point notation

Example

Floating-point Precision Sets the decimal precision to be used to format floating-point values on output operations. precision(number);

Example

Exercise 3.6 Write a C++ program to read price of the 3 items and print the total

Answer

Examples 1. Write a C++ program to read 3 integer numbers and find the total and average 2. Write a C++ program which will convert a weight in KG to pounds and ounces. ( 1 Kg = 2.2046 pounds)

Example Write a C++ program which will convert a Celsius temperature into Fahrenheit