《(4.7)--4.7Nestediterationanditsimplemen.pdf》由会员分享,可在线阅读,更多相关《(4.7)--4.7Nestediterationanditsimplemen.pdf(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Nested iterationand its implementationNested iteration and its implementation4.7If the processing inside an iterative algorithm is still iterativeitself,thenitformsanestediterationstructure.Inpracticalapplications,nested iteration is often applied in C+programs,whichmeans there could be various loop
2、 statements in the body of a loop andforms a nested iterative structure,which is usually called multipleloops.Nested iteration and its implementation4.7In this problem,we need to deal with an n-term accumulation,which means it needs to be repeated for n times.For each term inthe accumulation,it requ
3、ires an iterative processing,i.e.tocalculate the i-th term,the operation of multiplying by 2 isrepeated for i times.Therefore,this algorithm requires a nestediterative structure.The nested iterative algorithm for this problemis shown in the following table.Problem solving using computational thinkin
4、g:Eg1Design an algorithm to calculate 21+22+23+2n,and implement it in a C+program.Nested iteration and its implementation4.7StepProcessing1Input n,the highest power of 22Initialize sum to 0 for each term in the accumulation3Set the range of i to 1n,and operate according to the following steps:Set po
5、wer,which is used to store the result for the i-th term,to 1Repeat the following steps for i times:power=power2Add power to sum4Output the results,sumNested iteration and its implementation4.7int main()int n,sum,power,i,j;coutn;sum=0;for(i=1;i=n;i+)power=1;j=1;while(j=i)power=power*2;j+;sum=sum+powe
6、r;coutThe sum of 2 to the 1st power to 2 to the n th power is:sumendl;return 0;Question:How to implement the inner loop using for statement?Nested iteration and its implementation4.7Tips:Iterative statements of different structures can also form a nested iteration when theyre combined together,for e
7、xample,while loop can be nested in for loop.The execution order of nested iteration is that when the outer loop is executed,and encounters the inner loop,it will first complete the execution of all inner loops,and then continue to execute the next outer loop.Multiple inner loops can be nested in the
8、 body of a loop.Nested iteration and its implementation4.7Since the Narcissistic numbers are three digit integers,their valuemust be in the range of 100999.Using the triple loop,the outerloop variable i controls the value of hundreds digit varies from 1 to9,the middle loop variable j controls the va
9、lue of tens digit variesfrom 0 to 9,the inner loop variable k controls the the value of onesdigit varies from 0 to 9,exhaustively search and determine whethereach one of all three digit integers is an Narcissistic number.Problem solving using computational thinking:Eg2Narcissistic number(or Shui xia
10、n hua number in Chinese)refers to a three digitinteger whose sum of the cubes of each digit is equal to the number itself.For example,since 153=13+53+33,153 is a narcissistic number.Write a program to find out all Narcissistic numbers.Nested iteration and its implementation4.7int main()int i,j,k,m,n
11、;for(i=1;i=9;i+)/outer loop,controls the hundreds digitfor(j=0;j=9;j+)/middle loop,controls the tens digitfor(k=0;k=9;k+)/inner loop,controls the ones digitm=i*i*i+j*j*j+k*k*k;n=100*i+10*j+k;if(m=n)coutmendl;return 0;Question:How to find out only thefirst Narcissistic numberwith the program shownon the left?