Structured Programming Slide Week2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

SJES 2271/ SJEM 2231

2/19/2013

Data
 All C++ programs consist of commands and
data

SJES 2271 : Scientific Computing 1


SJEM 2231 : Structured Programming
LECTURE NOTES (WEEK 2)
BY
DR NOOR FADIYA MOHD NOOR

 Data is processed into meaningful results


 Data comprises variables and literals
 A variable data can change as the program
runs
 A literal data remains the same

What is a variable identifier?

What are standard keywords?

 a sequence of one or more letters, digits or a


single underscore

 asm, auto, bool, break, case, catch, char, class,


const_cast, continue, default, delete, do, double,
dynamic_cast, else, enum, explicit, export,
extern, false, float, for, friend, goto, if, inline, int,
long, mutable, namespace, new, operator,
private, protected, public, register,
reinterpret_cast, return, short, signed, sizeof,
static, static_cast, struct, switch, template, this,
throw, true, try, typedef, typeid, typename,
union, unsigned, using, virtual, void, volatile,
wchar_t, while

 exclude spaces, symbols or punctuation


marks
 begin with a letter or underscore
 cannot match C++ keywords or reserved
keywords

DR NOOR FADIYA MOHD NOOR, UM

SJES 2271/ SJEM 2231

What are reserved keywords?


 and, and_eq, bitand, bitor, compl, not,
not_eq, or, or_eq, xor, xor_eq

2/19/2013

Basic Types of Data Specifier


Name

Description

Size

Range (32 bit systems)

char

Character

1 byte

signed: -128 127 / unsigned: 0-255

short

Short integer

2 bytes

signed: -32768 32767 /


unsigned: 0- 65535

int

Integer

4 bytes

signed: -2147483648 2147483647 /


unsigned: 0 4294967295

long

Long integer

4 bytes

signed: -2147483648 2147483647 /


unsigned: 0 4294967295

bool

Boolean values

1 byte

True or false

float

Floating point
number

4 bytes

+/- 3.4e +/- 38 (~ 7 digits)

double

Double precision
floating pt no.

8 bytes

+/- 1.7e +/-308 (~ 15 digits)

long
double

Long double pre. 8 bytes


floating pt. no.

+/- 1.7e +/-308 (~15 digits)

wchar_t Wide character

Variable Declaration

2@4bytes 1 wide character

Example

 A variable can be declared in 2 places:


1) Before the main function

2) Before an execution code that requires the


variables
 to specify data type of variable identifiers so
that values can be assigned to them later
 i.e int a; float k; unsigned NumberofStudent;

DR NOOR FADIYA MOHD NOOR, UM

SJES 2271/ SJEM 2231

2/19/2013

Assigning Values to Variables

Assigning Values to Variables

 by means of assignment statement

 by means of assignment statement

 variable = expression

 variable = expression

 2 valid ways

 2 valid ways

 i.e. to assign number 7 to integer type data x

 i.e. to assign number 7 to integer type data x

1) int x=7;
2) int x (7);

What is a string?
 A variable that can store non-numerical values
more than 1 single character
 requires string.h file and access to namespace
std
 can be assigned with string value
 i.e. to assign the words I am a C++ beginner to
string type data myword

1) string myword=I am a C++ beginner;


2) string myword (I am a C++ beginner);

DR NOOR FADIYA MOHD NOOR, UM

1) int x=7;
2) int x (7);

Examples
//to assign numeric values to variables
//to assign string values to variables
#include <iostream>
using namespace std;
#include <iostream>
#include <string>
int main ()
using namespace std;
{
int x=10;
int main ()
{
int y(3);
int sum;
string Mysentence;
Mysentence=This is my first sentence
x=x+5;
cout << Mysentence << endl;
sum=x-y;
cout<< sum;
Mysentence=This is my second sentence
cout << Mysentence << \n;
return 0;
}
return 0;
}

SJES 2271/ SJEM 2231

2/19/2013

Literals

Integer Numerals

 to express fixed values within the source code of


a C++ program i.e x = 8, 8 is the literal

 constant that identify integer (int, long,


unsigned, unsigned long) values

 can be divided into 5 categories:

i.e 38, 38l, 38u, 38ul, 024, 0x1d


 can be divided into:
1) decimal numbers i.e 65
2) octal numbers i.e 017
3) hexadecimal numbers i.e 0x9c

1) integer numerals
2) floating point numerals
3) characters
4) strings
5) Boolean values

Floating Point Numbers

Character and String

 express numbers with decimals or exponent (e)

 non-numerical literals






i.e 2.7777778, 5e12, 0.11e-3, 2.0


unlike integers, can begin with a leading zero
i.e 0.71
default type is double
can also use float (f) or long double (l)
i.e 6.7805L, 0.00005123359f
valid for lower or uppercase letter of e, f and l

DR NOOR FADIYA MOHD NOOR, UM

 if numeric, the numbers are idle


 Character - enclosed in single quotation marks

i.e X, g
 String enclosed in double quotation marks
i.e. How are you?,this is my first C++ program
 Quotation marks ( , ) are to distinguish from
variable identifier, keywords and reserved
keywords

SJES 2271/ SJEM 2231

2/19/2013

Character and String

ASCII Characters

 A single character X has 1 character long


 A string X has 1 character long + a null zero in C++

1.
2.
3.

memory
Character literals include:
All alphabets i.e A
All numbers i.e 10
Special characters i.e ASCII characters /x7A

# ASCII-American Standard Code for Information


Interchange

Escape Character/Sequence

Maths On Character Data

Any character preceded by a back slash (\) i.e:

 can be performed using character data linked to


ASCII numbers

Character

Indication

\a

Alarm (terminals bell)

\b

Backspace

\n

Newline

\r

Carriage retun

\t

Tab

\v

Vertical tab

\f

Form feed for the printer

\024

Octal numbers

\x6c

Hexadecimal numbers

DR NOOR FADIYA MOHD NOOR, UM

#include <iostream>
using namespace std;
main ()
{
char f;
f = Q + 3;
cout << f;
return 0;
}

#include <iostream>
using namespace std;
main ()
{
int i;
i= K - 5;
cout << i;
return 0;
}

SJES 2271/ SJEM 2231

2/19/2013

String Literals

Boolean Literals

 can be expressed in more than a single line

 Two valid values:

i.e it is a fine day \


after a terrible yesterday
 can be separated using valid blank characters
(blank space, tabs, line etc)
i.e may this signs us
a good faith
 can be preceded with L prefix for wide characters
i.e Lthis is an example of a wide string character

1. True

Defined Constant

Declared Constant

 using preprocessor
directive #define
 placed before the
function
 does not require
semicolon (;) at the end

DR NOOR FADIYA MOHD NOOR, UM

#include <iostream>
#include <cmath>
using namespace std;
#define EXP 2.718
#define NewAlarm \a
int main ()
{
float p=pow(10, EXP);
cout << p<<NewAlarm;
return 0;
}

2. False
 Expressed in C++ as values of type bool

 declare values of a variable to be unchanged


#include <iostream>
using namespace std;
int main ()
{
const int Variable=12;
const char QuestionMark=\?;
return 0;
}

SJES 2271/ SJEM 2231

2/19/2013

What is A Header File?

Standard C++ Header Files


Input-Output Library

A standard library file included but not compiled in


the source file (.cpp) in order to:
 complete the commands in C++

 <iosfwd> forward declarations of all classes in the

input/output library

 <ios> std::ios_base class, std::basic_ios class

template and several typedefs

 speed up compiling time

 <istream> std::basic_istream class template and

 separate interface from implementation

 <ostream> std::basic_ostream class template and

 manage the source code systematically

several typedefs
several typedefs

 <iostream> std::basic_iostream class template and

several typedefs

Standard C++ Header Files


Input-Output Library
 <fstream> std::basic_fstream, std::basic_ifstream,std::basic
ofstream class templates and several typedefs
 <sstream>std::basic_stringstream, std::basic_istringstream,
std::basic_ ostringstream class templates and several
typedefs
 <strstream> std::strstream, std::istrstream, std::ostrstream
(deprecated)
 <iomanip> helper functions to control the format or input
and output
 <streambuf> std::basic_streambuf class template
 <cstdio> C-style input-output functions

DR NOOR FADIYA MOHD NOOR, UM

Standard C++ Header Files


Input-Output Stream Library



designed to perform input and output operations on


sequences of character i.e. files or strings.
provided through several related classes

SJES 2271/ SJEM 2231

2/19/2013

Standard C++ Header Files


Numerics Library
 <cmath> common mathematics functions
 <complex> complex number type
 <valarray> class for representing and manipulating





arrays of values
<random> random number generators and distributions
<numeric> numeric operations on values in containers
<ratio> (since C++11) compile-time rational arithmetic
<cfenv> (since C++11) floating-point environment access
functions

Standard C++ Header Files


Strings Library
 <cctype> functions to determine the type contained in
character data
 <cwctype> functions to determine the type of wide
character data
 <cstring>various narrow character string handling functions
 <cwstring>various wide character string handling functions
 <cwchar>various wide and multibyte string handling
functions
 <cuchar> (since C++11)C-style Unicode character conversion
functions
 <string>std::basic_string class template

Standard C++ Header Files

Standard C++ Header Files

Utilities Library

Utilities Library

 <cstdlib> program control, dynamic memory allocation,

 <bitset> std::bitset class template


 <functional> function objects designed for use with








random numbers, sort and search


<csignal> functions and macro constants for signal
management
<csetjmp> macro and function that saves and jumps to an
execution context
<cstdarg> handling of variable length argument lists
<typeinfo> runtime type information utilities
<typeindex> (since C++11) std::type_index
<type_traits> (since C++11) Compile-time type information

DR NOOR FADIYA MOHD NOOR, UM

the standard algorithms


<utility> various utility components
<ctime> C-style time/date utilities
<chrono> (since C++11) C++ time utilities
<cstddef> typedefs for types such as size_t, NULL, others
<initializer_list> (since C++11) std::initializer_list class
template
 <tuple> (since C++11) std::tuple class template






You might also like