structured program development dilshad m. shahid new york university @1998

18
Structured Program Development Dilshad M. Shahid New York University @1998

Post on 21-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Structured Program Development Dilshad M. Shahid New York University @1998

Structured Program Development

Dilshad M. Shahid

New York University

@1998

Page 2: Structured Program Development Dilshad M. Shahid New York University @1998

Today

• Float Variables

• How to use Borland

• Equality and Relational Operators

• Algorithms

• Pseudocode

• Control Structures

Page 3: Structured Program Development Dilshad M. Shahid New York University @1998

Float variables

• Float Data Type: Data Type that can hold numbers with decimal values e.g. 5.14, 3.14

• Float example

Page 4: Structured Program Development Dilshad M. Shahid New York University @1998

/* Float Example Program */

#include <stdio.h>

main ()

{

float var1, var2, var3, sum;

var1 = 87.25;

var2 = 92.50;

var3 = 96.75;

sum = var1 + var2 + var3;

printf(“Sum: %.2f”, sum);

getchar();

}

Page 5: Structured Program Development Dilshad M. Shahid New York University @1998

How to use Borland C++

• Borland comes with 3 disks. Please use the 5.0 disk.

• ACF labs have correct version installed

• A new text page automatically when you open Borland

• Alternatively, you can go to menu option File, then New, then Text Edit

Page 6: Structured Program Development Dilshad M. Shahid New York University @1998

More Borland

• Use Save As to save to your disk on the A:\ drive

• After you type in your program, there are 3 ways to compile:– click lightning bolt– go to menu option Debug, then Run– Hit Control-F9

Page 7: Structured Program Development Dilshad M. Shahid New York University @1998

Relational and equality operators

• table adapted from Figure 2.12, pg 38

• standard algebraic in C example in C meaning of C condition• Equality = == x == y x is equal to y• = != x != y x is not equal to y•

• Relational > > x > y x is greater than y• < < x < y x is less than y• > >= x >= y x is greater than or equal

to y• < <= x <= y x is less than or equal to y

Page 8: Structured Program Development Dilshad M. Shahid New York University @1998

Algorithms

• Algorithm – the procedure for solving a problem in terms of – the actions to be executed – the order in which these actions are to be

executed.

• See pages 56 to 57 for a more detailed description.

Page 9: Structured Program Development Dilshad M. Shahid New York University @1998

Pseudocode

• Pseudocode – this is simply writing your code in ordinary English to help yourself develop an algorithm that will be converted into a structured C program.

• More examples in the text book

Page 10: Structured Program Development Dilshad M. Shahid New York University @1998

Control structures

• All programs can be written in terms of only 3 control structures:

– sequence structure– selection structure– repetition structure

Page 11: Structured Program Development Dilshad M. Shahid New York University @1998

Sequence structure

• This is essentially built into C

• Unless directed otherwise, the computer will automatically execute C statements one after another in the order in which they are written

• This is called sequential execution

Page 12: Structured Program Development Dilshad M. Shahid New York University @1998

Selection structure

• A selection structure will perform an action based on the conditions it receives

• 3 kinds of selection structures in C– if – if/else – switch

Page 13: Structured Program Development Dilshad M. Shahid New York University @1998

If statement

Performs indicated action only when condition is true; otherwise the action is skipped.

Example in pseudocode:

If bank balance is less than 100

Print “You are below the required minimum”

Page 14: Structured Program Development Dilshad M. Shahid New York University @1998

If statement

Same example in C:

int balance;

balance = 90;

if (balance < 100)

printf(“You are below the required minimum balance\n”);

What will the output be? Change the program so that balance = 110. What will the output be in this case?

Answer: You are below the required minimum balance

No output.

Page 15: Structured Program Development Dilshad M. Shahid New York University @1998

If/else statement

Programmer can specify that different actions are to be performed when the condition is true than when the condition is false

Page 16: Structured Program Development Dilshad M. Shahid New York University @1998

If/else statement

Example in pseudocode:

If bank balance is less than 100

Print “You are below the required minimum”

else

Print “You may withdraw money”

Page 17: Structured Program Development Dilshad M. Shahid New York University @1998

If/else statement

Same example in C:

int balance;

balance = 90;

if (balance < 100)

printf(“You are below the required minimum balance\n”);

else

printf(“You may withdraw money\n”);

Make balance equal 120, i.e. greater than 100. What will the output be?

Page 18: Structured Program Development Dilshad M. Shahid New York University @1998

If/else statement

Another example in C:

int age;

float height;

age = 10;

height = 5.0;

if (height >= 4.0) /* must have 4.0, it is float*/

printf(“Your height is %f\n”, height);

else

printf(“Your age is %d\n”, age);

What will output be if height = 4.0 ? Does changing value of age affect the program output?