fundamental programming
Download
Skip this Video
Download Presentation
Fundamental Programming

Loading in 2 Seconds...

play fullscreen
1 / 36

Fundamental Programming - PowerPoint PPT Presentation


  • 115 Views
  • Uploaded on

Fundamental Programming. Data Processing and Expressions. Status. this week we are starting to: developing familiarity with the C++ language developing skill in software development (in lab) providing an introduction to some fundamental programming concepts

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Fundamental Programming' - knox


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
fundamental programming

Fundamental Programming

Data Processing and Expressions

Fundamental Programming 310201

status
Status
  • this week we are starting to:
    • developing familiarity with the C++ language
    • developing skill in software development (in lab)
    • providing an introduction to some fundamental programming concepts
  • in the last class we started to look at some C++ syntax – the grammar rules
  • today we look at some fundamental concepts common to all programming languages

Fundamental Programming 310201

review
Review
  • a program performs a sequence of input, output and assignment statements
  • selection and repetitionstatements control which of the program’s input, outputand assignment statements are performed
    • selection(if-then-else) statements provide alternate pathways through the program
    • repetition(while) statements control the number of times a blockof statements is performed

Fundamental Programming 310201

data processing
Data Processing
  • we said that computers are data processing devices – they convert input data into output data
  • where does all the data processing occur in a program?
  • recall our sample program...

Fundamental Programming 310201

sample program
Sample Program

write “Number of marks: “

read NbrMarks

write “Student’s mark: “

read StudentMark

set ProportionOfMarks to StudentMark / NbrMarks

set PercentageOfMarks to ProportionOfMarks * 100

write “ Student’s percentage: “

write PercentageOfMarks

  • in this program, all the data processing occurs in assignmentstatements

Fundamental Programming 310201

anatomy of an assignment
Anatomy of an Assignment
  • let’s analyse the assignment statement…
  • here, the two assignment statements are:

set ProportionOfMarks to StudentMark / NbrMarks

set PercentageOfMarks to ProportionOfMarks * 100

  • that is:

set < variable > to < value of interest >

  • here, the values of interest are:

StudentMark / NbrMarks

ProportionOfMarks * 100

Fundamental Programming 310201

expressions
Expressions
  • we use the term expressionto mean:
    • the description of a value of interest
  • we describe the value that we wish to assign to a data object in an expression
  • so:

StudentMark / NbrMarks

ProportionOfMarks * 100

  • are two expressions

Fundamental Programming 310201

data processing1
Data Processing
  • so, where does the data processinghappen?
  • answer: someof ithappens in

assignmentstatements

  • it can also happen in outputstatements…

Fundamental Programming 310201

alternative design
Alternative Design

write “Number of marks: “

read NbrMarks

write “Student’s mark: “

read StudentMark

set ProportionOfMarks toStudentMark / NbrMarks

write “ Percentage: “

write ProportionOfMarks * 100

Fundamental Programming 310201

anatomy of an output
Anatomy of an Output
  • the anatomy of our assignment statement is:

set < variable > to< expression >

  • the anatomy of our output statement is:

write < expression >

  • so, where does all the data processing happen?

Expressions!

Fundamental Programming 310201

expressions1
Expressions
  • clearly, expressions are important - that’s where the dataprocessing happens
  • let’s take a closer look at expressions
  • previously, we said that data was numbers and text -for now, we just deal with expressions to process numbers
  • the anatomy of an expressionis one we’ve seen before...

Fundamental Programming 310201

expressions as a black box
Expressions as a Black Box
  • we can think of an expression as a black box
  • expressions have one or more input values and produce one output value - theinput-process-output model again
  • example: StudentMark / NbrMarks

input process output

StudentMark ?

NbrMarks(a single value -

depends on inputs)

Fundamental Programming 310201

operators
Operators
  • we use the term operatorto mean:
    • a symbol, or name, used to represent an operationthat can be performed on data
  • in the two expressions:

StudentMark / NbrMarks

ProportionOfMarks * 100

  • the operators are:
    • / for division
    • * for multiplication
  • + and -are used for addition and subtraction
  • +, -, *, / all work in C++ as you would expect

Fundamental Programming 310201

operands
Operands
  • we use the term operandto mean:
    • an input to an expression
  • in the two expressions:

StudentMark / NbrMarks

ProportionOfMarks * 100

  • the operands are:
    • StudentMark and NbrMarks
    • ProportionOfMarks and100

Fundamental Programming 310201

binary operators
Binary Operators
  • in the following examples:

StudentMark / NbrMarks

ProportionOfMarks * 100

NbrMarks - StudentMark

StudentMark + 10

  • each operator is used with twooperands
  • so / , * , - and + are binaryoperators –they can all be used with twooperands

Fundamental Programming 310201

unary operators
Unary Operators
  • the + and - operators are also unaryoperators (they can be used with just one operand)
  • examples:

-273.15as inset AbsoluteZero to-273.15

+100as inset BoilingPointOfWater to+100

expression

- 273.15

operand

operator

Fundamental Programming 310201

numeric expressions
Numeric Expressions
  • expressions that evaluateto a number are called numericexpressions
  • numeric expression come in all shapes and sizes:
    • a number by itself – a literal:

set NbrTimesTold to0

    • the name of a variable:

write Percentage

    • expressions that use operators:

set NbrTimesTold toNbrTimesTold + 1

Fundamental Programming 310201

power of expressions
Power of Expressions
  • the arithmeticoperators+, -, * and / give us a powerful language to process numbers
  • the power comes from the ability to nestlittle expressions inside bigger expressions
  • instead of:

set ProportionOfMarks toStudentMark / NbrMarks

write ProportionOfMarks * 100

  • we can write:

write StudentMark / NbrMarks * 100

  • question: which operator is applied first here? and, does it matter?

Fundamental Programming 310201

nested expressions
Nested Expressions
  • which operator is applied first here?
  • is the division first?

StudentMark / NbrMarks* 100

divide StudentMark by NbrMarks, then multiply by 100

  • or is the multiplication first?

StudentMark / NbrMarks *100

multiply NbrMarks by 100, then divide StudentMark by result of multiplication

  • Activity: does it matter?

Fundamental Programming 310201

activity break
Activity Break

Fundamental Programming 310201

activity feedback
Activity Feedback
  • using StudentMark = 50, NbrMarks = 100…
  • division first:

(StudentMark / NbrMarks) * 100

= (50 / 100) * 100

= 50

  • multiplication first:

StudentMark / (NbrMarks *100)

= 50 / (100 * 100)

= 0.005

  • will a C++ program do it in the correct order?

Fundamental Programming 310201

order of use
Order of Use
  • there are rules to decide the order in which operators in an expression are applied
    • unary operators before binary operators
    • multiplication (*) and division (/) before addition (+) and subtraction (-)
    • otherwise, left to right
  • evaluate the following:

4 * -2 + 3

2 + 12 / 4 * 3

  • will the following be evaluated correctly?

StudentMark / NbrMarks * 100

Fundamental Programming 310201

activity break1
Activity Break

Fundamental Programming 310201

activity feedback1
Activity Feedback
  • evaluate:

4 * -2 + 3

    • unary operator first (- applies to 2)
    • * multiplication before addition

(4 * -2) + 3

= -8 + 3

= -5

Fundamental Programming 310201

activity feedback2
Activity Feedback
  • evaluate the following:

2 + 12 / 4 * 3

    • multiplication and division before addition
    • left to right otherwise – so division before multiplication here

2 + (12 / 4) * 3

= 2 + 3 * 3

    • multiplication before addition

= 2 + (3 * 3)

= 2 + 9

= 11

Fundamental Programming 310201

activity feedback3
Activity Feedback
  • will the following be evaluated correctly?

StudentMark / NbrMarks * 100

  • yes it will – since the division occurs before the multiplication, this is the same as:

(StudentMark / NbrMarks)* 100

Fundamental Programming 310201

order of use1
Order of Use
  • avoid errors by using parentheses:

(4 * -2) + 3

2 + ( ( 12 / 4 ) * 3 )

  • sometimes you can rewritean expression to make it easier to read – instead of:

StudentMark / NbrMarks * 100

  • you can write:

100 * StudentMark / NbrMarks

  • is this easier to understand? if so, why?

Fundamental Programming 310201

activity break2
Activity Break

Fundamental Programming 310201

activity feedback4
Activity Feedback
  • the expression:

100 * StudentMark / NbrMarks

  • may seem easier to read than:

StudentMark / NbrMarks * 100

  • possibly because, in the first expression above, the order in which operators are applied doesn’t matter

– left for student to check

  • always keep you code as simple as possible

Fundamental Programming 310201

activity
Activity
  • the following program is designed to convert temperatures between Fahrenheit and Centigrade
  • it has a logic error – fix it…

Fundamental Programming 310201

slide31
#include

using namespace std;

int main (void)

{

intConversionType = 0;

float Temperature = 0;

cout << "Select conversion - (1) C to F, (2) F to C ==> ";

cin >> ConversionType;

cout << "Input temperature ==> ";

cin >> Temperature;

if (ConversionType == 1)

{

cout << 32 + Temperature * 1.8;

cout << " degrees Fahrenheit";

}

else

{

cout << Temperature - 32 / 1.8;

cout << " degrees Centigrade";

}

getchar();

return 0;

}

Fundamental Programming 310201

activity break3
Activity Break

Fundamental Programming 310201

feedback
Feedback

#include

using namespace std;

int main (void)

{

intConversionType = 0;

float Temperature = 0;

cout << "Select conversion - (1) C to F, (2) F to C ==> ";

cin >> ConversionType;

cout << "Input temperature ==> ";

cin >> Temperature;

if (ConversionType == 1)

{

cout << 32 + Temperature * 1.8;

cout << " degrees Fahrenheit";

}

else

{

cout << Temperature - 32 / 1.8;

cout << " degrees Centigrade";

}

getchar();

return 0;

}

problem here:

division occurs

before subtraction

Fundamental Programming 310201

feedback1
Feedback

#include

void main (void)

{

intConversionType = 0;

float Temperature = 0;

cout << "Select conversion - (1) C to F, (2) F to C ==> ";

cin >> ConversionType;

cout << "Input temperature ==> ";

cin >> Temperature;

if (ConversionType == 1)

{

cout << 32 + (Temperature * 1.8);

cout << " degrees Fahrenheit";

}

else

{

cout << (Temperature – 32) / 1.8;

cout << " degrees Centigrade";

}

}

clarification:

parentheses make

intention clear

a solution:

enclose subtraction

in parentheses

Fundamental Programming 310201

c syntax summary
C++Syntax Summary
  • input: cin >> ;
  • output: cout << ;
  • assignment: = ;
  • aselection statement:

if ( )

{ }

else

{ }

  • a repetition statement:

while( )

{}

Fundamental Programming 310201

summary
Summary
  • data processing happens in expressions
  • expressions appear in assignmentand output statements
  • different types of expressions – literals, variables names, ones that use operators…
  • arithmetic operators are: +, -, *, /
  • rules control order of application
  • parentheses are used to impose ordering
  • computing has a lot of jargon!

Fundamental Programming 310201

ad