Please send questions to
st10@humboldt.edu .
Random notes from Week 12 Lecture - 4-9-07
* reminder: a Python function...
#-----
# contract: get_hypotenuse: number number -> number
# purpose: compute and return the length of the hypotenuse
# of a right triangle whose two "other" sides
# are of length <side1>, <side2>
# examples: get_hypotenuse(3, 4) == 5
# get_hypotenuse(3, 7) == 7.62
from math import *
def get_hypotenuse(side1, side2):
return sqrt( pow(side1, 2) + pow(side2, 2) )
#-----
* ...and its C++ equivalent:
//-----
/*
# contract: get_hypotenuse: double double -> double
# purpose: compute and return the length of the hypotenuse
# of a right triangle whose two "other" sides
# are of length <side1>, <side2>
# examples: get_hypotenuse(3, 4) == 5
# get_hypotenuse(3, 7) == 7.62
*/
#include <cmath>
using namespace std;
double get_hypotenuse (double side1, double side2)
{
return sqrt( pow(side1, 2) + pow(side2, 2) );
}
//-----
* SO: moving on and talking about if statements ---
* ... the basic FLOW CHART is the same for both C++'s and Python's
if statement!
...just the SYNTAX differs.
* the differences in their syntax:
* in C++, you GOTTA put an overall set of parentheses
around the if-condition
* in C++, you DON'T need a colon after the condition
* in C++, exactly ONE statement follows the if and condition,
BUT! in C++, you can always consider a BLOCK* to be a single
statement
(* a BLOCK is a {, as many statements as you want, and a } --
COURSE STYLE: each {, } are on their OWN line,
lined up with preceding and following lines,
and statements WITHIN are indented by 3 or more spaces)
* SO:
if (val < 3)
{
sum = sum + val;
return true;
}
* ...oh yes, and in C++ don't forget the ; after each action
PYTHON:
if grade < 0:
return -1
C++:
if (grade < 0)
{
return -1;
}
* there is no elif in C++ (but else if (cond) is fine)
* the else is optional in both C++ and Python,
but in C++, DON'T put a colon after the else.
PYTHON:
if grade < 0:
return -1
elif grade > 0:
return 1
else:
return 0
C++:
if (grade < 0)
{
return -1;
}
else if (grade > 0)
{
return 1;
}
else
{
return 0;
}
LECT EX #1: translate this PYTHON fragment into C++
(assume all functions already available and defined)
PYTHON:
if (worked_overtime(hrs_worked) == True):
return (reg_owed(hrs_worked) +
overtime_owed(hrs_worked - OVERTIME_LIMIT))
else:
return reg_owed(hrs_worked)
* what about while loops?
* again, the semantics are the same!! (flow chart would
be the same)
* only the syntax differs:
* in C++, you've GOT to have parentheses around the
overall while condition
* in C++, you don't have a colon after the while condition
* in C++, it expects exactly 1 "statement" after the
while (but you can use a BLOCK wherever you can use
a statement)
ASIDE: a C++ local variable must be DECLARED (and set to something)
before USING it...
type variable_name;
type variable_name = init_value;
PYTHON:
sum = 0
count = 0
while count < 10:
sum = sum + count
count = count + 1
return sum
C++:
int sum = 0;
int count = 0;
while (count < 10)
{
sum = sum + count;
count = count + 1;
}
return sum;
* what if we'd like to throw in a little screen output?
...in C, C++, input and output are actually not part of
the base language --- they are provided by libraries;
... the C++ iostream library provides some simple input/output
(you put:
#include <iostream>
using namespace std;
...to use goodies from the iostream library
SO: to print something to the screen:
cout << "value";
^ stream operator
cout << "value" << (3 + 5) << endl;
^ print value
^ print 8
^ and end it with a newline
cout << "\n";
^ this prints a newline, too
cout << "value" << (3 + 5) << endl;
value8
Note: if you put a variable name in, it prints the VALUE
of that variable
int i = 7;
cout << i;
i = i + 3;
cout << i;
710
Lect Ex #3: write a count-controlled loop that prints HELLO to
the screen 7 times, EACH on its own line (no named constant required
JUST for this syntax-lecture-exercise...)
How about input?
* in the C++ iostream standard, there's an object cin
cin >> variable;
... next thing typed by the user, C++ will try valiantly
to convert to variable's type and and set variable to
int val;
cout << "type something";
cin >> val;