• No results found

Programming Fundamentals

N/A
N/A
Protected

Academic year: 2020

Share "Programming Fundamentals"

Copied!
56
0
0

Loading.... (view fulltext now)

Full text

(1)

Programming

Fundamentals

(2)

Overview of Previous Lecture

● Phases of C++ Environment

● Program statement Vs Preprocessor

directive

● Whitespaces ● Comments

(3)

Today’s Lecture

● CPU and Memory ● Variable Types

● Integer Variables ● Character Variables ● Floating Point Types ● Input with cin

(4)

CPU and Memory

● A computer acts on data according to the

instructions specified in a program (like our

previous C++ program) and produces the output.

● This computation is done by Central Processing

Unit(CPU) of the computer.

● A CPU of computer performs arithmetic

operations such as addition, subtraction,

multiplication and division and also performs logical operation on the data such as comparing two numbers.

(5)

CPU and Memory

● Memory of a computer is the area where data

and programs are stored.

● A computer memory is made up of cells that is

capable of holding information in the form of binary digits 0 and 1 (bits).

● This is the way information is stored in a

memory just like we memorize numbers using 0 to 9.

(6)

CPU and Memory

● Collection of 8 bits is called a byte.

● Normally the CPU does not accessed memory by

individual bits.

● Instead, it accesses data in a collection of bits,

typically 8 bits, 16 bit, 32 bit or 64 bit.

● The amount if bits on which it can operate

simultaneously is known as the word length of the computer.

(7)

CPU and Memory

● When we say that Pentium 4 is a 32 bit machine,

it means that it simultaneously operates on 32 bit of data.

(8)

Program memory

● When the program is compiled and linked,

different parts of the program is organized in separate segments.

● Our code will be in one segment.

● Code means the instructions to be executed ● That segment is called as code segment or

(9)

Data segment and Stack memory

● Then there is data on which the code

operates, this data get stored in a segment called Data segment.

Stack memory is a part of programs memory

(10)

Program Memory

● The three types of memory specified above are

owned by the corresponding process or program.

● The linker will give information about where

to store which data to the loader

● Based on these information loader will load the

corresponding executable(.exe in our case)in the memory.

(11)

Heap memory

● Heap memory is the memory which is owned

by the process when a process requests for extra memory.

● These requests are done by the malloc and

(12)

Program vs Process

● A program needs memory when it gets

executed and a program under execution is a process.

(13)

CPU and Memory

● Data is stored in the memory at physical

memory locations.

● These locations are known as the memory

address.

● It is very difficult for humans to remember

the memory addresses in numbers like 46735, so instead we name a memory address by a variable.

(14)

Variables

● Variables are the most fundamental part of any

language.

● A variable has a symbolic name and can be given

a variety of values.

● Variables are located in particular places in the

computer’s memory.

● When a variable is given a value, that value is

actually placed in the memory space assigned to the variable.

(15)

Variable types

● Most popular languages use the same

general variable types, such as integers, floating-point numbers, and characters

(16)

Integer variables

● Integer variables represent integer numbers like 1,

30,000, and –27.

● Such numbers are used for counting discrete

numbers of objects, like 11 pencils or 99 bottles of beer.

● Unlike floating point numbers, integers have no

fractional part; you can express the idea of four

(17)

Contdd . . . .

● The amount of memory occupied by the integer

types is system dependent.

● On a 32-bit system such as Windows, an int

occupies 4 bytes (which is 32 bits) of memory.

● This allows an int to hold numbers in the range

(18)

OUTPU

T

(19)
(20)
(21)

Here’s a program that defines and uses several variables of type int:

(22)

Explanation

● The statements

int var1; int var2;

define two integer variables, var1 and var2

The keyword int signals the type of variable.

● These statements, which are called declarations,

must terminate with a semicolon, like other program statements.

(23)

Contdd . . . .

● You must declare a variable before using it. ● However, you can place variable declarations

(24)

Declarations and Definitions

A declaration introduces a variable’s name

(such as var1) into a program and specifies its type (such as int).

● However, if a declaration also sets aside

memory for the variable, it is also called a

(25)

Variable Names

● The names given to variables (and

other program features) are called identifiers. What are the rules for writing identifiers?

● You can use upper- and lowercase letters, and

the digits from 1 to 9.

● You can also use the underscore (_).

(26)

Contdd . . . .

● Identifiers can be as long as you like, but most

compilers will only recognize the first few hundred characters.

● The compiler distinguishes between upper- and

lowercase letters, so Var is not the same as var or VAR.

● You can’t use a C++ keyword as a variable

(27)

Keyword

● A keyword is a predefined word with a special

meaning.

● int, class, if, and while are examples of

(28)
(29)

Assignment Statements

● The statements

assign values to the two variables.

● The equal sign (=) causes the value on the right

(30)

Integer Constants

The number 20 is an integer constant.

● Constants don’t change during the course of the

program.

● An integer constant consists of numerical digits. ● There must be no decimal point in an integer

constant, and it must lie within the range of integers.

(31)
(32)

Output Variations

● The statement

displays a string constant. The next statement displays the value of the variable var2.

(33)

The endl Manipulator

● This causes a linefeed to be inserted into the

stream, so that subsequent text is displayed on the next line.

● It has the same effect as sending the ‘\n’

character.

(34)

Manipulators

● Manipulators are instructions to the output

stream that modify the output in various ways; we’ll see more of them as we go along.

(35)

Other Integer Types

● There are several numerical integer types besides

type int.

● The two most common types are long and short. ● Type char is an integer type as well.

(36)

Character Variables

● Type char stores integers that range in value from

–128 to 127.

● Variables of this type occupy only 1 byte (eight

bits) of memory.

● Character variables are sometimes used to store

numbers that confine themselves to this limited range, but they are much more commonly used to store ASCII characters.

(37)
(38)

● As you may already know, the ASCII character

set is a way of representing characters such as ‘a’, ‘B’, ‘$’, ‘3’, and so on, as numbers.

(39)

Character Constants

● Character constants use single quotation marks

around a character, like ‘a’ and ‘b’.

● Note: This differ from string constants, which use

double quotation marks.

● When the C++ compiler encounters such a

character constant, it translates it into the

corresponding ASCII code. The constant ‘a’ appearing in a program, for example, will be translated into 97

(40)

● Character variables can be assigned

(41)

Initialization

● Variables can be initialized at the same time

they are defined. In this program two variables of type char—charvar1 and charvar2—are

(42)
(43)

Escape Sequences

● Since the backslash, the single quotation marks,

and the double quotation marks all have

specialized meanings when used in constants, they must be represented by escape sequences when

(44)

Example

● Here’s an example of a quoted phrase in a string

(45)

Floating Point Types

● Floating-point variables represent numbers with

a decimal place—like 3.1415927, 0.0000625,and –10.2.

● They have both an integer part, to the left of the

(46)

Kinds of floating-point variables

● There are three kinds of floating-point variables

in C++: type float, type double, and type long double.

(47)

Type float

● Type float stores numbers in the range of about

3.4x10–38 to 3.4x1038.

(48)
(49)
(50)

Type double and long double

● The larger floating point types, double and long

double, are similar to float except that they require more memory space and provide a wider range of values and more precision.

● Type double requires 8 bytes of storage and

handles numbers in the range from 1.7x10–308 to 1.7x10308. Type long double is

compiler-dependent but is often the same as double.

(51)

Floating-Point Constants

● The number 3.14159F is an example of a

floating-point constant.

● The decimal point signals that it is a floating-point

constant, and not an integer, and the F specifies that it’s type float, rather than double or long double.

● The number is written in normal decimal notation.

You don’t need a suffix letter with constants of type double; it’s the default. With type long

(52)

Contdd . . . .

● You can also write floating-point constants

using exponential notation.

● Exponential notation is a way of writing large

numbers without having to write out a lot of zeros.

● For example,1,000,000,000 can be written as

(53)
(54)

● The statement

causes the program to wait for the user to type in a number.

● The resulting number is placed in the variable

ftemp.

(55)

Input with cin

● The keyword cin (pronounced “C in”) is an

object, predefined in C++ to correspond to the standard input stream.

● This stream represents data coming from the

keyboard.

● The >> is the extraction or get from operator.

● It takes the value from the stream object on its left

(56)

References

Related documents

Again… deep vascular loss – not typical of glaucoma.. Glaucoma as a diagnosis of exclusion. ▪ Glaucoma defined as a disease of the

This whitepaper compares the three year total cost of ownership of a high availability Microsoft Exchange 2010 mailbox between an Australian specialist-provider hosted model and

Variable declaration int i,j,k; long p,q; short s; unsigned u; float r; double dr; long double lr; char c, text[10];. MA511:

The BDW Model now contains the data structures needed by a financial institution to support the approaches for Credit Risk, Operational Risk, as well as the data structures needed

void nag_binary_con_greeks (Nag_OrderType order, Nag_CallPut option, Integer m, Integer n, const double x[], double s, double k, const double t[], double sigma, double r, double

short signed 16-bit integer int signed 32-bit integer long signed 64-bit integer. float 32-bit floating point (IEEE 754-1985) double 64-bit floating point

void nag_2d_cheb_eval (Integer mfirst, Integer mlast, Integer k, Integer l, const double x[], double xmin, double xmax, double y, double ymin, double ymax, double ff[], const

METHODS : We trained and evaluated new deep learning models to automate the detection, segmentation, and classification of four reproductive structures of Streptanthus tortuosus