CSC231m Lecture 18 - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

CSC231m Lecture 18

Description:

Files Needed in HW9 Part A. hw9a.m from /home/mhutchen/csc231. YOUR very ... off with a hammer, but only if the plate is plastic. ... as the art or science ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 55
Provided by: markshutch
Category:
Tags: csc231m | lecture

less

Transcript and Presenter's Notes

Title: CSC231m Lecture 18


1
CSC-231m Lecture 18
  • Other Languages
  • Progress
  • Programmers Toolbox

2
Todays Topics
  • How to do HW9.
  • Other high-level languages.
  • Where to get free compilers.
  • Syntax and semantics.
  • Your own toolbox.

3
Files Needed in HW9 Part A
  • hw9a.m from /home/mhutchen/csc231
  • YOUR very own function MyFunc.m
  • YOUR very own function MyOtherFunc.m
  • For example
  • function dRtn MyFunc (dInput)
  • dRtn dInput.2 4dInput 12
  • return

4
Plotting Reminder
  • case 2
  • fhTwo _at_Myfunc
  • dResult dMySimp (fhTwo, 1, 5, 500)
  • disp (The result is num2str(dResult))
  • dX 1 (5 1)/500 5
  • dY MyFunc (dX)
  • labels
  • plot (dX, dY)

5
Other High-Level Languages
  • Pascal led to Ada and was updated further as
    Modula2. These will be discussed and compared to
    MATLAB.
  • C led to C, which led to Java and C. These
    will be discussed and compared to MATLAB.
  • Fortran went through Fortran IV and Watfor to
    Fortran 77, Fortran 90, Fortran 95, and beyond.
    BASIC is somewhat derived from Fortran and comes
    in many flavors. These will also be compared to
    MATLAB.

6
Interpreted vs. Compiled
  • Basic is generally interpreted as is Java.
  • However, it is also possible to compile these
    languages.
  • Interpreted languages need a runtime environment
    to run within.
  • MATLAB is interpreted.
  • All other languages are compiled.
  • A standalone executable file is created and run
    within an operating system rather than a runtime.

7
Proprietary vs. Standard
  • MATLAB, Java, and C are proprietary. Their
    features are controlled by a particular company
    (MathWorks, Sun, and Microsoft respectively).
  • C, C, and Fortran are standard. Features are
    controlled by ANSI/ISO committees. The advantage
    of standardization is that any compiler can
    compile and run a program developed with a
    different compiler without modification.
  • Many Basic versions are proprietary, in fact I am
    not sure there is a standard Basic out there.

8
C Family of Languages
  • UNIX was originally written in machine language.
    C was developed to rewrite UNIX in a high-level
    language.
  • C was itself derived from B, which was derived
    from BCPL (Basic Combined Programming Language ).
  • C is literally an increment of C to add the
    concept of a class and object oriented design.
    Since C was the third language to come from
    BCPL, some argued it should be called P and
    others argued it should be called D.
  • Java evolved from C. Here, some argued this
    should be called C--, one step forward, one
    back.
  • Then came the lawsuits

9
Microsoft vs. Sun
  • Microsoft created J as part of their Visual
    Studio. However, J is pretty much Java 1 and
    not fully compatible with Java 2 from Sun.
  • Sun sued to keep Microsoft from calling their J
    Java and made them stop using the coffee-cup logo
    of Java.
  • Microsoft retaliated by creating C and the
    expanded .NET concept.
  • The battle ultimately still continues.

10
Why No Standard Java?
  • Java began life as a proprietary language for
    embedded computers, like those in household
    appliances.
  • The creation of the World Wide Web was an
    opportunity to take Java beyond embedded
    computers, although the Java Virtual Machine is
    ultimately an embedded simulated computer.
  • Were Sun to allow a committee to create an
    ISO/ANSI standard Java, they would lose control
    of their language. Time will tell if this is wise.

11
In Case You Have Forgotten
  • script M-file hw2a.m
  • fCels 0.0
  • fFahr 0.0
  • disp ('This program converts Celsius to
    Fahrenheit')
  • fCels input ('Enter a temperature in degrees C
    ')
  • fFahr ((9.0/5.0) fCels) 32.0
  • disp (num2str(fCels) ' degrees C is '
    num2str(fFahr) ' degrees F')

12
In C This Would Be
  • Homework 2, hw2a.c /
  • include ltstdio.hgt
  • int main (int argc, char argv)
  • float fCels 0.0
  • float fFahr 0.0
  • printf ("Enter temperature in degrees Celsius
    ")
  • scanf ("f", fCels)
  • fFahr ((9.0/5.0) fCels) 32.0
  • printf ("f degrees C f degrees F\n", fCels,
    fFahr)
  • return (0)

13
In Pre-Std C This Would Be
  • // Homework 2, hw2a.cpp, pre-standard
  • include ltiostream.hgt
  • int main (int argc, char argv)
  • float fCels 0.0
  • float fFahr 0.0
  • cout ltlt "Enter a temperature in degrees
    Celsius " ltlt endl
  • cin gtgt fCels
  • fFahr ((9.0/5.0) fCels) 32.0
  • cout ltlt fCels ltlt " degrees C " ltlt fFahr ltlt
    " degrees F" ltlt endl
  • return (0)

14
In Standard C It Is
  • // Homework 2, hw2a.cpp, pre-standard
  • include ltiostreamgt
  • using namespace std // otherwise stdcin and
    stdcout
  • int main (int argc, char argv)
  • float fCels 0.0
  • float fFahr 0.0
  • cout ltlt "Enter a temperature in degrees
    Celsius " ltlt endl
  • cin gtgt fCels
  • fFahr ((9.0/5.0) fCels) 32.0
  • cout ltlt fCels ltlt " degrees C " ltlt fFahr ltlt
    " degrees F" ltlt endl
  • return (0)

15
Now In Java
  • // Homework 2, hw2a.java
  • import cs1.Keyboard
  • public class hw2a
  • public static void main (String args)
  • float fCels 0.0f
  • float fFahr 0.0f
  • System.out.println("Enter a temperature
    in degrees Celsius\n")
  • fCels Keyboard.readFloat()
  • fFahr ((9.0f / 5.0f) fCels) 32.0f
  • System.out.println(fCels " degrees C is
    equal to "
  • fFahr " degrees
    F.\n")

16
On To C...
  • // Homework 2, hw2a.cs
  • using System
  • class hw2
  • static void Main (string args)
  • float fCels 0.0
  • float fFahr 0.0
  • Console.WriteLine (Enter a temperature in
    degrees Celsius )
  • fCels float.Parse (Console.ReadLine())
  • fFahr ((9.0/5.0) fCels) 32.0
  • Console.WriteLine (0 degrees C 1 degrees
    F\n, fCels, fFahr)

17
Common To All
  • if (fCels lt fLOWER) ...
  • else if (fCels gt fUPPER) ...
  • else ...
  • for (iLoop 1 iLoop lt 10 iLoop) ...
  • while (cQuit ! q) ...
  • do ... while (cQuit ! q)
  • switch (cAction)
  • case a
  • case A
  • break
  • default

18
Functions in C
  • float fCels, fFahr
  • float fAmount
  • int iDoll, iQuar, iDime, iNick
  • fCels fCelsToFahr (fCels)
  • Change (fAmount, iDoll, iQuar, iDime, iNick)
  • float fCelsToFahr (float fCelsius)
  • float fFahren
  • ...
  • return (fFahren)
  • void Change (float fAmt, int piDoll, ..., int
    piNick)
  • piDoll (int)fAmt
  • ...

19
Functions in C
  • float fCels, fFahr
  • float fAmount
  • int iDoll, iQuar, iDime, iNick
  • fCels fCelsToFahr (fCels)
  • Change (fAmount, iDoll, iQuar, iDime, iNick)
  • float fCelsToFahr (float fCelsius)
  • float fFahren
  • ...
  • return (fFahren)
  • void Change (float fAmt, int iDoll, ..., int
    iNick)
  • iDoll (int)fAmt
  • ...

20
Meanwhile, Over In Fortran
  • ! Homework 2, hw2a.f90
  • PROGRAM TempConv
  • IMPLICIT NONE
  • REAL rCels 0.0
  • REAL rFahr 0.0
  • PRINT , "Enter a temperature in degrees Celsius
    "
  • READ , rCels
  • rFahr ((9.0/5.0) rCels) 32.0
  • PRINT , rCels, " degrees C ", rFahr, " degrees
    F"
  • END PROGRAM TempConv

21
Other Statements
  • IF (rCels lt rLOWER) THEN
  • ELSE IF (rCels gt rUPPER) THEN
  • ELSE
  • END IF
  • DO iLoop 1, 10, 1
  • END DO
  • DO
  • IF (cQuit q) EXIT
  • END DO
  • SELECT CASE (cAction)
  • CASE (a, A)
  • CASE DEFAULT
  • END SELECT

22
Functions in Fortran
  • rFahr rCelsToFahr (rCels)
  • FUNCTION rCelsToFahr (rCelsius)
  • REAL rCelsToFahr
  • REAL, INTENT(IN) rCelsius
  • rCelsToFahr (9.0/5.0) rCelsius 32.0
  • END FUNCTION
  • CALL Change (rAmount, iDoll, iQuar, iDime, iNick)
  • SUBROUTINE Change (rAmt, iDol, iQua, iDim, iNic)
  • REAL, INTENT(IN) rAmt
  • INTEGER, INTENT(OUT) iDol, iQua, iDim, iNic
  • END SUBROUTINE

23
And In QuickBASIC
  • REM Homework 2, hw2a.bas
  • LET rCels 0.0
  • LET rFahr 0.0
  • PRINT Enter a temperature in degrees Celsius
  • INPUT rCels
  • rFahr ((9.0/5.0) rCels) 32.0
  • PRINT rCels degrees C rFahr degrees F
  • END

24
Other Statements
  • IF rCels lt LOWER THEN
  • ELSEIF rCels gt rUPPER THEN
  • ELSE
  • END IF
  • FOR iLoop 1 TO 10
  • NEXT iLoop
  • DO WHILE (cQuit ltgt q)
  • LOOP
  • DO
  • LOOP UNTIL cQuit q
  • SELECT CASE cAction
  • CASE a, A
  • CASE ELSE
  • END SELECT

25
Functions and Subroutines
  • rFahr rCelsToFahr (rCels)
  • CALL CHANGE (rAmt, iDol, iQua, iDim, iNic)
  • FUNCTION rCelsToFahr (rCelsius)
  • rCelsToFahr ((9.0/5.0) rCelsius) 32.0
  • END FUNCTION
  • SUB Change (rAmount, iDoll, iQuar, iDime, iNick)
  • iDoll INT(rAmount / 1.00)
  • rAmount rAmount (iDoll1.00)
  • iQuar INT(rAmount / 0.25)
  • ...
  • END SUB

26
Basics of BASIC
  • QuickBASIC from Microsoft was a compiled BASIC
    environment, you created programs that compiled
    into standalone executables.
  • QBasic was the next step, but it was an
    interpreted language. You had to run your
    programs within the QBasic runtime environment.
  • Visual Basic is the next step. It is based on a
    graphical user interface (GUI), see next slide.
  • Other companies have created other BASIC versions
    including TrueBASIC.

27
Visual Basic
  • The window (form) is laid out graphically.
  • The underlying code is then written. In this
    case
  • The user enters a temperature in the top edit
    box.
  • The user clicks on the Convert button.
  • The converted temperature is displayed in the
    lower edit box.

28
And Now in Pascal
  • ( hw2.pas )
  • PROGRAM hw2
  • VAR rCels, rFahr real
  • BEGIN
  • writeln ('Enter a temperature in degrees
    Celsius ')
  • readln (rCels)
  • rFahr ((9.0/5.0) rCels) 32.0
  • writeln (rCels, ' degrees C is ', rFahr, '
    degrees F.)
  • END.

29
Other Constructs in Pascal
  • IF x lt 10 THEN BEGIN
  • END
  • ELSE IF x gt 50 THEN BEGIN
  • END
  • ELSE BEGIN
  • END
  • CASE choice OF
  • label list statement
  • ELSE
  • END
  • WHILE digit lt n DO
  • BEGIN
  • END
  • FOR count 1 TO n DO
  • BEGIN
  • END
  • REPEAT
  • UNTIL digit gt n

30
Functions in Pascal
  • FUNCTION rCelsToFahr (rCel real) real
  • VAR
  • BEGIN
  • rCelsToFahr ((9.0/5.0) rCels) 32.0
  • END

31
Subroutines (Procedures)
  • PROCEDURE Change (rAmt real VAR iDol, iQua,
    iDim, iNic integer)
  • BEGIN
  • iDol int (rAmt / 1.00)
  • rAmt rAmt (iDol 1.00)
  • END

32
A Taste of Ada
  • WITH Text_IO
  • PROCEDURE hw2 IS
  • fCels Float
  • fFahr Float
  • BEGIN
  • Text_IO.Put (Item gt Enter a temperature in
    degrees Celsius )
  • Text_IO.NewLine
  • Text_IO.Get (Item gt fCels)
  • fFahr ((9.0/5.0) fCels) 32.0
  • Text_IO.Put (Item gt fCels)
  • Text_IO.Put (Item gt degrees C is )
  • Text_IO.Put (Item gt fFahr)
  • Text_IO.Put (Item gt degrees F.)
  • Text_IO.New_Line
  • END hw2

33
More Ada
  • IF x gt 10 THEN
  • ELSE
  • ELSIF x lt 1 THEN
  • END IF
  • FOR counter IN 1..repetitions LOOP
  • END LOOP
  • WHILE x lt 42 LOOP
  • END LOOP
  • LOOP
  • EXIT WHEN x lt 42
  • END LOOP
  • CASE cChoice IS
  • WHEN M gt
  • WHEN OTHERS gt
  • END CASE

34
Functions and Procedures
  • FUNCTION fCelsToFahr (fCels Float) RETURN
    Float
  • fFahr Float
  • BEGIN
  • fFahr ((9.0/5.0) fCels) 32.0
  • RETURN fFahr
  • END fCelsToFahr
  • PROCEDURE Change (fAmt IN Float iDol OUT
    Integer iQua OUT Integer iDim OUT Integer
    iNic OUT Integer) IS
  • BEGIN
  • iDol Integer (fAmt / 1.00)
  • fAmt fAmt (Float(iDol) 1.00)
  • END Change

35
A Bit of Modula 2
  • Module hw2
  • IMPORT
  • VAR rCelsius, rFahren REAL
  • PROCEDURE rCelsToFahr (rCels REAL)REAL
  • VAR rFahr REAL
  • BEGIN
  • rFahr ((9.0/5.0) rCels) 32.0
  • RETURN rFahr ( note no semicolon )
  • END rCelsToFahr
  • BEGIN
  • IO.StringOut (Enter a temperature in degrees
    Celsius )
  • IO.RealIn (rCelsius)
  • rFahren rCelsToFahr (rCelsius)
  • IO.FloatOut (rCelsius, 1, 2) ( minimum space,
    2 decimal places )
  • IO.StringOut ( degrees C is )
  • IO.FloatOut (rFahren, 1, 2)
  • IO.StringOut ( degrees F.)
  • IO.LineOut
  • END hw2.

36
Where To Get Free Compilers
  • For C and C, go to Borland and get their
    CBuilder. It comes with an integrated
    development environment (like MATLAB) and will
    handle both C and C.
  • http//www.borland.com/downloads/download_cbuilder
    .html
  • For Java, go to Sun. MATLAB installs a Java
    runtime also.
  • http//java.sun.com/j2se/1.4.2/download.html
  • For Fortran95, go to Salford Software
  • http//www.salfordsoftware.co.uk/software/download
    s/compilers.html

37
(No Transcript)
38
(No Transcript)
39
(No Transcript)
40
What About MATLAB?
  • It is probably closest to C, with a bit of
    Basic/Fortran mixed in.
  • You can create GUI applications as with Visual
    Basic (or Visual Whatever in Visual Studio).
  • You can create GUI applications as with Java.

41
MATLAB Alternatives
  • There appear to be four free alternatives to
    MATLAB, based on a Google search
  • DADiSP
  • Scilab
  • Octave
  • FreeMat
  • Disclaimer I have not tried any of these.

42
DADiSP
43
Scilab
44
Octave
45
FreeMat
46
Discipline, Etc.
  • This course was intentionally rigorous and
    structured.
  • MATLAB, or any other language is all about
  • Syntax the form of statements.
  • Semantics the meaning of those statements.
  • A lot of time was spent on style issues.
  • Consistent style leads to fewer programming
    errors and less frustration.
  • What you ultimately learned was simply discipline

47
Syntax
  • Given the following statement
  • for iLoop 1110
  • Syntax refers to the form of the statement
  • The for statement.
  • Variable iLoop is initially set to 1, incremented
    by 1 each time through the loop, and stops at 10.
  • The three numbers are separated by colons.

48
Semantics
  • Semantics refers to the meaning of the statement
  • Variable iLoop is initially set to 1, and the
    test expression is true, since iLoop lt 10.
  • The statements in the loop are executed.
  • At the end of the loop, variable iLoop is
    incremented.
  • Variable iLoop is compared to 10.
  • If it is less than or equal to that, the loop is
    executed again.
  • If it is greater than that, the loop terminates.

49
The Tool Problem
  • Old Adage
  • When your only tool is a hammer, every problem
    looks like a nail.
  • Fortunately, the hammer is not your only tool.

50
Programmers Toolbox
  • Top area basic data types, arrays, and strings.
  • First drawer basic program elements, input, and
    output.
  • Second drawer branching and looping constructs.
  • Third drawer functions.
  • Fourth drawer miscellaneous elements, like
    files.
  • Documentation headers.

51
Using the Proper Tool
  • Given the need to open a switch plate on your
    wall
  • A wrench wont help, nor will pliers.
  • Your fingernail probably isnt strong enough.
  • A knife might work.
  • You can break the plate off with a hammer, but
    only if the plate is plastic.
  • For this task, you need a screwdriver.

52
Pedagogy
  • (Defined as the art or science of teaching.)
  • In the curriculum of computer programming, it is
    widely accepted that students learn by
    progressing through three steps
  • Copy someone elses program and run it (hw1d,
    hw2a).
  • Modify someone elses program and run it (hw3a,
    etc.).
  • Create a program of their own and run it (pr2,
    etc.).
  • As you can see, we did all three of these early
    and often.

53
Progress
  • Week 1 Whats the difference between an integer
    and a float?
  • Stuff happened.
  • Week 10 You submit a running 100-line program of
    your own design.

54
Todays Lessons
  • You should realize that while you learned MATLAB,
    you also learned how to effectively program in
    any other language.
  • You should be able to get a free compiler for
    another language if you choose to do so.
  • You should understand why I stress syntax and
    semantics so much.
  • You should be able to design, code, test, and use
    a complex program for a project of your own.
Write a Comment
User Comments (0)
About PowerShow.com