Procedure Orientated Coding - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Procedure Orientated Coding

Description:

Sequence. Selection. Loops. Functions. While Loops. while(condition) ... Conditions. While, If. For. Functions or subprograms. main. Built in functions (sqrt(25) ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 21
Provided by: project53
Category:

less

Transcript and Presenter's Notes

Title: Procedure Orientated Coding


1
Procedure Orientated Coding
  • Sequence
  • Selection
  • Loops
  • Functions

2
While Loops
  • while(condition)
  • statement
  • // Make sure statement eventually meets the
    condition

3
For Loops
  • for(i0ilt45i)
  • statement

4
Conditions
  • While, If
  • lt, gt, lt, gt, , !,
  • For
  • lt, gt , lt, gt

5
Functions or subprograms
  • main
  • Built in functions (sqrt(25))
  • Subprograms
  • Same as a main function in many ways
  • Takes arguments like sqrt root e.g. 25

6
Function call from main
  • include ltblah.hgt
  • //Declare function
  • int FunctionName(int var1,int var2)
  • void main( )
  • int var1,var2,var3
  • var3FunctionName(var1,var2)

7
Function
  • int FunctionName(int v1,int v2)
  • int v3
  • statements
  • return v3

8
Function
  • int FunctionName(int v1,int v2)
  • int v3
  • statements
  • return v3

9
Object Orientated Coding
  • Input and output files

10
  • include ltfstream.hgt //Needed for Input Output
    file streams
  • void main()
  • // Declare file stream
  • ifstream file1
  • // Open file
  • input_file.open("test.dat")
  • // read in inputs
  • input_file gtgt number
  • // Close files
  • input_file.close()

11
  • include ltstdlib.hgt //Needed to exit program
  • char filename21
  • cout ltlt "Please enter the name of the input file
    you wish to open" ltlt endl
  • cin gtgt filename
  • input_file.open(filename,iosnocreate)
  • if(input_file.fail())
  • cout ltlt "\n Input file failed to open, please
    check that the"
  • cout ltlt " file exists" ltlt endl
  • exit(1)

12
Object ifstream functions
  • input_file.open() input_file.eof()
  • input_file.close() output_file.put
  • input_file.fail()
  • input_file.ignore()
  • input_file.get()
  • input_file.getline()
  • input_file.peek()

13
File Options
  • input_file.open(file,iosnocreate)
  • input_file.open(file,iosin)
  • output_file.open(file,iosout)
  • input_file.open(file,iosapp)
  • input_file.open(file,iosate)
  • input_file.open(file,iosbinary)
  • input_file.open(file,iostrunc)
  • input_file.open(file,iosnoreplace)

14
setiosflags(iosoptions)
  • showpos Show
  • scientific Scientific notation
  • dec Decimal base 10
  • oct Octal base 8
  • hex Hexadecimal base 16
  • fixed Show as decimal
  • showpoint Show decimal point
  • right Right Justify

15
stdlib.h
  • exit()

16
Type Coercion
  • Float to Integer Truncates
  • Integer to Float Adds .0
  • Integer to Character Gives ASCII
    number for that integer
  • Float to character Truncates and
    converts to ASCII

17
ASCII
  • Integers 0-9 ASCII 48-57
  • Lower case alphabet a-z ASCII 97-122
  • Upper case alphabet A-Z ASCII 65-90
  • Means can order alphabet in an if
  • if (alt b) //is true
  • if (jltc) // is false

18
Object string functions
  • find () find the argument in the string,
    returns position in string (starting at 0)
  • substr(a,b) returns segment of string from a to
    b
  • length(),size() returns length of string

19
Arrays
  • int number1,number2,number3
  • sumnumber1number2number3
  • Or
  • int number3
  • sumnumber0number1number2

20
Conversion String to Number
  • Need stdlib.h and math.h
  • atof() string to float
  • atoi() string to integer
Write a Comment
User Comments (0)
About PowerShow.com