1 //28-programming exercise one. CPP: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6#include <climits>7#include <array>8#include <string>9#include <math.h>Ten One using namespacestd; A - intMain () - { the //1. What does the following code print? - inti; - for(inti =0; I <5; i++) -cout <<i; +cout <<Endl; - //Answer: 01234 + A //2. What the following code will print at intJ; - for(j =0; J < One; J + =3) -cout <<J; -cout << Endl << J <<Endl; - //Answer: 0369 - // A in // - to //3. What will be printed on the next generation? + intf =5; - while(++f <9)//There is no difference when using ++i and i++ alone, but there are differences when placed in an expression. thecout << f++ << Endl;//i++ will first use the value of I for expression operations, after the end I again self-increment, the ++i will be self-increment and then perform the operation of the expression. * //Answer: 6 $ //8Panax Notoginseng // - the //4. What does the following code print? + intK =8; A Do thecout <<"k="<< k <<Endl; + while(K++ <5); - //Answer: $ //k=8 $ // - - //5. Write a For loop that prints 1 2 4 8 16 32 64 the //method One: do and loop - inttemp =1;Wuyi intindex =0; the Do - { Wucout << Temp <<Endl; -Temp *=2; Aboutindex++; $} while(index<7); - - //method Two: For Loop - intTemp2 =1; A for(intI=0;i<7; i++) + { theTemp2 =int(Pow (2, i)); -cout << Temp2 <<Endl; $ } the //method Three: First cycle the storage, then traverse the output. thearray<int,7>array1{}; the for(inti =0; i<7; i++) the { -Array1[i]= Pow (2, i); in } the the for(intTEMP:ARRAY1)//can only take temp corresponding value, cannot set the value, wants to set the temp corresponding array the value to use int& temp:array1 About { thecout << Temp <<Endl; the } the + //method Four: The simplest - for(inti =1; I <= -; I *=2) the {Bayicout << i <<Endl; the } the - //6. Write a program that allows the user to enter two integers and output the and of all integers between the two integers, including the two integers. For example, 2 5 contains 2 4 5 all integers and one for each - the intNUM1; the intnum2; the intnum3=0; the -cout <<"Please enter a total of two integers:"<<Endl; theCIN >>NUM1; theCIN >>num2; thecout <<"the two numbers entered are:"<<num1 <<" "<< num2<<Endl;94 if(Num1 >num2) the { the inttemp; thetemp =NUM1;98NUM1 =num2; Aboutnum2 =temp; - }101 for(inti = NUM1; I <= num2; i++)102 {103cout << i <<" ";104num3 = num3 +i; the }106cout <<"their and for:"<<NUM3 <<Endl;107 108 //7. Write a program that allows the user to continuously enter a number, each time the number is entered, report all the current input and. When the user enters 0, the program ends. 109 //method One: the intalnum=0;111 intinpnum=2; the while(inpnum!=0)113 { thecout <<"Please enter a number:"<<Endl; theCIN >>Inpnum; theAlnum + =Inpnum;117cout <<"the current input and is:"<< Alnum <<Endl;;118 }119 //Method Two: - floatTotal =0;121 while(true)122 {123cout <<"Please enter a number:";124 floatNumber ; theCIN >>Number ;126 if(Number = =0)127 { - Break;//Break statement jumps out of loop129 } theTotal + =Number ;131cout <<"all currently entered and are:"<< Total <<Endl; the }133 134 intT;135CIN >>T;136 return 0;137}
Fundamentals of C + + programming 128-programming exercise one