• 검색 결과가 없습니다.

Ch. 3 Dealing with Data C++ Programming

N/A
N/A
Protected

Academic year: 2022

Share "Ch. 3 Dealing with Data C++ Programming"

Copied!
27
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

C++ Programming

Ch. 3 Dealing with Data

Lecture Note of Digital Computer Concept and Practice

Spring 2014

Myung-Il Roh

Department of Naval Architecture and Ocean Engineering Seoul National University

(2)

Ch. 3 Dealing with Data

(3)

Contents

þ Review þ Variables

þ C++ Built-in Types

n Integer Variables n Character Variables

n Floating-Point Variables

þ C++ Arithmetic Operators þ Type Conversions

þ Summary

þ Practice

(4)

Review

þ Structure of the C++ Program þ Programming Practice

n A program that gets 2 variables, adds variables, and shows its result

(5)

Variables (1/3)

þ A symbolic name or identifier for the data storage in a program þ All variables must be declared before it is used.

þ Ex.

int x, y; // declare integer variables x and y x = 3; // assign a value to the variable x y = x + 5; // assign a value to the variable y

(6)

Variables (2/3)

þ Names for Variables

n The only characters you can use in names are alphabetic characters, numeric digits, and the underscore (_) character.

n The first character in a name cannot be a numeric digit.

n Uppercase characters are considered distinct from lowercase characters.

n You can’t use a C++ keyword for a name.

n Names beginning with two underscore characters or with an

underscore character followed by an uppercase letter are reserved for use by the implementation, that is, the compiler and the resources it uses. Names beginning with a single underscore character are

reserved for use as global identifiers by the implementation.

l Caution! It doesn’t produce compile error. However, it can lead to undefined behavior.

l Ex. __time_stop, _Donut

n C++ places no limits on the length of a name, and all characters in a name are significant.

(7)

Variables (3/3)

þ Quiz about Names for Variables

n Put O if it’s possible to use as a variable name, or put X if it’s impossible.

l Poodle ( )

l I_am_going_to_use_this_as_my_fisrt_variable ( )

l X5 ( )

l 3x ( )

l __AB ( )

l _Com ( )

l R2D2 ( )

l int ( )

l double ( )

l hello-there ( ) l GoodMorning ( ) l My variable ( )

(8)

C++ Built-in Types

þ Fundamental Types

n bool, char, short, int, long, float, double, long double n Integer type: char, short, int, long

l There are signed type and unsigned type for the integer variables.

n Floating-point type: float, double, long double

þ Compound Types

n Array: Data form that can hold several values, all of one type n Structure

l Data form that can hold several values of differing types

l Collection of variables under a single name with various types

n String: Array of char type variables

(9)

Integer Variables

þ Integer (‘int’) Types

n Types of integer variable: bool, char, short, int, long (ascending order in data size)

n There are two kind of integers; signed and unsigned integer.

l short, unsigned short, int, unsigned int, long, unsigned long (ascending order in data size)

n Width of short, int, and long type

l short: at least 16 bits wide

– Range of 16 bit signed integer: -215 ~ 215-1

l int: at least as big as short

l long: at least 32 bits wide and at least as big as int

– Range of 32 bit signed integer: -231 ~ 231-1

n Overflow error

l Runtime error that the value exceeds its limit.

(10)

Size of Data Types

þ Calculation of the Size of Data Types

n ‘sizeof()’ operator: Return the size of a value or data type in byte.

n To use this operator, the ‘climits’ header file should be included.

n Ex.

……..

#include <climits>

int main(void) {

int n_int = INT_MAX;

cout<<“The size of int is ”<< sizeof(int) <<“bits.\n”;

cout<<“int: “<< n_int <<“\n”;

return 0;

}

(11)

Symbolic Constants in ‘climits’ Header File

Symbolic constant Represents

CHAR_BIT CHAR_MAX

CHAR_MIN SCHAR_MAX

SCHAR_MIN UCHAR_MAX

SHRT_MAX SHRT_MIN USHRT_MAX

INT_MAX INT_MIN UINT_MAX LONG_MAX

LONG_MIN ULONG_MAX

Number of bits in a char Maximum char value Minimum char value

Maximum signed char value Minimum signed char value Maximum unsigned char value Maximum short value

Minimum short value

Maximum unsigned short value Maximum int value

Minimum int value

Maximum unsigned int value Maximum long value

Minimum long value

Maximum unsigned long value

(12)

Character Variables

þ Character (‘char’) Types

n Data type for the storage of a character

n 1 byte (= 8 bits) is enough to represent all symbols because the number of symbols are less than 28 (= 256).

n The char type could be used as an integer type that is typically smaller than short type.

n The way to write a character constant

l Enclose the character within two single quotation mark like ‘a’.

l ‘a’ ≠ “a” // ’a’ is a char, “a” is an array of char (string) l Escape sequence codes

Character Name ASCII Symbol C++

code

ASCII Decimal Code

ASCII Hex Code

Newline

Horizontal Tab Backspace Double quote

NL(LF) HT BS

\n

\t

\b

\”

10 9 8 34

0xA 0x9 0x8 0x22

(13)

The const Qualifier

þ The keyword for declaration and initialization of a symbolic constant

þ After setting with the const qualifier, we can not change subsequently the value.

þ Similar with ‘#define’ keyword in C language þ Purpose of use

n Specify the type explicitly.

n We can use C++ scoping rules to limit the definition to particular functions or files.

þ Expression

n const datatype name of value = initial value;

n Ex.

const int MONTHS = 12; // MONTHS becomes a constant as 12

(14)

Floating-Point Variables (Real Numbers)

þ Float-Point Types

n Types of float-point variable: float, double, long double (order in data size ascending)

n Purpose of use

l To represent real numbers

l To represent very small or large number

n With of significant figures (in general)

l float: at least 32 bits (4 bytes) wide

l double: at least 48 bits, typically 64 bits (8 bytes) wide

l long double: at least larger than double. 80, 96, 128 bits wide

n Range of exponents

l At least -37 to +37

n The way to find the limits for the system

l the ‘cfloat’ or ‘cloat.h’ header file should be included.

(15)

Floating-Point Constants

þ Example of Float-Point Constants

n 1.234f // float constant

n 2.34E20f // float constant

n 2.3455235E28 // double constant

n 2.2L // long double constant

þ Caution of Declaration

n A warning occurred when declaring a real constant with ‘const float’.

n Ex.

const float PI = 3.1415 // Warning const float PI = 3.1415f // OK

const double PI = 3.1415 // OK

(16)

C++ Arithmetic Operators

þ Basic Arithmetic Operator

n Operator between 2 values (operands) n +: Addition

n -: Subtraction n *: Multiplication n /: Division

n %: Taking the modulus

þ Order of Operation

n Same as normal arithmetic operation n ‘( )’ is the first.

n Ex. x = 3 + 4 * 2 – 3 / 3; // What is the value of x?

(17)

Type Conversions (1/3)

þ Type Conversions

n Automatic type conversion vs. Forced type conversion (type casts)

þ Automatic Type Conversion

n C++ makes many type conversions automatically to match types to the need.

n Cases of automatic type conversion

l Conversion on assignment: When you assign a value of one arithmetic type to a variable of another arithmetic type

– Ex. int y = 3.5; // The variable y stores 3 instead of 3.5.

l Conversions in expressions: When you combine mixed types in expressions

– Integral promotions

» bool, char, unsigned char, signed char, and short Æ converted to int

– When an operation involves two types, the smaller is converted to the larger.

l Conversions in passing arguments: When you pass arguments to function

– C++ function prototyping controls type conversions for the passing of arguments.

(18)

Type Conversions (2/3)

þ Automatic Type Conversion (continued)

n Ex.

float x, z;

int y;

x = y; // assign a value to the different type variable z = 2.0 / 3 * 3; // varies of types in one statement

cal(2, 3); // if the function prototype is declared as void cal(float, float);

n Potential numeric conversion problems

Conversion Type Potential Problems

Bigger floating-point type to

smaller floating-point type such as double to float

Loss of precision (significant figures); value might be out of range for target type, in which case result is undefined.

Floating-point type to integer type Loss of fractional part; original value might be out of range for target type, in which case result is undefined.

Bigger integer type to smaller Original value might be out of range for target type;

(19)

Type Conversions (3/3)

þ Forced Type Conversions (Type Casts)

n C++ empowers you to force type conversions explicitly via type cast mechanism.

n Type cast operator

l Expression

(type name) value // old C syntax type name (value) // new C++ syntax

n Ex. These statements add two values into int type.

coots = int(19.99) + int(11.99); // new C++ syntax cout << “coots = “<< coots << ‘\n’;

char ch = ‘Z’;

cout << “The code for “ << ch << “ is “; // print ‘ch’ with char type cout << int(ch) << ‘\n’; // print ‘ch’ with int type

(20)

Summary (1/2)

þ C++’s basic types fall into two groups. One group consists of values that are stored as integers. The second group consists of values that are stored in floating-point format.

þ The integer types differ from each other in the amount of memory used to store values and in whether they are signed or unsigned.

From smallest to largest, the integer types are bool, char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, and unsigned long.

þ The floating-point types can represent fractional values and values

much larger than integers can represent. The three floating-point

types are float, double, and long double.

(21)

Summary (2/2)

þ By providing a variety of types in different sizes and in both signed and unsigned varieties, C++ lets you match the type to particular data requirements.

þ C++ uses operators to provide the usual arithmetical support for numeric types: addition, subtraction, multiplication, division, and taking the modulus.

þ C++ converts values from one type to another when you assign

values to a variable, mix types in arithmetic, and use type casts to

force type conversions.

(22)

þ Make a program

that get a radius of a circle from the user and calculate the area and the circumference of the circle.

n Define  (Pi) as a constant 3.14159 using the const qualifier.

n What will happen if you declare a variable a and p with int type?

Practice

Preprocessor directives Define global variable PI.

int main(void) {

declare float type variables r,a,p.

get the radius from user and store the value at r.

calculate the circumference and store the value at p.

calculate the area and store the value at a.

print out the radius, circumference, and area.

return 0;

}

(23)

Reference Slides

(24)

[Reference] Representation of Negative Integers

þ 2’s Complement Method

Ex.

Assume that a computer represents a integer with 4 bits. Then, the range of the number that can be represented is between -23 ~ 23-1.

Dec 5 → 0101

Dec -5 → 1101 ? (X) 1011 (O) The way to represent a negative integer.

Dec 5 Æ Bin 0101

1010 (1’s complement) + 1

1011 (2’s complement)

The computer performs addition through XOR operation and subtraction through adding the negative number. For example, (7 – 5) is (7 + (-5)).

0111 + 1011

(25)

[Reference] ASCII Code Table

Char Hex Oct Dec Char Hex Oct Dec Char Hex Oct Dec Char Hex Oct Dec

Ctrl-@ NUL 00 000 0 Space 20 040 32 @ 40 100 64 ` 60 140 96

Ctrl-A SOH 01 001 1 ! 21 041 33 A 41 101 65 a 61 141 97

Ctrl-B STX 02 002 2 " 22 042 34 B 42 102 66 b 62 142 98

Ctrl-C ETX 03 003 3 # 23 043 35 C 43 103 67 c 63 143 99

Ctrl-D EOT 04 004 4 $ 24 044 36 D 44 104 68 d 64 144 100

Ctrl-E ENQ 05 005 5 % 25 045 37 E 45 105 69 e 65 145 101

Ctrl-F ACK 06 006 6 & 26 046 38 F 46 106 70 f 66 146 102

Ctrl-G BEL 07 007 7 ' 27 047 39 G 47 107 71 g 67 147 103

Ctrl-H BS 08 010 8 ( 28 050 40 H 48 110 72 h 68 150 104

Ctrl-I HT 09 011 9 ) 29 051 41 I 49 111 73 i 69 151 105

Ctrl-J LF 0A 012 10 * 2A 052 42 J 4A 112 74 j 6A 152 106

Ctrl-K VT 0B 013 11 + 2B 053 43 K 4B 113 75 k 6B 153 107

Ctrl-L FF 0C 014 12 , 2C 054 44 L 4C 114 76 l 6C 154 108

Ctrl-M CR 0D 015 13 - 2D 055 45 M 4D 115 77 m 6D 155 109

Ctrl-N SO 0E 016 14 . 2E 056 46 N 4E 116 78 n 6E 156 110

Ctrl-O SI 0F 017 15 / 2F 057 47 O 4F 117 79 o 6F 157 111

Ctrl-P DLE 10 020 16 0 30 060 48 P 50 120 80 p 70 160 112

Ctrl-Q DCI 11 021 17 1 31 061 49 Q 51 121 81 q 71 161 113

Ctrl-R DC2 12 022 18 2 32 062 50 R 52 122 82 r 72 162 114

Ctrl-S DC3 13 023 19 3 33 063 51 S 53 123 83 s 73 163 115

Ctrl-T DC4 14 024 20 4 34 064 52 T 54 124 84 t 74 164 116

Ctrl-U NAK 15 025 21 5 35 065 53 U 55 125 85 u 75 165 117

Ctrl-V SYN 16 026 22 6 36 066 54 V 56 126 86 v 76 166 118

Ctrl-W ETB 17 027 23 7 37 067 55 W 57 127 87 w 77 167 119

Ctrl-X CAN 18 030 24 8 38 070 56 X 58 130 88 x 78 170 120

Ctrl-Y EM 19 031 25 9 39 071 57 Y 59 131 89 y 79 171 121

Ctrl-Z SUB 1A 032 26 : 3A 072 58 Z 5A 132 90 z 7A 172 122

Ctrl-[ ESC 1B 033 27 ; 3B 073 59 [ 5B 133 91 { 7B 173 123

Ctrl-\ FS 1C 034 28 < 3C 074 60 \ 5C 134 92 | 7C 174 124

Ctrl-] GS 1D 035 29 = 3D 075 61 ] 5D 135 93 } 7D 175 125

(26)

[Reference] 32-bit vs. 64-bit Computer (1/2)

þ The size of the data that is transmitted and received at once

n If the size of data that is transmitted and received is 32 bits, then it is 32-bit computer, if the size of the data is 64 bits, then 64-bit

computer.

þ Capacity of the CPU Processing Power

n 32-bit and 64-bit refer to the way a computer's processor (also called a CPU) handles information at once.

n The 64-bit computer handles large amounts of random access memory (RAM) more effectively than a 32-bit system.

(27)

[Reference] 32-bit vs. 64-bit Computer (2/2)

þ If there is a 4-bit computer (which uses 4-bit memory address) with 1GB memory, the size of usable memory is only 16 bytes.

Thus, the other memories are all losses.

þ 32-bit computer uses 4-byte memory address (size of the pointer), and 64-bit computer uses 8-byte memory address. Thus, there is the maximum capacity of the memory depend on the system.

n 4-bit: 24 = 16 bytes memory is the maximum.

n 32-bit: 232 = 4GB memory is the maximum.

참조

관련 문서

[r]

indicated input stream fgets( char *s, int n, FILE *) Address of the string/NULL pointer. fputs() Writes the character string pointed to

► Excess enthalpy data plays an important role in chemical engineering process design and operation.... Types

Baseline characteristics of genotype 1b chronic hepatitis C patients who treated with daclatasvir and asunaprevir (n=231)... Characteristics of genotype 1b

indicated input stream fgets( char *s, int n, FILE *) Address of the string/NULL pointer. fputs() Writes the character string pointed to

Global structural analysis model of the 300K VLCC by using the developed method. (Shell element, 1-D

select customer name borrower loan number as loan id amount select customer-name, borrower.loan-number as loan-id, amount from borrower, loan.. where borrower lo n number = lo n

 Data stream consists of a universe of elements chosen from a set of size N.  Maintain a count of the number of distinct elements