COM1100 Fundamentals of Computer Science Review

Fall 1997 - Professor Fell


variables and assignment, cout
int x = 5;
int y = 7, z = 9;
cout << x << ' ' << y ' ' << z << endl;
x = y; y = z; z = x;
cout << x << ' ' << y ' ' << z << endl;
x = y; y = z; z = x;
cout << x << ' ' << y ' ' << z << endl;

arithmetic
int x = 5, y = 2, z = 3;
cout << x * y + z << endl;
cout << x * y / y / x << endl;
cout << x / z << ' ' << x % z << endl;
cout << y / z << ' ' << y % z << endl;
cout << x - y * z - x << endl;

cin
int x = 5, y = 2, z = 3;
cin >> x >> y >> z;				// NO endl
cout << x << ' ' << y << ' ' << z << endl;

simple graphics
(0, 0) to (200, 200) or to (400, 400)
verbs:	Frame, Paint, Erase, Invert
objects:	Rect, Oval, Circle
Also:  MoveTo, LineTo, DrawLine, ShowText
SetForeColor(red, green, blue);

for, while, do .. while
for (int N = 1; N <= 15; N += 3)
	cout << N <<" "<< N / 2 << endl;

Write a loop with this output:
1 2 4 8 16 32 64

int x = 1;
int y = 1;
while (x < 200){
	cout << x << "   " << y << endl;
	x = 2*x;
	y = y + x;
}

x = 100;
do {
	cout << x << endl;
	x /= 3;
} while (x > 1)

if, if else
int x = 100;
if (x % 9 == 1) cout << "OK" << endl;
if (x % 9 == 3) 
	x = x - 11;
else
	x = x + 4;
cout << x << endl;


cout << "enter x:  ";
cin >> x;						// try 100, 72, 35
if (x % 5 == 0)
	if (x % 10 == 0)
		cout << "divisible by 10" << endl;
else
	cout << "not divisible by 5" << endl;

logical operators
	and &&		or ||  		not !
	equals ==		<	<=	>	>=	!=
(3 < 5) or (7 < 2)
(3 < 5) and (7 < 2)

Write a C++ logical expression for each of the following:

switch
Simple Functions - Flow of Control
// function prototype
void somestuff();
void otherstuff();

main()
{
	somestuff();
	cout << "Now is the time" << endl;
	otherstuff();
	cout << "For all good dogs" << endl;
	otherstuff();
	somestuff();
	cout << "To find their bones." << endl;
}

void somestuff()
{
	cout << "+=+=+=+" << endl;
}

void otherstuff()
{
	for (int K = 0; K < 6; K++)
		cout << K << ' ' << " *** ";
	cout << endl;
}

Functions
int x = 2; y = 9;
while (x < y) 
	x = F(x, y);
xout << x << ' ' << y << endl;

int F(int x, int y){
	if (x < y)
		return 2*x;	
	else
		return 2*y;;	
}

These are the functions from the Little Function Lab. You should be able to write them.

  1. the distance function
  2. the factorial function
  3. the min function
  4. the Money function
  5. the IsSquare function
  6. the IsLetter function
  7. the Roll function
Some other funtions to write:
  1. CircleArea - argument radius, returns the area
  2. CircleCircumference - argument radius, returns the circumference
  3. RectArea - arguments height and width returns the area
  4. CircleFits arguments, x, y, r, returns true if fits in small window
  5. IsVowel char argument, true if char is a vowel
  6. IsPrime returns true iff int argument is prime
  7. argument a string, returns the number of words // assume only letters and blanks
  8. IsOp returns true iff the char argument is '+', '-', '*', ore '/'

Arrays
int A[8] = { 3, 5, 4, 2, -1, 7, 0, 6 };

void WriteArray(int A[], int N){
	for (int J = 0; J < N; J++)
		cout << J << ' ' << A[J] << endl;
	cout <<, endl;
}

for (int J = 0; J < 8; J++)
	if (A[J] < 1) A[J] = 1;
WriteArray(A, 8);

for (int J = 0; J < 7; J++)
	A[J] += A[J+1];
WriteArray(A, 8);

for (int J = 0; J < 8; J++)
	A[J] = A[(J*J) % 8];
WriteArray(A, 8);

Other Stuff
RequestInt() Char, ASCII, and Strings Simple File IO


Last Updated: November 11, 1997 9:29 pm by
Harriet Fell
College of Computer Science, Northeastern University
360 Huntington Avenue #161CN,
Boston, MA 02115
Internet: fell@ccs.neu.edu
Phone: (617) 373-2198 / Fax: (617) 373-5121
The URL for this document is: http://www.ccs.neu.edu/home/fell/COM1100/Docs/Review.html