180 likes | 200 Views
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. Float variables.
E N D
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
Float variables • Float Data Type: Data Type that can hold numbers with decimal values e.g. 5.14, 3.14 • Float example
/* 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(); }
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
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
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
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.
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
Control structures • All programs can be written in terms of only 3 control structures: • sequence structure • selection structure • repetition structure
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
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
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”
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.
If/else statement Programmer can specify that different actions are to be performed when the condition is true than when the condition is false
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”
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?
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?