C++ chapter 4-6 review
when a condition is true if both expressions are true
AND (&&)
write a program that is to judge whether the number is even or odd, where the number is input from keyboard.
#include <iostream> using namespace std; int main() { int number; cout<<"Enter an integer: \n"; cin>>number; // True if remainder is 0 if( number%2 == 0 ) cout<<number<<" is an even integer.\n"; else cout<<number<<" is an odd integer.\n"; return 0; }
interactive while loop syntax
#include <iostream> using namespace std; int main() { int count = 0, total = 0; while(count < 4) { int num; cout<<"Enter a num \n"; cin>>num; total = total + num; count = count + 1 } cout<<"The total is "<<total; return 0; }
Write a program that is to judge if the number falls in the range (-10, 0), where the number is received from keyboard
#include <iostream> using namespace std; int main() { int number; cout<<"Enter an integer: \n"; cin>>number; // Test expression is true if number is less than 0 if (number < 0) { if (number > -10) cout<<number<<" in the range (-10, 0)\n"; } return 0; }
Write a Program to print the numbers from 1 to 10 in increments of 3. The output of your program should be the following: 1 4 7 10
#include <stream> using namespace std; int main() { int num = 1; while(num < 11) { cout<<num<<" "; num = num + 3; } return 0; }
the condition can be tested
- At the beginning: Pretest - At the end: Posttest
4 required elements of a repetition structure
- Repetition statement - Condition to be changed - Initial value for the condition - Loop termination
repetition statements include:
- while - for - do while
for statement
A loop with a fixed count condition that handles alteration of the condition
Function header's purpose:
Identify data type of value function returns, provide function with name, and specify number, order, and type of arguments function expects
changes an expression to its opposite state; true becomes false, false becomes true
NOT ( ! )
when a condition is true if either one or both of the expressions is true
OR ( | | )
determine the number, type and order (sequence) of the values that must be passed to the function: void factorial ( int n )
Require one int value
determine the number, type and order (sequence) of the values that must be passed to the function: void power ( int type, double induct, double cap )
Require three values in this order: an int, a double, and a double
determine the number, type and order (sequence) of the values that must be passed to the function: void total ( double amount, double rate )
Require two values in this order: a double, and a double
Function body's purpose:
To operate on passed data and return, at most, one value directly back to the calling function
if-else statement
a decision structure for two alternatives
one-way selection
an if statement without the optional else portion
nested if statement
an if-else statement completely contained within another if-else
write a function called beamMoment ( ) that accepts two double-precision number as parameters ( one for the base (b) and one for the height(h) ) , calculates the corresponding second moment of inertia(I) and displays the result. formula: I = b x h^3 /12
double beamMoment(double b, double h) { double I = (b*h^3)/12.0; cout<<"The second moment is "<<I<<endl; return I; } int main(){ double b = 0.2, h = 1.2; double I = beamMonment(b,h); return 0; }
write the function header for: a function named findAbs( ) that accepts a double precision number passed to it and returns its absolute value.
double findAbs(double num);
a zero value is considered to be
false
a=5 and b=2 what is the value of ! ( a * b ) ?
false
b=2, c=4, and d=6 what's the value of d % b == c % b ?
false
write the function header for: a function named mult( ) that accepts 2 floating-point numbers as parameters, multiplies these 2 numbers, and returns the result.
float mult(float num1, float num2);
for statement syntax
for (initializing list; expression; altering list)statement;
break statement
forces an immediate break, or exit, from switch, while ,for, and do-while statements
syntax for if-else statement
if (condition) statement executed if condition is true; else statement executed if condition is false;
write the function header for: a function named powfun( ) that raises and interger number passed to it (as an argument) to a positive integer power and returns the result as an integer.
int powfun(int num, int power);
write the function header for: a function named sqrIt( ) that computes and returns the square of the integer value passed to it.
int sqrIt(int num);
infinite loop
never terminates
total = 0 for (i = 1; i <= 10; i =i +1 ) total = total + 1; determine the value in total after the loop is executed
total = 0 + 1*10 = 10
total = 0; for ( i = 10; i <=15; i = i +1 ) total = total + i;
total = 0 + 10 + 11 + 12 + 13 + 14 + 15 = 75
a non-zero value is considered to be
true
determine the value of the following expression assuming a=5 and b=2 a>b
true
write the function header for: a function named void check ( ) that has 3 parameters. The first parameter should accept an interger number, and the second and third parameters should accept a double-precision number. the function returns no value.
void check(int a, double b, double c);
write the function header for: a function that produces a table of the numbers from 1 to 10, their squares, and their cubes. No arguments are to be passed to the function, and the function returns no value.
void displayTable(void);
while statement syntax
while ( expression) statement;
Every C++ function consists of two parts:
• Function header • Function body