Lab 4 - FL13 - ECE 198 JL - University of Illinois - Engineering Wiki

A more appropriate use of the while loop the c ode s

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: h as s hown above, us ing the "for" loop res ults in a muc h more c onc is e and c leaner c ode. Henc e, we s ugges t y ou us e the "for" loop (ins tead of the "while" loop) to c arry out s imple tas k s lik e this . Below, we s how a muc h more appropriate us e of the while loop. A more appropriate use of the While loop The c ode s hown below is us ed to ac c ept a s eries of c harac ters from a us er. W hen the us er hits return (enter on the k ey board), the program s tops ac c epting c harac ters and dis play s how many c harac ters the us er has entered. In a real program y ou would s tore thes e c harac ters in an array for further manipulation. W e will introduc e array s and operations on them in later ex erc is es . #nld<ti.> icuesdoh itmi( n an) { ca c hr ; itcut=0 n on ; wie( ! 'n) hl c = \' { c=gthr) eca(; cut+ on+; } } pit(\ Yuhv etrd% caatr\" cut1; rnf"n o ae nee d hrcesn, on-) rtr 0 eun ; The rest of this lab's assignment Here y ou are given a program s tored in lab4.c file in whic h the iteration is done us ing a "for" loop. W he n re w rite the "for" loop into "w hile " loop, don't try to re a ssign "a " to som e othe r va lue s othe r tha n 256. #nld<ti.> icuesdoh #nld<ahh icuemt.> itmi( n an) { ita n; } fr( =26 a>1 a=sr() oa 5; ; qta) { pit(%\" a; rnf"dn, ) } pit(%\" 1; rnf"dn, ) Your tas k is to c onvert the above program and us e the "while" looping c ons truc t ins tead of the "for" looping c ons truc t". Edit this c ode in lab4.c to us e only the "while" loop and upload the modified C file to y our SVN repos itory . Remember, the output of the s eries s hould be ex ac tly the s ame as the output of the program given us ing "for" loop. HINT: Try unrolling the "for" loop as s hown above. This will give y ou a c lear idea of what is happening and will help y ou trans form y our c ode to u...
View Full Document

Ask a homework question - tutors are online