haverford cascade mentoring program computer programming: c++ to python conversion professor: dave...

21
Haverford Cascade Haverford Cascade Mentoring Program Mentoring Program Computer Programming: Computer Programming: C++ to Python Conversion C++ to Python Conversion Professor: Dave Wannacott Professor: Dave Wannacott Student: Kris Brower Student: Kris Brower Dobbins Vocational Tech Dobbins Vocational Tech Teacher: Andre O’Brien Teacher: Andre O’Brien Student: Denisha Davis Student: Denisha Davis

Upload: estefania-mudge

Post on 14-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

Haverford Cascade Haverford Cascade Mentoring Program Mentoring Program

Computer Programming:Computer Programming:C++ to Python ConversionC++ to Python Conversion

Professor: Dave WannacottProfessor: Dave WannacottStudent: Kris BrowerStudent: Kris Brower

Dobbins Vocational TechDobbins Vocational TechTeacher: Andre O’BrienTeacher: Andre O’BrienStudent: Denisha DavisStudent: Denisha Davis

Page 2: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

C++ and PythonC++ and Python

Brief History: C++Brief History: C++        In 1985 Bjarne Stroustrup, also of Bell Labs,In 1985 Bjarne Stroustrup, also of Bell Labs,

invented the C++ programming language. To the Cinvented the C++ programming language. To the Clanguage he added features for data abstraction andlanguage he added features for data abstraction andobject-oriented programming. Instead of naming theobject-oriented programming. Instead of naming thelanguage D, the Bell Labs group named it C++ in alanguage D, the Bell Labs group named it C++ in ahumorous vein. As we see later, ++ signifies thehumorous vein. As we see later, ++ signifies theincrement operation  in the C and C++ language. Givenincrement operation  in the C and C++ language. Givena variable x, the expression x++ means to incrementa variable x, the expression x++ means to increment(add one to) the current value of x. Therefore, the(add one to) the current value of x. Therefore, thename C++ suggests an enhanced ("incremented") versionname C++ suggests an enhanced ("incremented") versionof the C++ language.of the C++ language.

Although C originally was intended as a systemAlthough C originally was intended as a systemprogramming language, both C and C++ are widely usedprogramming language, both C and C++ are widely usedtoday in business, industry, and personal computing.today in business, industry, and personal computing.C++ is powerful and versatile, embodying a wide rangeC++ is powerful and versatile, embodying a wide rangeof programming concepts.of programming concepts.

Page 3: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

C++ and PythonC++ and Python

Brief History: PythonBrief History: PythonPython is a portable, interpreted, object-oriented Python is a portable, interpreted, object-oriented

programming language. Its development started in 1990 at programming language. Its development started in 1990 at CWICWI in Amsterdam, and continues under the ownership of in Amsterdam, and continues under the ownership of the the Python Software FoundationPython Software Foundation. The language has an . The language has an elegant (but not over-simplified) syntax; a small number of elegant (but not over-simplified) syntax; a small number of powerful high-level data types are built in. Python can be powerful high-level data types are built in. Python can be extended in a systematic fashion by adding new modules extended in a systematic fashion by adding new modules implemented in a compiled language such as C or C++. implemented in a compiled language such as C or C++. Such extension modules can define new functions and Such extension modules can define new functions and variables as well as new object types.variables as well as new object types.

Page 4: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

Differences Between C++ and Differences Between C++ and PythonPython

Simple program Simple program Comments (Declarations) Comments (Declarations) Syntax (input/output) Syntax (input/output) Variables (Identifier and Data Type)Variables (Identifier and Data Type) If statement If statement While loopsWhile loops For loopsFor loops FunctionsFunctions

Page 5: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

Simple programSimple program

C++C++

#include <iostream.h>#include <iostream.h>int main()int main(){{ cout<<“Hello cout<<“Hello

Haverford”<<endl;Haverford”<<endl; return 0;return 0;}}

PythonPython

print “Hello Haverford”print “Hello Haverford”

Page 6: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

Simple program (with comments)Simple program (with comments)

C++C++

//this program demonstrates //this program demonstrates C++C++

//in its simplest form//in its simplest form

#include <iostream.h>#include <iostream.h>

int main()int main()

{{

cout<<“Hello cout<<“Hello Haverford”<<endl;Haverford”<<endl;

return 0;return 0;

}}

PythonPython

#this program demonstrates#this program demonstrates

#python in its simplest form#python in its simplest form

print “Hello Haverford”print “Hello Haverford”

Page 7: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

Syntax (input/output)Syntax (input/output)

C++C++

cout<<“Please enter a name ”<<endl;cout<<“Please enter a name ”<<endl;

cin>>name;cin>>name;

cout<<“the name you entered was”, cout<<“the name you entered was”, name<<endl;name<<endl;

PythonPython

name = raw_input(“Please enter a name”)name = raw_input(“Please enter a name”)

print “the name you entered was”, nameprint “the name you entered was”, name

Page 8: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

VariablesVariables

C++C++

int number;int number;char name;char name;float calculate;float calculate;

number = 27;number = 27;name = “Denisha”;name = “Denisha”;calculate = 99.8;calculate = 99.8;

PythonPython

Number = 27Number = 27Name = “Denisha”Name = “Denisha”Calculate = 99.8Calculate = 99.8

Page 9: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

If statement (If-Then-Else form)If statement (If-Then-Else form)C++C++

Num1 = 10;Num1 = 10;Num2 = 32;Num2 = 32;Num3 = 14;Num3 = 14;

If (num1 > num2)If (num1 > num2){{

cout<<“10 is greater than 32<<“endl;cout<<“10 is greater than 32<<“endl;}}Else if (num2 < num3)Else if (num2 < num3){{

cout<<“32 is less than 14”<<endl;cout<<“32 is less than 14”<<endl;}}Else if (num3 > num2)Else if (num3 > num2){{

cout<<“14 is greater than 32”<<endl;cout<<“14 is greater than 32”<<endl;}}ElseElse

cout<<“please review your first grade cout<<“please review your first grade math material”<<endlmath material”<<endl

PythonPython

Num1 = 10Num1 = 10Num2 = 32Num2 = 32Num3 = 14Num3 = 14

If num1 > num2:If num1 > num2: print “10 is greater than 32”print “10 is greater than 32”Elif num2 < num3:Elif num2 < num3: print “32 is less than 14”print “32 is less than 14”Elif num3 >num2:Elif num3 >num2: print “14 is greater than 32”print “14 is greater than 32”ElseElse print “please review your first grade print “please review your first grade

math math material” material”

Page 10: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

While loopsWhile loops

C++C++

Count = 0;Count = 0;

While (count < 10)While (count < 10)

{{

cout<<“Andre cout<<“Andre O’Brien”<<endl;O’Brien”<<endl;

count =count +1;count =count +1;

}}

PythonPython

Count = 0Count = 0

While count < 10:While count < 10:

print “Andre O’Brien”print “Andre O’Brien”

count = count + 1count = count + 1

Page 11: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

For loops For loops

C++C++

For (loopCount = 1; loopCount<=10; For (loopCount = 1; loopCount<=10; loopCount++)loopCount++)

{{

cout<<“Andre O’Brien”<<endl;cout<<“Andre O’Brien”<<endl;

}}

PythonPython

For loopCount in range(10):For loopCount in range(10):

print “Andre O’Brien”print “Andre O’Brien”

Page 12: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

FunctionsFunctionsC++C++

Void someFunction()Void someFunction(){{

count = 0;count = 0;while (count<10)while (count<10){{

cout<<“Hey OB”<<endl;cout<<“Hey OB”<<endl;}}

someFunction() //function callsomeFunction() //function call

PythonPython

Def someFunction:Def someFunction:

count = 0count = 0

while count < 10:while count < 10:

print “Hey OB”print “Hey OB”

someFunction() #function callsomeFunction() #function call

Page 13: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

Differences Between C++ and PythonDifferences Between C++ and Python SummarySummary

On the average Python programs compile slower On the average Python programs compile slower than C++ programs. However, it usually takes less time to than C++ programs. However, it usually takes less time to write programs in Python and they tend to be shorter in write programs in Python and they tend to be shorter in length than their C++ counter parts. The approach to length than their C++ counter parts. The approach to programming remains virtually the same.programming remains virtually the same.

Another noteworthy comparison deals with how C++ Another noteworthy comparison deals with how C++ and Python deals with debugging issues. C++ highlights a and Python deals with debugging issues. C++ highlights a syntax or logic error but you must know the language well syntax or logic error but you must know the language well to understand what’s wrong. Python, on the other hand, to understand what’s wrong. Python, on the other hand, doesn’t highlight your errors. It does, however, tell you doesn’t highlight your errors. It does, however, tell you what line your error can be found on and it tells you what what line your error can be found on and it tells you what datatype or function is undefined. I personally prefer datatype or function is undefined. I personally prefer Python’s approach better.Python’s approach better.

Page 14: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

Summer Experiment – Lab 1Summer Experiment – Lab 1

Do two lines overlap?Do two lines overlap? Do two rectangles overlap?Do two rectangles overlap? Do two circles overlap?Do two circles overlap? Does a rectangle and a circle Does a rectangle and a circle

overlap?overlap? Do two line segments intersect? – Do two line segments intersect? –

beyond the scope of my current skill beyond the scope of my current skill level.level.

Page 15: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

Lab 1Lab 1

Page 16: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

Do two lines overlap?Do two lines overlap?Level – beginnerLevel – beginner

# Do 2 ranges overlap?# Do 2 ranges overlap?def range_overlap(min1,max1,min2,max2):def range_overlap(min1,max1,min2,max2):

if max1 < min2 or min1 > max2:if max1 < min2 or min1 > max2: return Falsereturn False else:else: return Truereturn True

Page 17: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

Do two rectangles overlap?Do two rectangles overlap?Level – beginnerLevel – beginner

# Do 2 Windows overlap? # Do 2 Windows overlap? Def window_overlap(minx1,maxx1,miny1Def window_overlap(minx1,maxx1,miny1 ,maxy1,minx2,maxx2,miny2,,maxy1,minx2,maxx2,miny2,

maxy2):maxy2): if maxy1<miny2 or maxx1<minx2 or maxy2 < if maxy1<miny2 or maxx1<minx2 or maxy2 <

miny1 or miny1 or maxx2 < minx1:maxx2 < minx1: return Falsereturn False else:else: return Truereturn True

Page 18: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

Do two circles overlap?Do two circles overlap?Level – intermediateLevel – intermediate

# Do 2 Cicles overlap?# Do 2 Cicles overlap?def circle_overlap(x1,y1,r1,x2,y2,r2):def circle_overlap(x1,y1,r1,x2,y2,r2):

distanceX = x2 - x1distanceX = x2 - x1 distanceY = y2 - y1distanceY = y2 - y1 distanceGAP = pow(distanceX,2) + distanceGAP = pow(distanceX,2) +

pow(distanceY,2)pow(distanceY,2) roc = r1 + r2roc = r1 + r2 roc = pow(roc,2)roc = pow(roc,2) if distanceGAP<=roc:if distanceGAP<=roc: return Truereturn True else:else: return Falsereturn False

Page 19: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

Does a rectangle and a circle Does a rectangle and a circle overlap?overlap?

Level – hardLevel – hard

# Do a circle and a rectangle overlap? # Do a circle and a rectangle overlap? def circle_rectangle_overlap(center_x,center_y,def circle_rectangle_overlap(center_x,center_y,

radius,xmin,xmax,ymin,ymax):radius,xmin,xmax,ymin,ymax): testX = center_xtestX = center_x testY = center_ytestY = center_y

if testX < xmin:if testX < xmin: testX = xmintestX = xmin if testX > xmax:if testX > xmax: testX = xmaxtestX = xmax if testY < ymin:if testY < ymin: testY = ymintestY = ymin if testY > ymax:if testY > ymax: testY = ymaxtestY = ymax

distanceX2 = (center_x-testX) * (center_x-testX)distanceX2 = (center_x-testX) * (center_x-testX) distanceY2 = (center_y-testY) * (center_y-testY)distanceY2 = (center_y-testY) * (center_y-testY) distTotal2 = distanceX2 + distanceY2distTotal2 = distanceX2 + distanceY2 fDist = sqrt(distTotal2)fDist = sqrt(distTotal2) if fDist < radius:if fDist < radius: return Truereturn True else:else: return Falsereturn False

Page 20: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

Do two line segments intersect?Do two line segments intersect?

Level – down right unfair!Level – down right unfair!

Page 21: Haverford Cascade Mentoring Program Computer Programming: C++ to Python Conversion Professor: Dave Wannacott Student: Kris Brower Dobbins Vocational Tech

ConclusionConclusion

C++ and PythonC++ and Python

Out with the Old in with the New!!!Out with the Old in with the New!!!