Notes
Outline
COM1100
Fundamentals of Computer Science –Winter 2000
Lecture 15
02/15/00
Announcement
Midterm will be Thursday (17th, Feb.)
1:35 ~ 2:35 PM
Close book, close note and handouts
If you can not make it, email me in advance.
yinyh@ccs.neu.edu
Announcement
In order to prepare the midterm, please review:
for loop lab
RandomLong(10, 20)
Possible answer is any number between 10 and 20
Logic expression
Lecture today
De Morgan’s Law
AND statements can be converted to OR statements and vice verse.
!(A && B) = !A || !B
!(A || B) = !A && !B
A && B = !(!A || !B)
A || B = !(!A && !B)
Mathematical functions
Function  Description Returned Value
abs(x) Absolute Value Same data type as x
pow(x,y) x raised to the y power Data type of argument x
sqrt(x) Square root of x Same data type as x
sin(x) Sine of x (x in radiants) double
cos(x) Cosine of x (x in radiants) double
tan(x) Tangent of x (x in radiants) double
log(x) Natural logarithm of x double
exp(x) e raised to the x power double
#include <math.h>
Example 1 -- while loop
const int MAXNUMS = 3;
int main() {
   int count = MAXNUMS;
   int total = 0;
   while ( count >= 1)  {
     total ++ ;
     count --;
   }
   cout << “Total = “ << total << endl;
   cout <<”count = “ << count << endl;
   return 0;
}
Example 2 – for statement
// Method 1 : use for statement
int space_count, line_count;
for (line_count = 0; line_count <= 9; line_count++ )
{
space_count = line_count;
for(int j = space_count; j >= 0; j--) {
cout << " ";
}
cout << line_count << endl;
}
Example 3 – while statement
// Method 2 : use while statement
int space_count = line_count = 0;
while (line_count <= 9) {
space_count = line_count;
while (space_count > 0) {
cout << " ";
  space_count--;
}
cout << line_count << endl;
    line_count++;
}
Example 4 : while and do-while
Question: ask user to guess a number 10 time. Each time if the number is great than 25, then print out “You lose !” and stop the loop immediately. Otherwise, print out “Keep entering” and continue the loop.
Solution 1: Please use while statement.
Solution 2: Please use do-while statement.
Solution 1 – while statement
int count = 1;
int num;
while( count <= 10 ) {
cout << "Enter a number: ";
cin >> num;
if (num > 25) {
cout << "You lose !" << endl;
break;
}
else {
cout << "Keep entering..." << endl;
count++;
}
}
cout << "\nwhile loop stop !" << endl;
Solution 2 – do-while statement
int count = 1;
int num;
do {
cout << "Guess a number: ";
cin >> num;
if (num > 25) {
cout << "You lose !" << endl;
break;
}
else {
cout << "Keep entering..." << endl;
count++;
}
} while (count <= 10);
cout << "do-while loop stop !" << endl;