DATA STRUCTURE : DAT 20104 JENIS DATA DAN JENIS DATA ABSTRAK (4JAM)

Slides:



Advertisements
Similar presentations
Chapter 10.
Advertisements

CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 2: Introduction to C++.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Chapter 8 Arrays and Strings
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
CSCI-383 Object-Oriented Programming & Design Lecture 5.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CSCI 383 Object-Oriented Programming & Design Lecture 6 Martin van Bommel.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
C++ Classes and Data Structures Jeffrey S. Childs
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays.
Module 1: Array ITEI222 - Advance Programming Language.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Constants, Data Types and Variables
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
1 C++ Classes and Data Structures Course link…..
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
BASIC ELEMENTS OF A COMPUTER PROGRAM
Programming Fundamentals
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays Arrays exist in almost every computer language.
Basic Elements of C++.
The Selection Structure
Lecture 6 C++ Programming
Chapter 2: Introduction to C++
DATA HANDLING.
Basic Elements of C++ Chapter 2.
Arrays, For loop While loop Do while loop
7 Arrays.
2.1 Parts of a C++ Program.
Arrays Kingdom of Saudi Arabia
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
7 Arrays.
C++ Pointers and Strings
C++ Programming Lecture 3 C++ Basics – Part I
Fundamental Programming
Arrays Arrays A few types Structures of related data items
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Basics
Lecture 2 Arrays & Pointers September 7, 2004
C++ Pointers and Strings
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

DATA STRUCTURE : DAT 20104 JENIS DATA DAN JENIS DATA ABSTRAK (4JAM) Jenis Data.(DATA TYPE) Tatasusunan.(ARRAY) Penuding.?(POINTER) Jenis Data Abstrak.(DATA ABSTRACTION) Objek.(OBJECT) Kelas.(CLASS) Bahasa Pengaturcaraan Sokongan untuk Jenis Data Abstrak.(PROGRAMMING LANGUAGE IN DATA ABSTRACTION)

Constants DATA STRUCTURE : DAT 20104 A constant is the quantity that does not change. This quantity can be stored at a locations in the memory of the computer. A variable can be consider as a name given to the location in memory where this constant is stored. Three Basic Types of Constant Integer Constants Real Constants String Constants

Integer Constant DATA STRUCTURE : DAT 20104 Rules for Constructing Integer Constant An integer constant must have at least one digit. It must not have a decimal point. It could be either negative or positive. If no sign precedes an integer constant it is assumed to be positive. No commas or blanks are allowed in integer constant. Example 426 -777 +20000

Floating Point Constant / Real Constants DATA STRUCTURE : DAT 20104 Floating Point Constant / Real Constants Rules for Constructing Real Constant A real constant must have at least one digit. It must have a decimal point. It could be either positive or negative Default sign is always positive. No commas or blanks are allowed in real constant. Example +300.25 221.005 -19845.0

Character Constant DATA STRUCTURE : DAT 20104 Rules for Constructing Character Constants A character constant is either a single alphabet, a single digit or a single special symbol enclosed within Single inverted commas. The maximum length of a character constant can be 1 character. Example ’a’ ’5’ ’=’ ’G’

Data Types DATA STRUCTURE : DAT 20104 Built-in Data Type A computer Program operates on data and produce an output. In C++, each data must be of specific data type. The data type determines how the data is represented in the computer and kind of processing the computer can perform on it. There are two kind of data types. Built-in data type User define data type Built-in Data Type The are already defined by C++. int ( For working with integer numbers) float (For working with real numbers having decimal points) char ( for working with character data)

Variables DATA STRUCTURE : DAT 20104 Variable is a location in memory, referenced by an identifier, that contain a data value that can be changed. Identifier: name given to a variable is known as identifier. Rules for writing Identifier The first character must be letter or underscore ( _ ). You can use upper and lowercase letters and digits from 1 to 9. Identifier can be as long as you like but only the first 250 character are recognizable in C++ Compiler.

Integer Variables DATA STRUCTURE : DAT 20104 Integer variables represent integer numbers like 1, 30,000 and -45. Integer variables do not store real numbers. Defining integer variables The amount of memory occupied by integer types is system dependent. On 32-bit system like windows 98/XP an integer occupies 4 bytes of memory. This allows an integer variable to hold numbers in the range from -2,147,483,648 to 2,147,483,647.

Integer Variables Example DATA STRUCTURE : DAT 20104 Integer Variables Example #include<iostream.h> void main ( ) { int var1; //define var1 int var2, var3; //define var2, var3 var1 = 20; //assign value to var1 var2 = var1 + 10; //assign value to var2 cout<<“Result =”; cout<<var2<< endl; //displaying the sum of var1 + 10 }

Character Variables DATA STRUCTURE : DAT 20104 Type char stores integer that range in value from -128 to 127. Variables of this type occupy only 1 byte of memory. Character variables are sometime use to store number but they are much more commonly used to store ASCII characters. Character Constants Character constants use single quotation marks around a character, like ‘a’ and ‘b’. When the C++ compiler encounters such a character constant, it translates it into the corresponding ASCII code. 10

Character Variable Example DATA STRUCTURE : DAT 20104 Character Variable Example #include<iostream.h> void main ( ) { char charvar1 = ‘A’; //define char variable char charvar2 = ‘\t’; cout << charvar1; // display character cout << charvar2; charvar1 = ‘B’; //reassigning charvar1 to B cout << charvar1; }

DATA STRUCTURE : DAT 20104 Floating point Types Floating point variables represent number with a decimal place e.g. 3.14787 or 10.2 There are three kind of floating point variables in C++ float double long double

float Type float stores numbers in the range of about to . DATA STRUCTURE : DAT 20104 float Type float stores numbers in the range of about to . It occupy 4 bytes of memory. Double and Long Double: 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.

float DATA STRUCTURE : DAT 20104 #include<iostream.h> void main ( ) { float rad, area; float PI =3.14; cout << “Enter radius of circle”; cin >> rad; area = PI * rad * rad; cout <<“Area is” << area << endl; }

Variable Type Summary DATA STRUCTURE : DAT 20104 Bytes of Memory Digits of precision High range Low Range Keyword 1 n/a 127 -128 Char 2 32,767 -32,768 short 4 2,147,483,647 -2,147,483,648 int long 7 float 8 15 double

DATA STRUCTURE : DAT 20104 Arrays

DATA STRUCTURE : DAT 20104 What is Array An Array is a structured collection of components, all of same type, that is given a single name. Each component (array element) is accessed by an index that indicates the component’s position within the collection.

DATA STRUCTURE : DAT 20104 Defining Array Like other variables in C++, an array must be defined before it can be used to store information. Like other definitions, an array definition specifies a variable type and a name. But it includes another feature i.e. size. DataType ArrayName [Const Int Expression ];

Array Elements The items in an array are called array elements. DATA STRUCTURE : DAT 20104 Array Elements The items in an array are called array elements. All elements in an array are of the same type; only the values vary. Example int array1[4] = { 10, 5, 678, -400 } ;

Accessing Array Elements DATA STRUCTURE : DAT 20104 Accessing Array Elements To Access an individual array component, we write the array name, followed by an expression enclosed in square brackets. The expression specifies which component to access. Syntax ArrayName [ IndexExpression] Example array1[2] array1[i] where i = 3

Initializing array in Declarations To initialize an array, you have to specify a list of initial values for the array elements, separate them with commas and enclose the list within braces. int array1[5] = {23, 10, 16, 37, 12}; We don’t need to use the array size when we initialize all the array elements, since the compiler can figure it out by counting the initializing variables. int array1[ ] = { 23, 10, 16, 37}; What happens if you do use an explicit array size, but it doesn’t agree with the number of components ? If there are too few components/ items , the missing element will be set to zero. If there are two many, an error is signaled.

Lack of Aggregate Array Operations C++ does not allow aggregate operations on arrays. int x[50], y[50] ; There is no aggregate assignment of y to x x = y; //not valid To copy array y into array x, you must do it yourself, element by element. for ( i=0; i<50; i++) x[i] = y[i]; //valid operation Similarly, there is no aggregate comparison of arrays. if (x == y ) //Not valid

Lack of Aggregate Array Operations Also, you cannot perform aggregate input / output operations on arrays. cin>>x; //not valid, where x is an array cout<<x //not valid You cannot perform aggregate arithmetic operations on arrays x = x + y // not valid, where x and y are arrays Finally, it is not possible to return an entire array as the value of a value-returning function return x; //not valid, where x is an array.

Example of One Dimensional Array void main() { double sales [6], average, total=0; cout<< “Enter sales of 6 days”; for( int j=0; j<6; j++) cin >> sales[ i ]; for (int j=0; j<6; j++) total += sales[ j ] ; average = total / 6; cout<< “Average =”<< average; }

Multidimensional Arrays A two dimensional array is used to represent items in a table with rows and columns, provided each item in the table is of same data type. Each component is accessed by a pair of indexes that represent the component’s position in each dimension.

Defining Multidimensional Array Two Dimensional Array The array is defined with two size specifiers, each enclosed in brackets DataType ArrayName[ConstIntExp][ConstIntExp] Example double array2[3][4]; Three Dimensional Array float array3[x][y][z]

Accessing Multidimensional Array Elements Array elements in two dimensional arrays required two indexes array2[1][2] Notice that each index has its own set of brackets. Don’t write commas. array2[1,2] // not valid syntax

Example Two Dimensional Array void main() { float array2[3][3]= {{ 12.2, 11.0, 9.6 }, { 23.9, -50.6, 2.3 }, { 2.2, 3.3, 4.4 } }; for (int row=0; row<3; row++) for (int col=0; col<3; col++) cout<< array2[row][col]; }

DATA STRUCTURE : DAT 20104 BYE BYE ARRAY.........

C++ Pointers and Strings DATA STRUCTURE : DAT 20104 C++ Pointers and Strings

Pointers A pointer is a variable that holds the address of something else. ... MEMORY 1 2 3 4 5 81345 81346 81347 Address ME 123 int ME; int *x; ME = 123; x = &ME; x 3

int *x; x is a pointer to an integer. You can use the integer x points to in a C++ expression like this: y = *x + 17; *x = *x +1; “the int x points to”

&ME In C++ you can get the address of a variable with the “&” operator. ... MEMORY 1 2 3 4 5 Address int ME; ME = 123; x = &ME; ME 123 &ME means “the address of ME”

Assigning a value to a dereferenced pointer A pointer must have a value before you can dereference it (follow the pointer). int *x; *x=3; int ME; int *x; x = &ME; *x=3; x doesn’t point to anything!!! ERROR!!! this is fine x points to foo

Pointers to anything int *x; int **y; double *z; x some int y some double

Pointers and Arrays An array name is basically a const pointer. You can use the [] operator with a pointer: int *x; int a[10]; x = &a[2]; for (int i=0;i<3;i++) x[i]++; x is “the address of a[2] ” x[i] is the same as a[i+2]

Pointer arithmetic Integer math operations can be used with pointers. If you increment a pointer, it will be increased by the size of whatever it points to. int *ptr = a; *(ptr+2) *(ptr+4) *ptr a[0] a[1] a[2] a[3] a[4] int a[5];

printing an array void print_array(int a[], int len) { for (int i=0;i<len;i++) cout << "[" << i << "] = " << a[i] << endl; } array version void print_array(int *a, int len) { for (int i=0;i<len;i++) cout << "[" << i << "] = " << *a++ << endl; } pointer version

Passing pointers as parameters void swap( int *x, int *y) { int tmp; tmp = *x; *x = *y; *y = tmp; }

Pointer Parameters Pointers are passed by value (the value of a pointer is the address it holds). If we change what the pointer points to the caller will see the change. If we change the pointer itself, the caller won't see the change (we get a copy of the pointer)

C++ strings A string is a null terminated array of characters. null terminated means there is a character at the end of the the array that has the value 0 (null). Pointers are often used with strings: char *msg = “RPI”; zero (null) msg 'R' 'P' 'I'

String Manipulation Functions C++ includes a library of string handling functions: char * strcpy(char *dst, const char *src) char * strcat(char *dst, const char *src) lots more!

String Example - Count the chars int count_string( char *s) { int n=0; while (*s) { n++; s++; } return(n); while the thing pointed to by s is not null increment count set s to point to the next char

Another way int count_string( char *s) { char *ptr = s; while (*ptr) { } return(ptr - s); pointer arithmetic!

Abstract Data Type DATA STRUCTURE : DAT 20104 Consider Rational a(1,2); // a = 1/2 Rational b(2,3); // b = 2/3 cout << a << " + " << b << " = " << a + b; Rational s; // s = 0/1 Rational t; // t = 0/1 cin >> s >> t; cout << s << " * " << t << " = " << s * t; Observation Natural look that is analogous to fundamental-type arithmetic objects

Classes A class is similar to a struct A class contains data members, but it also contains function members Objects are made from classes, similarly to the way that objects are made from structs The main program communicates with the objects data is passed from main program to object and from object back to the main program

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

How the Main Program Uses A Class Object The main program does not access the data within a class object The main program only accesses the functions of a class object communication occurs by passing data as parameters into the object’s function the object passes data to the main program through its return type

Main Program and Object public: functions private: data Main Program

Main Program Calls a Function in the Object public: functions private: data Main Program

The Function Accesses Data Object public: functions private: data Main Program

Function Returns a Value Back to the Main Program Object public: functions private: data Main Program

Main Program Calls a Different Function Object public: functions private: data Main Program

Function Calls Another Function Object public: functions private: data Main Program

Second Function Accesses Data Object public: functions private: data Main Program

Second Function Returns Back to First Function Object public: functions private: data Main Program

First Function Accesses Data Object public: functions private: data Main Program

Function Returns Back to Main Program Object public: functions private: data Main Program

Example of a Class 1 class Checkbook 2 { 3 public: 2 { 3 public: 4 void setBalance( float amount ); bool writeCheck( float amount ); void deposit( float amount ); 7 float getBalance( ); 8 float getLastCheck( ); 9 float getLastDeposit( ); 10 private: 11 float balance; 12 float lastCheck; 13 float lastDeposit; 14 }; This class definition is placed into its own file, called the class specification file, named checkbook.h (by convention)

Example of a Class (cont.) 1 class Checkbook 2 { 3 public: 4 void setBalance( float amount ); bool writeCheck( float amount ); void deposit( float amount ); 7 float getBalance( ); 8 float getLastCheck( ); 9 float getLastDeposit( ); 10 private: 11 float balance; 12 float lastCheck; 13 float lastDeposit; 14 }; The writeCheck function returns false if the amount of the check is greater than the balance; returns true otherwise.

Example of a Class (cont.) 1 class Checkbook 2 { 3 public: 4 void setBalance( float amount ); bool writeCheck( float amount ); void deposit( float amount ); 7 float getBalance( ); 8 float getLastCheck( ); 9 float getLastDeposit( ); 10 private: 11 float balance; 12 float lastCheck; 13 float lastDeposit; 14 }; Don’t forget the semicolon.

Example of a Class (cont.) 15 #include “checkbook.h” 16 17 void Checkbook::setBalance( float amount ) 18 { 19 balance = amount; 20 } The function definitions are placed into a separate file called the class implementation file. This file would be called checkbook.cpp (by convention).

Example of a Class (cont.) 15 #include “checkbook.h” 16 17 void Checkbook::setBalance( float amount ) 18 { 19 balance = amount; 20 } The balance variable is declared in the private section of the class definition.

Example of a Class (cont.) 15 #include “checkbook.h” 16 17 void Checkbook::setBalance( float amount ) 18 { 19 balance = amount; 20 } Special notation for class function definitions

Example of a Class (cont.) 21 bool Checkbook::writeCheck( float amount ) 22 { 23 if ( amount > balance ) 24 return false; 25 balance -= amount; 26 lastCheck = amount; 27 return true; 28 }

Example of a Class (cont.) 29 void Checkbook::deposit( float amount ) 30 { 31 balance += amount; 32 lastDeposit = amount; 33 }

Example of a Class (cont.) 34 float Checkbook::getBalance( ) 35 { 36 return balance; 37 } 38 39 float Checkbook::getLastCheck( ) 40 { 41 return lastCheck; 42 } end of checkbook.cpp

A Program that Uses the Checkbook Class 1 #include <iostream> 2 #include <iomanip> 3 #include "checkbook.h" 4 5 using namespace std; 6 7 int menu( ); 8 9 const int CHECK = 1, DEPOSIT = 2, BALANCE = 3, QUIT = 4; 10 11 int main( ) 12 { 13 Checkbook cb; 14 float balance, amount; 15 int choice; A main program that uses the Checkbook class is placed into a separate .cpp file

A Program that Uses the Checkbook Class (cont.) 16 cout << "Enter the initial balance: $"; 17 cin >> balance; 18 cb.setBalance( balance ); 19 20 cout << fixed << showpoint << setprecision( 2 );

A Program that Uses the Checkbook Class (cont.) 21 choice = menu( ); 22 while ( choice != QUIT ) { 23 if ( choice == CHECK ) { 24 cout << "Enter check amount: $"; 25 cin >> amount; 26 if ( cb.writeCheck( amount ) ) 27 cout << "Check accepted." << endl; 28 else { 29 cout << "Your balance is not high "; 30 cout << "enough for that check." << endl; 31 } 32 } body of the while loop continues

A Program that Uses the Checkbook Class (cont.) 33 else if ( choice == DEPOSIT ) { 34 cout << "Enter deposit amount: $"; 35 cin >> amount; 36 cb.deposit( amount ); 37 cout << "Deposit accepted." << endl; 38 } body of the while loop continues

A Program that Uses the Checkbook Class (cont.) 39 else { // must be a balance request 40 amount = cb.getBalance( ); 41 cout << "Your balance is: $" << amount << endl; 42 } 43 44 choice = menu( ); 45 } 46 47 return 0; 48 } 49 end of while loop

A Program that Uses the Checkbook Class (cont.) 50 int menu( ) 51 { 52 int choice; 53 54 cout << endl; 55 cout << "1 Write a check" << endl; 56 cout << "2 Make a deposit" << endl; 57 cout << "3 Get the balance" << endl; 58 cout << "4 Quit" << endl << endl; 59 cout << "Enter a number between 1 and 4: "; 60 cin >> choice; 61 return choice; 62 }

Keep In Mind The data members of a class cannot be accessed by a main program. The object always retains the current values of its data members, even when object code is no longer executing. Each function of a class can use the data members of the class as though they have been declared within the function.

DATA STRUCTURE : DAT 20104 SAYONARA CLASS....