CSC231m Sample MidTerm - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

CSC231m Sample MidTerm

Description:

Unless otherwise stated, it is unnecessary to put statements ... Using fEnergy, fMass, and fLIGHTSPEED, write Einstein's famous equation E=Mc in proper MATLAB. ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 22
Provided by: markshutch
Category:
Tags: midterm | csc231m | emc2 | sample

less

Transcript and Presenter's Notes

Title: CSC231m Sample MidTerm


1
CSC-231m Sample Mid-Term
  • Questions From Past Mid-Terms
  • Point Values
  • Answers

2
Todays Topics
  • Sample questions from a previous test. Since this
    is the first quarter under MATLAB, these were
    translated from Fortran and C.
  • Desired or expected answers to those questions.

3
Notes on Semantics
  • Unless otherwise stated, it is unnecessary to put
    statements in IF, FOR, or WHILE constructs.
    is sufficient, or stuff goes here.
  • If a question includes, and something else
    otherwise, it means you need an ELSE or an
    OTHERWISE statement.
  • If a question about logical expressions includes
    the wording, if this is true and that is also
    true, it means you need an AND ().
  • Conversely, if it says or , you need an OR
    ().
  • In a loop question, if there are three
    quantities, its a FOR loop. If there is only one
    quantity, its a WHILE loop.

4
Variable Declaration
  • 5 points
  • Declare one floating point variable fNumberOne
    and one integer variable iNumberTwo and
    initialize them to 12.34 and 567 respectively.
  • Desired answer
  • fNumberOne 12.34
  • iNumberTwo 567

5
Constant Declaration
  • 5 Points
  • Declare a floating constant called fSEED which is
    equal to 123.45.
  • Desired answer
  • fSEED 123.45

6
Prompt and Input
  • 5 Points
  • Write statements that prompt a user to enter
    his/her year of birth, and input the number as an
    integer called iBirth.
  • Two possible answers
  • disp (Enter your year of birth )
  • iBirth input ()
  • Or
  • iBirth input (Enter your year of birth )
  • not required in either case.

7
Equation Development
  • 5 Points
  • Using fEnergy, fMass, and fLIGHTSPEED, write
    Einsteins famous equation EMc² in proper
    MATLAB.
  • Two possible answers, the first one is the
    preferred one
  • fEnergy fMass fLIGHTSPEED 2.0
  • Or
  • fEnergy fMass fLIGHTSPEED fLIGHTSPEED

8
Simple IF Structure
  • 5 Points
  • Write the significant lines of an IF structure
    that will be executed if iYear is not equal to
    2000.
  • You only need TWO statements
  • if (iYear 2000)
  • end

9
Loop, Counter-Controlled
  • 5 Points
  • Write the significant lines of a loop in which
    iLoop is initially set to 100, does not exceed
    iMaxVal, and has a step size of 25.
  • You only need TWO statements
  • for iLoop 100 25 iMaxVal
  • end
  • (spaces added for clarity and readability)

10
Loop, Event-Controlled
  • 5 Points
  • Write the significant lines of a loop that
    terminates when iInput is equal to 175.
  • Answer
  • while (iInput 175)
  • end

11
Compound Logical Expression
  • 5 Points
  • Write the compound logical expression that would
    be true if iInput is greater than 7 and less than
    or equal to 21.
  • Answer
  • ((iInput gt 7) (iInput lt 21))
  • Note the parentheses

12
Program Beginning
  • 5 Points
  • Other than a header, what are the first lines of
    a program that has integer variables iOne, iTwo,
    and iThree, a float fFour, and a constant fFIVE
    with a value of 5.55?
  • Answer
  • iOne 0
  • iTwo 0
  • iThree 0
  • fFour 0.0
  • fFIVE 5.5

13
Character Strings
  • 5 Points
  • Declare a character string of Sorry, not a
    winner. called sSorry and then write the line of
    code to display it
  • Answer
  • sSorry Sorry, not a winner.
  • and
  • disp (sSorry)

14
Yet Another Loop
  • 5 Points
  • Write the significant lines of a loop that counts
    from 0 to 100 by 3s using variable iLoop.
  • Answer
  • for iLoop 0 3 100
  • end

15
IF Structure
  • 5 Points
  • Write the significant lines of an IF construct
    that will do one thing if fVar is less than or
    equal to 42.98 and something else if it is
    greater than 98.25 (dont worry about writing the
    code that goes between or after these).
  • Answer
  • if (fVar lt 42.98)
  • elseif (fVar gt 98.25)
  • end

16
More Complex IF Structure
  • 10 Points, two parts
  • A. Write the significant lines of an IF construct
    that does one thing if cInput is C, another if
    it is S, and something else otherwise
  • Answer
  • if (cInput C)
  • elseif (cInput S)
  • else
  • end
  • B. List two ways to properly handle lowercase
    letters
  • cInput upper (cInput) before the construct
  • If ((cInput C) (cInput c)) etc.

17
SWITCH-CASE Construct
  • 10 Points
  • Write the SWITCH-CASE construct for iVar that
    will do one thing if iVar is 0, another if it is
    1, 2, or 3, yet another if it is 4, 5, or 6, and
    something else otherwise
  • Answer
  • switch (iVar)
  • case 0 case 0 is probably OK
  • case 1, 2, 3
  • case 4, 5, 6
  • otherwise
  • end

18
Placeholders
  • 5 points
  • In a sprintf() statement, what do the following
    placeholders mean
  • d, s, f, c, 7.2f
  • Answer
  • d holds the place of an integer
  • s holds the place of a string
  • f holds the place of a float
  • c holds the place of a character
  • 7.2f holds 7 places for a float with two decimal
    places

19
Placeholders, Continued
  • 7.2f means width of 7 with 2 decimal places,
    width includes sign and decimal point
  • - 1 0 0 . 0 0
  • - - - - - - -
  • 4.3d means width of 4 with minimum of 3 digits
    (leading 0s)
  • 0 0 7
  • - - - -
  • 10s means minimum of 10 spaces, right justified
  • H u t c h
  • - - - - - - - - - -

20
String Conversion
  • 5 Points
  • What types of data do these functions convert to
    strings for use in disp()
  • num2str(), char(), int2str()
  • Answer
  • num2str() converts float to string
  • char() converts characters to string
  • int2str() converts integer to string

21
Todays Lessons
  • You should have some idea of what the test will
    be like, and what sorts of answers I am expecting.
Write a Comment
User Comments (0)
About PowerShow.com