(4.5)--4.5Iterativealgorithmandforstate.pdf
Iterative algorithm and for statement implementationIterative algorithm and for statement implementation4.5For many problems,a set of steps need to be performed repeatedly until achieving a certain situation.For example,design an algorithm for calculating the sum of 15.Iterative algorithm and for statement implementation4.5A simple algorithm could be designed as following:Step 1:Initialize sum to 0;Step 2:Add 1 to sum;Step 3:Add 2 to sum;Step 4:Add 3 to sum;Step 5:Add 4 to sum;Step 6:Add 5 to sum;Step 7:Output sum,which stores the sum from 1 to 5;Iterative algorithm and for statement implementation4.5With the above algorithm,100000 lines of operations like Add 1 to sum should be added.Obviously,this will not work when we need to deal with large-scale problems.A potential issue in this algorithm designIf the problem is to calculate the sum of 1100000,how should we design the algorithm?Iterative algorithm and for statement implementation4.5The iterative algorithm uses the idea of iteration,which is often adopted incomputational thinking.The repeated steps in an algorithm can be describedsimply and clearly,so that shorten the algorithm and improve its readability.To design an iterative algorithm,first we should determine the operation or a setof operations that need to be repeatedly performed,and then determine how manytimes they need to be performed.Iterative algorithmIterative algorithm and for statement implementation4.5For the problem we just mentioned,a shorter iterative algorithm can be designed as following:Step 1:Initialize sum to 0;Step 2:Let i range from 1 to 5,add i to sum;Step 3:Output sum,which stores the sum from 1 to 5;When the scale of problem becomes n,the only thing we need to do is to modify 5 to n.Iterative algorithm and for statement implementation4.5Problem solving using computational thinking:In this problem,the operation needs to be performed repeatedly is to accumulate an integer,and this operation needs to be performed for n times.Therefore,the iterative algorithm to solve this problem is shown in the following table.StepProcessing1The user enters the largest integer n to be accumulated.2Initialize sum,which stores the cumulative result,to 03Let i range from 1 to n,perform the following operation:Add i to sum4Output the result sumEg1Design an iterative algorithm for calculating 1+2+3+.+n.Iterative algorithm and for statement implementation4.5Implement iterative algorithm with C+for statementfor(;)The syntax of using for statement is:Iterative algorithm and for statement implementation4.5Expression1Expression3Expression2Loop BodyIterative algorithm and for statement implementation4.5int main()int n,sum;cout Please enter the largest integer to be accumulated:n;sum=0;for(int i=1;i=n;i+)sum=sum+i;coutThe result of 1+2+3+,+n is:sumendl;return 0;Question:How to use programming to calculate the sum of all even numbers from 1 to 100?Eg2According to the algorithm mentioned before,write a program using C+for statement to calculate 1+2+3+.+n.