Introduction to Data Structures & Algorithm

Slides:



Advertisements
Similar presentations
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
Advertisements

Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Chapter 1 Principles of Programming and Software Engineering.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
C++ fundamentals.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
Comp 245 Data Structures Software Engineering. What is Software Engineering? Most students obtain the problem and immediately start coding the solution.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
Introduction to Data Structures & Algorithm. Objectives: By the end of the class, students are expected to understand the following: data structure and.
Programming With C.
Prepared By Ms.R.K.Dharme Head Computer Department.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Data Structures Using C++1 Chapter 1 Software Engineering Principles and C++ Classes.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Software Engineering and Object-Oriented Design Topics: Solutions Modules Key Programming Issues Development Methods Object-Oriented Principles.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Data Structure and Algorithms
Principles of Programming. Achieving an Object-Oriented Design  Abstraction and Information Hiding  Object-Oriented Design  Functional Decomposition.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
 Problem Analysis  Coding  Debugging  Testing.
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
Maitrayee Mukerji. INPUT MEMORY PROCESS OUTPUT DATA INFO.
Mohammed I DAABO COURSE CODE: CSC 355 COURSE TITLE: Data Structures.
Introduction toData structures and Algorithms
Principles of Programming & Software Engineering
Memory Management.
Introduction to Algorithms
CSE 1342 Programming Concepts
Programming Logic and Design Seventh Edition
Data Abstraction: The Walls
Pointers and Linked Lists
Data Structure By Amee Trivedi.
Introduction to Computers Computer Generations
5.13 Recursion Recursive functions Functions that call themselves
CS 215 Final Review Ismail abumuhfouz Fall 2014.
GC211Data Structure Lecture2 Sara Alhajjam.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Principles of Programming and Software Engineering
Data Structures Data Structure is a way of collecting and organising data in such a way that we can perform operations on these data in an effective.
Data Structures Interview / VIVA Questions and Answers
About the Presentations
Cinda Heeren / Geoffrey Tien
Lecture 2 Introduction to Programming
Data Structure Interview
Introduction to Data Structure
structures and their relationships." - Linus Torvalds
Chapter 5 - Functions Outline 5.1 Introduction
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
CS 2308 Final Exam Review.
Designing and Debugging Batch and Interactive COBOL Programs
Unit# 9: Computer Program Development
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Topics Introduction to File Input and Output
Objective of This Course
Introduction to Data Structures
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Introduction to Data Structures
Programming Fundamentals (750113) Ch1. Problem Solving
Introduction to Algorithms
C programming Language
Introduction to Data Structure
Programming Fundamentals (750113) Ch1. Problem Solving
Topics Introduction to File Input and Output
structures and their relationships." - Linus Torvalds
Chapter 4: Writing and Designing a Complete Program
Presentation transcript:

Introduction to Data Structures & Algorithm

Objectives: By the end of the class, students are expected to understand the following: Concept of File processing data structure and algorithm concept key programming principle Programming development paradigm

Brief History of File Processing File processing consists of creating, storing, and/or retrieving, the contents of a file to or from a recognizable medium. For example, it is used to save word processed files to a hard drive, to store a presentation on floppy disk, or to open a file from a CD-ROM. File processing is traditionally performed using the FILE class. In the strict C sense, FILE is a structure and it is defined in the stdio.h header file. This object is equipped with variables used to indicate what operation would be performed. To use this structure, first declare an instance of the FILE structure. Example: FILE *Starter;

Continue.. File processing In C++ file processing is performed using the fstream class. Unlike the structure, fstream is a complete C++ class with constructors, a destructor and overloaded operators. To perform file processing, declare an instance of an fstream object. If the name of the file is not known during the process, use the default constructor. Unlike the FILE structure, the fstream class provides two distinct classes for file processing. One is used to write to a file and the other is used to read from a file.

Continue.. File processing When processing a file, you will typically specify the type of operation you want to perform. The operation is specified using what is referred to as a file mode. It can be one of the following: Mode Description ios::app If FileName is a new file, data is written to it. If FileName already exists and contains data, then it is opened, the compiler goes to the end of the file and adds the new data to it. ios::ate If FileName is a new file, data is written to it and subsequently added to the end of the file. If FileName  already exists and contains data, then it is opened and data is written in the current position. ios::in If FileName is a new file, then it gets created fine as an empty file. If FileName already exists, then it is opened and its content is made available for processing ios::out If FileName is a new file, then it gets created fine as an empty file. Once/Since it gets created empty, you can write data to it. If FileName already exists, then it is opened, its content is destroyed, and the file becomes as new. Therefore you can create new data to write to it. Then, if you save the file, which is the main purpose of this mode, the new content is saved it.*This operation is typically used when you want to save a file

Continue.. File processing One of the operations can be perform on a file is saving it, which is equivalent to storing its value(s) to a medium. To save a file, first declare an instance of the ofstream class using one of its constructors from the following syntaxes: ofstream(); ofstream(const char* FileName, int FileMode); The default constructor allows to initiate file processing without giving details. If using the default constructor, one of the methods can be call and will perform the necessary operation.  The ofstream(const char* FileName, int FileMode) constructor provides a complete mechanism for creating a file. It does this with the help of its two arguments. The first argument, FileName, is a string that specifies the name of the file that needs to be saved. The second argument, FileMode, specifies the kind of operation in order to perform on the file. It can be one of the modes that listed above.

Software Eng. & Problem Solving Software engineering Provides techniques to facilitate the development of computer program Problem solving The entire process of taking the statement of a problem and developing a computer program that solves that problem Requires to pass many phases, from understanding the problem, design solution and implement the solution.

Problem Solving A solution to a problem is computer program written in C++ and consist of: Modules A single, stand-alone function A method of a class A class Several functions or classes working closely together Other blocks of code

Problem Solving Challenges to create a good solution Create a good set of modules that must store, move, and alter data use algorithms to communicate with one another Organize your data collection to facilitate operations on the data in the manner that an algorithm requires

Algorithm Functions and methods implement algorithms Algorithm: a step-by-step recipe for performing a task within a finite period of time Algorithms often operate on a collection of data, which is stored in a structured way in the computer memory (Data Structure) Algorithms: Problem solving using logic

Algorithm Algorithm a sequence of instructions, often used for calculation and data processing It is formally a type of effective method in which a list of well-defined instructions for completing a task will: when given an initial state, (INPUT) proceed through a well-defined series of successive states, (PROCESS) eventually terminating in an end-state (OUTPUT)

Algorithm Initial state Series of process

Algorithm 3 types of algorithm basic control structure Sequential Selection Repeatition (Looping)

Algorithm Basic algorithm characteristics Finite solution (ada penamat) Clear instructions (jelas) Has input to start the execution Has output as the result of the execution Operate effectively ( dilaksana dengan berkesan) Algorithm creation techniques Flowchart, pseudo code, language etc Factors for measuring good algorithm Running time Total memory usage

Algorithm & Data Structure a way of STORING AND RETRIEVING data in a computer so that it can be used efficiently – (this class – RAM only) carefully chosen data structure will allow the most efficient algorithm to be used A well-designed data structure allows a variety of critical operations to be performed, Using as few resources, both execution time and memory space, as possible

Data Structure Operations to the Data Structure Traversing- access and process every data in data structure at least once Searching – search for a location of data Insertion – insert item in the list of data Deletion - delete item from a set of data Sorting – sort data in certain order Merging – merge multiple group of data

Data Types Basic data types and structured data types Basic Data Types (C++) – store only a single data Integral Boolean – bool Enumeration – enum Character - char Integer – short, int, long Floating point – float, double

Unsorted Linked List Linked Structure Structured Data Types Network Binary Tree Graph Array Structured Data Types Storage Structure Structure (struct) State Structure Stack Queue

Data Types Structured Data Types Array – can contain multiple data with the same types Struct – can contain multiple data with different type typedef struct { int age; char *name; enum {male, female} gender; } Person;

Data Types Linked Data Structure Linear Data Structure with restriction Queue & Stack Linear Data Structure with no restriction Unsorted linked list Sorted linked list Non-linear Data Structure Binary Tree Graph

Linear Data Structure with restriction Queue First-In-First-Out (FIFO) data structure the first element added to the queue will be the first one to be removed (post office, bank etc)

Queue First In First Out out in

Linear Data Structure with restriction Stack Based on the principle of Last In First Out (LIFO) Stacks are used extensively at every level of a modern computer system (compiler etc.)

Stack Stack top

Stack out in Last In First Out

Linear Data Structure with no restriction Linked list consists of a sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes

Linear Data Structure with no restriction

Linear Data Structure with no restriction Sorted linked list Data stored in ascending or descending order with no duplicates Insertion at front, middle or rear of the list Deletion will not affect the ascending / descending order of the list Unsorted linked list A linked list with no ordering

Non-linear Data Structure Binary Tree A data structure based on a tree structure A tree structure is a way of representing the hierarchical nature of a structure in a graphical form a binary tree is a tree data structure in which each node has at most two children Used for searching big amount of data

Tree Root Children / Sibling Node Link leaf

Programming Principle

Seven Key Issues in Programming Modularity Style Modifiability Ease of Use Fail-safe programming Debugging Testing

Key Issues in Programming: Modularity Modularity has a favorable impact on Constructing programs – small/large modules Debugging programs – task of debugging large programis reduced to small modular program. Reading programs- easier to understand compared to large program Modifying programs – reduce large modification by concentrating on modules Eliminating redundant code – by calling the modules will avoid the same code to be written multiple times

Key Issues in Programming: Style Use of private data members – hide data members from modules – information hiding Proper use of reference arguments – pass by value / pass by reference Proper use of methods to reduce coupling Avoidance of global variables in modules – thru encapsulation Error handling – invalid input : action to handle Readability – code easy to follow Documentation – well documented

Key Issues in Programming: Modifiability Program need to change after each iteration. Requires program to be written in a way that is easy to modify. Modifiability is easier through the use of Named constants const int number = 200; int scores[number]; The typedef statement typedef float cpaStudent; typedef long double cpaStudent;

Key Issues in Programming: Ease of Use In an interactive environment, the program should prompt the user for input in a clear manner A program should always echo its input The output should be well labeled and easy to read.

Key Issues in Programming: Fail-Safe Programming Fail-safe programs will perform reasonably no matter how anyone uses it Test for invalid input data and program logic errors Enforce preconditions Check argument values

Key Issues in Programming: Debugging Programmer must systematically check a program’s logic to find where an error occurs Tools to use while debugging: – Single-stepping – Watches – Breakpoints – cout statements – Dump functions

Key Issues in Programming: Testing Levels Unit testing: Test methods, then classes Integration testing: Test interactions among modules System testing: Test entire program Acceptance testing: Show that system complies with requirements

Key Issues in Programming: Testing Types Open-box (white-box or glass-box) testing Test knowing the implementation Test all lines of code (decision branches, etc.) Closed-box (black-box or functional) testing Test knowing only the specifications

Development Paradigm

System Development Process Understand the problem: Input Output Process Algorithm is the steps to solve problems Develop the solution (Algorithm): Structure chart Pseudocode Flowchart Converting design to computer codes. e.g: Flowchart -> C program System Development Process

Coding Coding is a process of converting flowchart to program code (source code) But, before you can start doing this, you should learn some basics including the language itself. 43

The conversion is almost straight forward Example: multiplying two numbers Source Code void main (void) { printf(“Enter the first number=> ”); scanf(“%d”,&A); printf(“Enter the second number=> ”); scanf(“%d”,&B); C = A * B; printf(“The Result is %d”, C); return; } The program still cannot be executed. It is not completed yet. 44

You will get these errors Error 1: Call to undefined function ‘printf’ We’re trying to call a function that is not recognized by the compiler. Error 3: Undefined symbol ‘A’ We’re trying to use a variable but it has never been defined. Compiler doesn’t recognize it 45

Fix the errors and complete the program This statement will help the compiler to recognize function ‘printf’ and function ‘scanf’ Variable must be declared before we can use it 46

Conclusion In this class you have learned about: Data structure and types of data structures Algorithm and its characteristics Programing principle System development process The knowledge given is to ensure that you are able to provide good solution to problem solving

References Frank M. Carano, Data Abstraction and problem solving with C++. Nor Bahiah et al. Struktur data & algoritma menggunakan C++.