(4.7)--4.7Nestediterationanditsimplemen.pdf
《(4.7)--4.7Nestediterationanditsimplemen.pdf》由会员分享,可在线阅读,更多相关《(4.7)--4.7Nestediterationanditsimplemen.pdf(8页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 4.7 Nestediterationanditsimplemen
限制150内