C++ info

download C++ info

of 61

Transcript of C++ info

Program Layout: To include header files: #include //at beginning of program All names defined in header file, and belong to a namespace std using namespace std; If not using namespace, commands must be entered as std::cout > input1 >> input2; cin >> charVal >> otherString;

Read line up to Format of getline: Concatenate strings Input multi inputs Input Char and String Declare Variables: Integer Assign value in declare Constant integer program Float variable (0.123) String variable Character Value Boolean Value Bool assign Val One Dimensional Array

int intValue; int intValue=0; //can be changed in program const int INT_VALUE=0; //cannot be changed in float floatValue; string name; //to be used with #include char charValue; bool result; bool result=false; int arrayVal[10];

Initialize during declare Two Dimensional Array Initialize during declare

int arrayVal[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int arrayVal[2][3]; //used for tables, row columns int arrayVal[][3]={1,2,3},{1,2,3};

Implicit conversion from int to float: floatValue = intValue; Explicit conversion from int to float: floatValue = float(intValue); Explicit conversion from float to int: intValue = int(floatValue); Explicit conversion from int to char: ASCIIVal = char(i); Explicit conversion from char to int: IntVal = int(charVal); String Manipulation: charVal=stringWord[charPos]; //{specific char in string} integerVal=stringWord.size( ); //{size of string} stringVal=stringWord.substr(fromPosToEnd); //{from pos to end} stringVal=stringWord.substr(fromPos, toPos);//{from pos to pos} integerVal=stringWord.find(oneWord); //{search string, -1 not found} integerVal=stringWord.find(oneWord, fromPos);//{search str from pos} stringWord.erase(fromPos, toPos); stringWord.insert(position, word2); stringWord.replace(fromPos, toPos, word2); getline(cin, stringWord, '\n'); Calculations: Increase by 1: Decrease by 1:

set set set set set

m m m m m

= = = = =

m m m m m

+ * / %

n n n n n

n++; n--; m = n++; //set m=n then increase n+1 m = ++n; //increase n+1 then set m=n m += n; m -= n; m *= n; m /= n; m %= n; // get leftover value from m / n

Value-returning functions: int abs(int) absolute value int ceil(float) smallest integer greater than or equal to float cos(float) cosine float exp(float) exponential function float fabs(float) floating point absolute value int floor(float) largest integer less than or equal to float log(float) natural logarithm float log10(float) logarithm base 10 float pow(float,float) power int rand( ) random integer value from 0 to 32767

float sin(float) float sqrt(float) char tolower(char) char toupper(char) int time(int)

sine square root converts to lower case converts to upper case current time as a large positive integer

Generating Random Number: srand(time(0)); //(seed random numbers with current date/time) randomInteger = interval * (rand( ) % many) + smallest; randomEvenNumbers = 2 * (rand( ) % 41) + 20;//(even ran 20-100) randomNumbers = 1 * (rand( ) % 101) + 0; //(rand 0-100=101 numb) // The smallest value generated by rand( ) % 21 is 0, so the smallest //value generated by 10 + rand( ) % 21 will be 10. The largest value //generated by rand( ) % 21 is 20, so the largest value generated by //10 + rand( ) % 21 will be 30: s p o o L e l i h W

Statement is executed repeatedly while Condition is True. The only exit from the while loop occurs when Condition is False. while (Condition) //(pre-test loop) { Statement; Statement; } do { Statement; Statement; } while (Condition); //(post-test loop) Three aspects of the while: 1) Initializing the control variable 2) Testing the value of the control variable 3) Changing the value of the control variable Counter-driven while loops: count = 1; while (count > value; } Situation-driven loops: cin >> x; while (x != 1) { if (x % 2 == 1) x = x * 3 + 1; else x = x / 2; cout = <