CS 211:
Introduction to Computer Programming II
Instructor: Brian M. Dennis
Teaching Assistants:
Tom Lechner, Bin Lin
http://www.cs.northwestern.edu/~bmd/cs211/

A Simple C++ program
#include <iostream>
using namespace std;
// HelloWorld programs originated with the
// book “The C Programming Language”
main() {
cout << “Hello, World\n”;
}

Scheme Equivalent
;; HelloWorld programs originated with the
;; book “The C Programming Language”
(display “Hello World”)
(newline)

Slide 4

Slide 5

Slide 6

Slide 7

Slightly More Complex
#include <iostream>
using namespace std;
main() {
cout << “The answer to the universe\n”
<< 42;
}

A New Literal Datatype
#include <iostream>
using namespace std;
main() {
  cout << “Pi is: “ << 3.14159f << “\n”;
}

Some Arithmetic
#include <iostream>
using namespace std;
main() {
 cout << “1 + 1 = “ << 1 + 1 << “\n”;
 cout << “2 * 2 = “ << 2 * 2 << “\n”;
 cout << “3 – 5 = “ << 3 – 5 << “\n”;
 cout << “11 / 2 = “ << 11 / 2 << “\n”;
}

Conditional
#include <iostream>
using namespace std;
main() {
  if ((10 % 2) == 0)
    cout << “10 is even\n”;
  if (5 % 2)
    cout << “5 is odd\n”;
  if ((5 % 2) != 0)
   cout << “5 is odd\n”;
}

Variables
#include <iostream>
using namespace std;
main() {
 int integer_temp =  1 + 1;
 cout << “1 + 1 = “ << integer_temp << “\n”;
 float floating_point_temp;
 floating_point_temp = 1.414f;
 cout << “Root 2 = “ << floating_point_temp
      << “\n”;
}

Scheme Equivalent
(let ((integer_temp (+ 1 1))
      (floating_point_temp 1.414))
  (display “1 + 1 = “)
  (display integer_temp)
  (newline)
  (display “Root 2 = “)
  (display floating_point_temp)
  (newline)
)

Slightly tweaked
(let ((temp (+ 1 1)))
  (display “1 + 1 = “)
  (display temp)
  (newline)
  (set! temp 1.414)
  (display “Root 2 = “)
  (display temp)
  (newline)
)

Variables
#include <iostream>
using namespace std;
main() {
 int temp =  1 + 1;
 cout << “1 + 1 = “ << temp << “\n”;
 temp = 1.414;
 cout << “Root 2 = “ << temp << “\n”;
}

That’s a Wrap
Reading
SICP
CH 1
D&D Ch1
1.14, 1.19 – 1.25