Algorithmics and C basis A. Mucherino Algorithmics and C basis Introduction For beginners . . . Definition of algorithm Examples Antonio Mucherino The product of real numbers Maximum element of a vector University of Rennes 1 C basis www.antoniomucherino.it Variables, arrays Coding . . . Execution A sorting last update : August 2013 algorithm The algorithm The C code A little more advanced . . . Pointers Libraries
What’s an algorithm First example Make a travel by train Algorithmics and C basis A. Mucherino begin 1 go to the train station; 2 Introduction For beginners . . . buy a ticket for your destination; 3 Definition of algorithm look for the platform; 4 Examples The product of real numbers while the train is not yet at the station: wait ; 5 Maximum element of a vector get in the train; 6 C basis Variables, arrays for each stop 7 Coding . . . Execution if the train is at your destination A sorting algorithm get off the train; The algorithm else The C code A little more stay in ; advanced . . . Pointers end 8 Libraries
What’s an algorithm Second example Algorithmics and C basis Cook a pasta dish A. Mucherino begin 1 Introduction put water in the pot and make it boil; 2 For beginners . . . Definition of algorithm put pasta in the boiling water; 3 Examples The product of real repeat 4 numbers Maximum element of a vector taste the pasta; C basis Variables, arrays until the pasta is al dente ; 5 Coding . . . Execution remove the water; 6 A sorting algorithm add some sauce; 7 The algorithm The C code end 8 A little more advanced . . . Pointers Libraries
Some remarks Algorithmics If you can answer yes to the following two questions: and C basis A. Mucherino Are you able to understand well this algorithm? Introduction Are you able to perform all these tasks? For beginners . . . Definition of algorithm then, this algorithm is well-defined . Examples The product of real numbers Maximum element of a vector Control structures: C basis Variables, arrays Coding . . . Execution while , A sorting algorithm repeat . . . until , The algorithm The C code if , A little more advanced . . . for . Pointers Libraries
Some remarks Algorithmics If you can answer yes to the following two questions: and C basis A. Mucherino Are you able to understand well this algorithm? Introduction Are you able to perform all these tasks? For beginners . . . Definition of algorithm then, this algorithm is well-defined . Examples The product of real numbers Maximum element of a vector Control structures: C basis Variables, arrays Coding . . . Execution while , A sorting algorithm repeat . . . until , The algorithm The C code if , A little more advanced . . . for . Pointers Libraries
while Algorithmics and C basis A. Mucherino The while loop is used to execute an instruction or a block of instructions while a given condition is satisfied. Introduction For beginners . . . Its general format is: Definition of algorithm while (condition) do Examples instruction(s); The product of real end while numbers Maximum element of a vector where: C basis Variables, arrays Coding . . . condition is a logical condition; Execution A sorting instruction(s) represents the instruction or the block of algorithm instructions that are executed while condition is satisfied. The algorithm The C code A little more advanced . . . Pointers Libraries
repeat . . . until Algorithmics and C basis The repeat . . . until loop is used to execute an instruction or a A. Mucherino block of instructions until a certain condition is satisfied. Its general format is: Introduction repeat For beginners . . . Definition of instruction(s); algorithm until (condition); Examples The product of real numbers where: Maximum element of a vector C basis condition is a logical condition; Variables, arrays Coding . . . instruction(s) represents the instruction or the block of Execution instructions that are executed until condition is satisfied. A sorting algorithm The algorithm Main difference with while : The C code A little more at least one execution of instruction(s) is performed . advanced . . . Pointers Libraries
if Algorithmics The if keyword is used to execute an instruction or a block of and C basis instructions only when a certain condition is satisfied. A. Mucherino Its general format is: Introduction if (condition) then For beginners . . . instruction(s) A; Definition of else algorithm instruction(s) B; Examples end if The product of real numbers Maximum element of a vector where: C basis Variables, arrays condition is a logical condition; Coding . . . Execution A marks the instructions that are executed if condition is A sorting algorithm true; The algorithm The C code B marks the instructions that are executed if condition is A little more advanced . . . false. Pointers Libraries
for Algorithmics and C basis A. Mucherino The for loop repeats a set of instructions a predetermined number of times. It makes use of an internal counter. Introduction Its general format is: For beginners . . . Definition of for (initialization; condition; change) do algorithm instruction(s); Examples end for The product of real numbers Maximum element of a vector where: C basis Variables, arrays initialization defines the first value of the counter; Coding . . . Execution condition defines the stopping condition for the counter; A sorting algorithm change indicates how to modify the counter at each iteration. The algorithm The C code A little more advanced . . . Pointers Libraries
What’s an algorithm Third example Algorithmics and C basis Compute the sum of n numbers ( x 1 , x 2 , . . . , x n ) A. Mucherino begin 1 Introduction For beginners . . . define a new number and name it s ; 2 Definition of algorithm set s equal to 0; 3 Examples The product of real numbers for each number x i 4 Maximum element of a vector add x i to s ; C basis put the result in s ; Variables, arrays Coding . . . Execution end for 5 A sorting algorithm print s ; 6 The algorithm The C code end 7 A little more advanced . . . Pointers Libraries
Some remarks Algorithmics and C basis A. Mucherino Who can perform the algorithms we are studying? Introduction For beginners . . . Definition of algorithm Can you easily perform this algorithm? Examples The product of real numbers Can a computer machine perform this algorithm? Maximum element of a vector C basis Variables, arrays Coding . . . The algorithm must be well-defined for the executer . Execution A sorting algorithm The algorithm The C code A little more advanced . . . Pointers Libraries
Algorithm the formal definition Algorithmics and C basis An algorithm is a finite sequence of well-defined and A. Mucherino unambiguous instructions needed for performing a given task. Introduction finite sequence: infinite sequences are not useful! For beginners . . . Definition of algorithm well-defined and unambiguous instructions: all instructions Examples must be clearly stated so that the executer can understand The product of real numbers them well; Maximum element of a vector C basis performing a given task: algorithms are supposed to solve a Variables, arrays given problem. Coding . . . Execution A sorting algorithm The previous three are examples of algorithms that can be The algorithm The C code executed by humans . How to develop an algorithm that can be A little more executed by a computer? advanced . . . Pointers Libraries
What’s an algorithm Rewriting the third example Algorithmics and C basis A. Mucherino Given a vector of n real numbers ( x 1 , x 2 , . . . , x n ) , compute the sum s = x 1 + x 2 + · · · + x n Introduction For beginners . . . begin 1 Definition of algorithm set s = 0; // initialization of s 2 Examples The product of real numbers for i = 1 , n 3 Maximum element of a vector s = s + x i ; // adding each x i to s C basis Variables, arrays end for 4 Coding . . . Execution print s ; A sorting 5 algorithm The algorithm end 6 The C code A little more advanced . . . Pointers Libraries
The product of n real numbers version I Algorithmics and C basis Given a vector of n real numbers ( x 1 , x 2 , . . . , x n ) , A. Mucherino compute the product p = x 1 × x 2 × · · · × x n begin 1 Introduction For beginners . . . set p = 1; // initialization of p Definition of 2 algorithm Examples for i = 1 , n 3 The product of real numbers p = p × x i ; // multiplying the partial p and x i Maximum element of a vector C basis end for 4 Variables, arrays Coding . . . print p ; 5 Execution A sorting end 6 algorithm The algorithm The C code Is this algorithm the best one for computing products of real A little more advanced . . . numbers? Pointers Libraries
Recommend
More recommend