1 / 23

Data Structures

Data Structures. 1. Data Structures. Reference http://www.algolist.net. 2. Algorithm :- Outline the essence of a computational procedure , step by step instructions . Program :- an implementation of an algorithm in some programming language

herb
Télécharger la présentation

Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures 1

  2. Data Structures • Reference • http://www.algolist.net 2

  3. Algorithm :- Outline the essence of a computational procedure , step by step instructions . • Program :- an implementation of an algorithm in some programming language • Data Structure :- Organization of data needed to solve the problem . 3

  4. What is an Algorithm ? Algorithm: Finite set of instructions that, if followed, accomplishes a particular task. Describe: Representation of algorithm can written By :- in natural language ( English ) / pseudo-code / diagrams / etc. 4

  5. Any computing algorithm will have AT MOST five kinds of components: • Data structures to hold data • Instructions change data values • Conditional expressions to make decisions • Control structures to act on decisions • Modules to make the algorithm manageable by abstraction, i.e., grouping related components 5

  6. Relations between Problems, Algorithms and Programs Problem . . . . Algorithm Algorithm . . . . . . . . Program Program Program Program 6

  7. What is a Good Algorithm ? • Efficient :- • Running Time • Space Used • Efficiency as function of input size :- • The number of bits in an input number • Number of data elements ( numbers , points ) 7

  8. Worst- / average- / best-case • Worst-case running time of an algorithm • The longest running time for any input of size n • An upper bound on the running time for any input  guarantee that the algorithm will never take longer • Example: Sort a set of numbers in increasing order; and the data is in decreasing order • The worst case can occur fairly often • E.g. in searching a database for a particular piece of information • Best-case running time • sort a set of numbers in increasing order; and the data is already in increasing order • Average-case running time • Average number of steps taken on any instance • of size n 8

  9. Pseudo- code :- A mixture of natural language and high – level programming concepts that describes the main ideas behind a generic implementation of a data structure or algorithm Eg:- Algorithm arrayMax ( A, n ) input : An array A sorting n integers Output : The Maximum element in A currentMax A[0] for i 1 to n-1 do if currentMax < A[i] then currentMax A[i] return currentMax 9

  10. Pseudo – Code • It is more structured than usual language but less formal than a programming language . • Expressions : • Use standard mathematical symbols to describe numeric and Boolean expression . • Use for assignment ( “=“ in java ) • Use = for the equality relationship( “==“ in java ) 10

  11. Pseudo- Code • Programming Constructs :- • decision structures :- if …. Then … [ else ] • while – loops : while .. Do • repeat – loops : repeat … until .. • for – loop : for .. Do • array indexing : A[i] , A[I,j] • Methods : • Calls : object method ( args ) • returns : return value 11

  12. Simple Algorithm Find sum n numbers between any given range numbers 1- Start 2- Read N 3- Sum =0 4- For I= 1 to N 4.1 sum = sum + I 4.1 next I 5- print Sum 6- End 12

  13. Basic Structure of C++ Language Documentation Section Link Section Definition Section Global Declaration Section main(){ Declaration Section Executable Section } Subprogram Section Function 1 ….. Function n 13

  14. Language Built-in User-defined Data Operators Data Operators Assignment ( = ) Arithmetic (+,-,*,/,%) Relational (<, <=, >, >=,==,!=) Logical (&&, ||) Unary (++, --) Atomic int (2 bytes) char (1 byte) float (4 bytes) double (8 bytes) 14

  15. Tokens • The smallest element in the C language is the token. • Tokens can be: • Numeric constants : 123, 98.6, 1000000 • Character constants : ‘A’, ‘a’, ‘$’, ‘4’ • String constants : “UMBC”, “I like ice cream.”, “123”, “CAR”, “car” • Keywords : int , while, for • Names (identifiers) • Punctuation : ; : , ‘ “ [ ] { } ( ) • Operators: =, +, -, *, /, <>, <, >, >=, !=, &&, ||, ++, --

  16. Simple C++Program /*---- Hello World C Example ("hello.c") -------------------------------------*/ /* ANSI C Headers */ #include <iostream.h> /* Main Program starts here */ void main( ) { inti = 0; /* End of declarations ... */ for ( i = 0; i < 10; i++ ) { cout<<“Hello World”<<endl<< i ); /* print to standard output */ } } Include headers if you need to use functions in these files Comment: /* .. */ Declare all your variables first 15

  17. Control Statement 16

  18. Eg:- to calculate the average of n number • /*Calculate the average of n numbers*/ #include<iostream.h> void main(){ int n, count=1; float x, average,sum=0; cout<<"Enter the limit”<<endl; /*initialize and read in a value for n*/ cin>>n; while(count<=n) /*Read in the numbers*/ { cout<<"x= “; cin>>x; sum+=x; ++count; } /*calculate the average and display result*/ average=sum/n; cout<<“The average is ”<<average; getch();} 17

  19. While Syntax :- While (Expression ) { Statement (s ) Increment counter } Do – while Syntax :- Do { statement(s) Counter } While(expression ); 18

  20. Functions • A function is a self contained program segment that carries out some specific,well-defined task. • C regards main() as function • Functions should be defined separately before or after main. • A function will process information that is passed to it from the calling program and return a single value • Each function contains: • A function heading (function name), followed by optional list of arguments enclosed in parentheses. • A list of argument declarations • A compound statement enclosed within a pair of braces { } 19

  21. Functions Cont.. • Function Definition: returntypefn_name(type1 arg1,type2 arg2, …..type n,arg n) {localvariables functioncode } Example: int maximum(int x, int y) { int z; z=(x>=y) ? x:y; return(z);} • Calling A function: function-name(arg1,arg2,….arg n); • Function Prototype: returntypefn_name(type1 arg1,type2 arg2, …..type n,arg n);

  22. C ++ Program illustrating Function /*Program to calculate the area of a circle*/ #include <iostream.h> const float PI= 3.14; float areacircle( float radius) ; /* function prototype*/ main() { float radius, area; cout<<"Enter value of radius“<<endl; cin>>radius; area=areacircle(radius); /* call function */ cout<<“Area= “<<area ; getch(); } float areacircle(float r) { /* definition of function */ a= PI*r*r; return(a); } 20

  23. Assignment -1- • Write algorithm for the following • sum of n numbers between given range ( Using while ) • sum of n numbers between given range ( Using Do… while ) • count even& odd numbers in given range( Using For ) • count even & odd numbers in given range( Using while ) • count even & odd numbers in given range(Using Do.. while) 21

More Related