ࡱ> 130@ bjbjצצ -   8H \ 6 $(RzV   |    ]h x d* L06L   $  Programming Examples Convert Length converts feet and inches to centimeters. Write a program that takes as input a given length expressed in feet and inches Convert and output the length in centimeters Input: Length in feet and inches Output: Equivalent length in centimeters Lengths are given in feet and inches Program computes the equivalent length in centimeters One inch is equal to 2.54 centimeters Convert the length in feet and inches to all inches: Multiply the number of feet by 12 Add given inches Use the conversion formula (1 inch = 2.54 centimeters) to find the equivalent length in centimeters The algorithm is as follows: Get the length in feet and inches Convert the length into total inches Convert total inches into centimeters Output centimeters Variables int feet; //holds given feet int inches; //holds given inch int totalInches; //holds total inches double centimeters; // holds length in centimeters Named Constant const double conversion = 2.54; const int inchesPerFoot = 12; Main algorithm: Prompt user for input Get data Echo the input (output the input) Find length in inches Output length in inches Convert length to centimeters Output length in centimeters Putting it together: Program begins with comments System resources will be used for I/O Use input statements to get data and output statements to print results Data comes from keyboard and the output will display on the screen The first statement of the program, after comments, is preprocessor directive to include header file iostream Two types of memory locations for data manipulation: Named constants Variables Named constants are usually put before main so they can be used throughout program This program has only one function (main), which will contain all the code The program needs variables to manipulate data, which are declared in main Body of function: The body of the function main has the following form: int main () { declare variables statements return 0; } Writing a complete program: Begin the program with comments for documentation Include header files Declare named constants, if any Write the definition of the function main OP  ; < q r   f g    R̴ש̞uh- hGB*phhhGB*phhGB*phhWO.hGB*ph3fhWO.hGB*phhWO.hGB* phfhchG>*B*phhchGB*ph3fhchGB* phfhchGB*phhG h>xhG#hthG6CJ$OJQJ^JaJ$*P < r  g  & FgdG & FgdG & F 88^8gdG & F ^gdG & F 88^8gdG & FgdG & FgdGh^hgdGgdGgd(G   2 W ~  & / Q g   [ BS & FgdG & FgdG & FgdGRS\]Hh(G h(G5h~Ch>xhG5hWO.hGB*phha;hGB*phhGB*phh O hGB*phh- hGB*phh- hGB*ph3f S]HZ*?_$a$gdG^gdG & FgdG & FgdG & FgdG+0P/ =! "`#`$`%``D@D NormalCJ_HaJmH nHsH tH``` G Heading 2$<@&$56CJOJQJ\]^JaJtH DAD Default Paragraph FontRiR  Table Normal4 l4a (k(No List@ `@ GFooter  ! CJaJtH P<rg2W~&/Qg[ BS]HZ*?_00x0x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0@& 0nA& 0"n@0n@0n@0n@0n@0n@0n@& 0nA& 0J#nA& 0J#nA& 0J#nA& 0J#n@0 My00@0@00,R  S  25WZ[f 3333~/QB]HZ*_ V*&[ dIfP"R)$,[[Fh hh^h`hH.h88^8`OJQJo(hHh L^`LhH.h   ^ `hH.h   ^ `hH.h xLx^x`LhH.h HH^H`hH.h ^`hH.h L^`LhH. ^`OJQJo("  ^`OJQJo(" pp^p`OJQJo("  @ @ ^@ `OJQJo("  ^`OJQJo("  ^`OJQJo("  ^`OJQJo("  ^`OJQJo("  PP^P`OJQJo("  88^8`OJQJo("  ^`OJQJo("   ^ `OJQJo("    ^ `OJQJo("  xx^x`OJQJo("  HH^H`OJQJo("  ^`OJQJo("  ^`OJQJo("  ^`OJQJo("  88^8`OJQJo("  ^`OJQJo("   ^ `OJQJo("    ^ `OJQJo("  xx^x`OJQJo("  HH^H`OJQJo("  ^`OJQJo("  ^`OJQJo("  ^`OJQJo("  ^`OJQJo("  ^`OJQJo(" pp^p`OJQJo("  @ @ ^@ `OJQJo("  ^`OJQJo("  ^`OJQJo("  ^`OJQJo("  ^`OJQJo("  PP^P`OJQJo(" VR)[[FfP"[          xr*&f\@^xDS4J8gxJx㔾NDOZ n> L , $ N 3!Gz KmQ~*LCBd)DgX^!p!@6$FL%k&V'Z*)fS+![-1.I0Nj3> 7x8 ?=/ASAB~C5D~FFF#jFgH9PLSM?S YyYZg\ ] r_Sb,dopMzp0qq3sv-yV?y~ )tozKi @;?'='g9h!!TS~)Z;?G; ,CP*](G>lY\lDKZ B>#gp'Vfa~NPw4E; |cNMpN_Q 03eQJ@@pP`@UnknownGz Times New Roman5Symbol3& z Arial;[SOSimSun"1hQ+_+FEFE!(`42QH ?0Programming Examples       Oh+'0t  0 < HT\dlProgramming Examples1rog ogogog Normal.dot  rm1rmMicrosoft Word 10.0@ @v%[*@JO*FE՜.+,0 hp|   aO Programming Examples Title !"#$%&')*+,-./2Root Entry F9d*41TableWordDocument-SummaryInformation( DocumentSummaryInformation8(CompObjj  FMicrosoft Word Document MSWordDocWord.Document.89q