Algorithmic Notations - Presentation Transcript
Algorithmic Notations - Presentation Transcript
Algorithmic Notations - Presentation Transcript
1. Muhammad Muzammal E Mail : hello_hi99@hotmail.com Gc University Faisalabad 2. Intro uc n d tio An algorithm, named after the ninth century Muslim scholar Abu Jafar Muhammad Ibn Musu Al- Khowarizmi, is defined as follows: An algorithm is a finite step-by-step procedure to achieve a required result. An algorithm is a sequence of computational steps that transform the input into the output. An algorithm is a sequence of operations performed on data that have to be organized in data structures. 3. Algorithmic Notations variable:=expression if condition then statements {elsif condition then statements} [else statements] case switch of {sub case: statements} while condition do statements repeat statements until condition for variable:=initial value [step step] to final value do statements proc procedure name (parameters) [returns type] statements procedure name (arguments) return expression read variable write expression 4. Algorithmic Notations 1. Programming notation 2. Specification notation 5. Programming notation General purpose algorithmic notations Pascal, C, C++, Java Declarations: int i, j = 3; double sum = 0; real x; int a[n] // array of n integers int b[1:n] # array of n integers5 double c[n,n] = ([n] ([n] 1.0)) 6. Programming notation (2) Sequential statements (look: Java) for [quantifier1, quantifier2 , .] { (list of statements) } quantifiers: i=0 to N i=1 to N by 2 i=0 to n-1 st i!=x #every value except x 7. Programming notation (3) Concurrent statements co statement1; #N parallel processes // ... // statementN; oc process foo { # a process in the ... (list of statements) # ... background } process bar [i=1 to n] # n processes 8. Programming notation (4) Functions and Procedures int addone(int v) { # an integer fnct return (v+1); } main () { # a void" procedure int n, sum; read(n); for [i = 1 to n] sum = sum + addone(i); write (the final value is, sum); } 9. Specification notation (1) Specifying atomic transactions process_state_1 #state of computation <S1; Sn;> #list of statements process_state_2 #state of computation the execution is atomic: the other processes see either state_1 or state_2 Example: x = 0; y = 10; < x = x+1; y = y + 1; > For other processes: either x==0 & y==10 or x==1 & y==11 Specification notation (2) Specification of await < await (B) S; > B delay condition S sequence of statements B is guaranteed to be true when execution of S begins S is guaranteed to terminate No internal state of S is visible to other processes Example: <await ( s>0 ) s = s - 1; >Algorithmic
Notations - Presentation
Transcript
1. Muhammad Muzammal E Mail : hello_hi99@hotmail.com Gc University Faisalabad 2. Intro uc n d tio An algorithm, named after the ninth century Muslim scholar Abu Jafar Muhammad Ibn Musu Al- Khowarizmi, is defined as follows: An algorithm is a finite step-by-step procedure to achieve a required result. An algorithm is a sequence of computational steps that transform the input into the output. An algorithm is a sequence of operations performed on data that have to be organized in data structures.
3. Algorithmic Notations variable:=expression if condition then statements {elsif condition then statements} [else statements] case switch of {sub case: statements} while condition do statements repeat statements until condition for variable:=initial value [step step] to final value do statements proc procedure name (parameters) [returns type] statements procedure name (arguments) return expression read variable write expression 4. Algorithmic Notations 1. Programming notation 2. Specification notation 5. Programming notation General purpose algorithmic notations Pascal, C, C++, Java Declarations: int i, j = 3; double sum = 0; real x; int a[n] // array of n integers int b[1:n] # array of n integers5 double c[n,n] = ([n] ([n] 1.0)) 6. Programming notation (2) Sequential statements (look: Java) for [quantifier1, quantifier2 , .] { (list of statements) } quantifiers: i=0 to N i=1 to N by 2 i=0 to n-1 st i!=x #every value except x 7. Programming notation (3) Concurrent statements co statement1; #N parallel processes // ... // statementN; oc process foo { # a process in the ... (list of statements) # ... background } process bar [i=1 to n] # n processes 8. Programming notation (4) Functions and Procedures int addone(int v) { # an integer fnct return (v+1); } main () { # a void" procedure int n, sum; read(n); for [i = 1 to n] sum = sum + addone(i); write (the final value is, sum); } 9. Specification notation (1) Specifying atomic transactions process_state_1 #state of computation <S1; Sn;> #list of statements process_state_2 #state of computation the execution is atomic: the other processes see either state_1 or state_2 Example: x = 0; y = 10; < x = x+1; y = y + 1; > For other processes: either x==0 & y==10 or x==1 & y==11 10. Specification notation (2) Specification of await < await (B) S; > B delay condition S sequence of statements B is guaranteed to be true when execution of S begins S is guaranteed to terminate No internal state of S is visible to other processes Example: <await ( s>0 ) s = s - 1; >
This is Google's cache of http://www.mycplus.com/tutorials/data-structures/arrays-c-cpp-programming/. It is a snapshot of the page as it appeared on 27 Aug 2011 10:13:08 GMT. The current page could have changed in the meantime. Learn more Text-only version These search terms are highlighted: data structure one dimensional array
C PROGRAMMING C++ PROGRAMMING SOURCE CODE PROGRAMMING BOOKS FREE UTILITIES FREE MAGAZINES DOWNLOADS PROGRAMMING FAQ
RSS - EMAIL
partner-pub-4182
FORID:10
ISO-8859-1
Search
Custom Search
What is an array and how you can use it? How to declare and initialise simple arrays? How to declare and initialise multidimensional arrays? How to perform simple operations on arrays?
What is an array?
Array is a very basic data structure provided by every programming language. Lets talk about an example scenario where we need to store ten employees data in our C/C++ program including name, age and salary. One of the solutions is to declare ten different variables to store employee name and ten more to store age and so on. Also you will need some sort of mechanism to get information about an employee, search employee records and sort them. To solve these types of problem C/C++ provide a mechanism called Arrays.
Definition
An array is simply a number of memory locations, each of which can store an item of data of the same data type and which are all referenced through the same variable name. Ivor Horton. Array may be defined abstractly as finite order set of homogeneous elements. So we can say that there are finite numbers of elements in an array and all the elements are of same data type. Also array elements are ordered i.e. we can access a specific array element by an index.
Here array_type declares base type of array which is the type of each element in array. In our example array_type is int and its name is Age. Size of the array is defined by array_size i.e. 10. We can access array elements by index, and first item in array is at index 0. First element of array is called lower bound and its always 0. Highest element in array is called upper bound. In C programming language upper and lower bounds cannot be changed during the execution of the program, so array length can be set only when the program in written.
Age 0 30 Age 1 32 Age 2 54 Age 3 32 Age 4 26 Age 5 29 Age 6 23 Age 7 43 Age 8 34 Age 9 5
Array has 10 elements Note: One good practice is to declare array length as a constant identifier. This will minimise the required work to change the array size during program development. Considering the array we declared above we can declare it like
#define NUM_EMPLOYEE 10 int Age[NUM_EMPLOYEE];
Declare and initialise array in one statement. Declare and initialise array separately.
Look at the following C code which demonstrates the declaration and initialisation of an array.
int Age [5] = {30, 22, 33, 44, 25}; int Age [5]; Age [0]=30; Age [1]=22; Age [2]=33; Age [3]=44; Age [4]=25;
Array can also be initialised in a ways that array size is omitted, in such case compiler automatically allocates memory to array.
int Age [] = {30, 22, 33, 44, 25};
Lets write a simple program that uses arrays to print out number of employees having salary more than 3000.
Array in C Programming
#include <windows.h> #include <stdio.h> #include <stdlib.h> #define NUM_EMPLOYEE 10 int main(int argc, char *argv[]){ int Salary[NUM_EMPLOYEE], lCount=0,gCount=0,i=0; printf("Enter employee salary (Max 10)\n "); for (i=0; i<NUM_EMPLOYEE; i++){ printf("\nEnter employee salary: %d - ",i+1); scanf("%d",&Salary[i]); } for(i=0; i<NUM_EMPLOYEE; i++){ if(Salary[i]<3000) lCount++;
else gCount++; } printf("\nThere are {%d} employee with salary more than 3000\n",gCount); printf("There are {%d} employee with salary less than 3000\n",lCount); printf("Press ENTER to continue...\n"); getchar(); return 0; }
cout << "There are " << gCount << " employee with salary more than 3000" << endl << "There are " << lCount << " employee with salary less than 3000" << endl; system("PAUSE"); return EXIT_SUCCESS; }
This defines an array containing 10 elements of type int. Each of these elements itself is an array of two integers. So to keep track of each element of this array is we have to use two indices. One is to keep track of row and other is to keep track of column.
Salary [4][0]=3450; Salary [0][1]=460; Salary [1][1]=680; Salary [2][1]=640; Salary [3][1]=240; Salary [4][1]=690;
Here is a complete program written in both C and C++ to demonstrate the use of multidimensional arrays. You can find the whole source code in a zip file at the end of the tutorial. Source code is available in both C and C++ programming languages. Zip file also contains the demonstration of three dimensional arrays.
for(i=0; i<NUM_EMPLOYEE; i++){ Salary[i][1] = ((Salary[i][0]*20)/100); } printf("Initial Salary + Increment = Total Salary\n"); for (i=0; i<NUM_EMPLOYEE; i++){ printf("%d\t\t%d\t\t%d\n",Salary[i][0],Salary[i][1],Salary[i][0 ]+Salary[i][1]); } printf("Press ENTER to continue...\n"); getchar(); return 0; }
for(i=0; i<NUM_EMPLOYEE; i++){ Salary[i][1] = ((Salary[i][0]*20)/100); } cout << "Initial Salary + Increment = Total Salary" << endl; for (i=0; i<NUM_EMPLOYEE; i++){ printf("%d\t\t%d\t\t%d\n",Salary[i][0],Salary[i][1],Salary[i][0]+Salar y[i][1]); } system("PAUSE"); return EXIT_SUCCESS; }