CS 325 Software Development and Systems www'bama'ua'edutruit002 - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CS 325 Software Development and Systems www'bama'ua'edutruit002

Description:

Type this code in and run it (or cut and paste) ... Allows us to have two functions with the same name that take in different types of values ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 20
Provided by: janett
Category:

less

Transcript and Presenter's Notes

Title: CS 325 Software Development and Systems www'bama'ua'edutruit002


1
CS 325 Software Development and
Systemswww.bama.ua.edu/truit002
2
Pre-class (just for fun)
  • include ltiostream.hgt
  • void main ()
  • int count 0
  • for (int x 0 xlt100 x5)
  • if (x100)
  • for (int y x yltx5 y)
  • count
  • cout ltlt count ltlt endl

3
Lecture 4
  • Today
  • Functions and Arrays
  • Project 1 Due Wed, 9/15/99 _at_midnight
  • www.bama.ua.edu/truit002
  • Will eventually be on cs.ua.edu/325
  • Test 1 Tuesday, 9/14/99
  • Chapters 1-4 from book
  • Writing C code to solve problems
  • Analyzing existing C code (no use of computers)
  • Misc. questions
  • Next Thursday Practice Session 1

4
So far
  • Sequence
  • Selection
  • if
  • switch (later)
  • Loops
  • for
  • while
  • do (later)

5
Modules in C
  • Goals in software engineering Reusability,
    Maintenance, etc.
  • C breaks code into classes and functions
  • Well talk about classes and their methods after
    1st test
  • Today, we learn to write functions in C

6
Function definition syntax
  • return_type functionId (param_list)
  • function_body
  • Return_type type of return value
  • C convention name functions with verbNoun
    (i.e. averageGrades), however, any valid
    identifier will be valid
  • Param_list list of arguments separated by
    commas
  • Function body any valid declarations and
    statements that fulfill the purpose of the
    function

7
Function Call Syntax
  • functionId(actual_param_list)
  • Pass by value
  • Function gets a copy of the value passed
  • Pass by reference
  • Function gets the location of the value passed,
    therefore, function has right to change the
    contents at that location

8
Function Prototype
  • If the function is defined after main(), there
    must be a way for the compiler to recognize a
    call to that function within the body of the main
    program function prototype
  • return_type functionID(type_for_each_parameter)
  • This is unnecessary if the function is defined
    before main()

9
Simple Example
  • include ltiostream.hgt
  • int getMin (int,int )
  • void main()
  • int num,temp, min
  • cout ltlt How many grades do you want to enter?
    ltlt endl
  • cin gtgt num
  • cout ltlt enter the first number ltlt endl
  • cin gtgt min
  • for (int x 0 x lt num-1 x)
  • cin gtgt temp
  • mingetMin(min,temp)
  • cout ltlt setw(7) ltltmin ltlt endl
  • // end of main function

10
Simple example contd
  • int getMin(int num1, int num2)
  • if (num1 gt num2)
  • return num2 // new min
  • else
  • return num1

11
Class Exercises
  • Type this code in and run it (or cut and paste)
  • Write another function that will find the maximum
    value

12
Arrays
  • List of common elements
  • In most situations, we would want to keep grades.
    In our example, we simply discarded the grade
    after we compared it. We need to maintain the
    variables in a list, or an array

13
Arrays in C
  • element_type idarray_size
  • Array notes
  • All subscripts begin at 0.
  • If array_size is 100, the range for the array is
    0..99.
  • Access an array by idindex
  • Each element may be treated as a variable

14
Initialization options
  • int exsize 0 //all elements set to 0
  • int ex5 1,2,3,4,5 // position 0 set to 1,
    Position 1 set to 2, so on
  • int ex 1,2 //will set array size to 2 with
    range 0..1
  • int exsize
  • Use for loop to initialize

15
Simple Example
  • include ltiostream.hgt
  • void main()
  • const int maxSize30
  • int num, gradesmaxSize
  • cout ltlt how many grades do you wish to enter?
    ltlt endl
  • cin gtgt num
  • for (int position 0 position lt num
    position)
  • cout ltlt enter a grade ltlt endl
  • cin gtgt gradesposition

16
Class Exercises
  • Type (cut and paste) program and try it
  • Write the following functions
  • findMin(array, numEl) ? min
  • findMax(array, numEl) ? max
  • findAvg(array, numEl) ? avg

17
Operator Overloading
  • Allows us to have two functions with the same
    name that take in different types of values

18
One more class exercise
  • Using same program, take the code out of the main
    program and write a function called getGrades to
    get the grades from the user
  • Now write another getGrades, only do so with an
    array of doubles.

19
End of Lecture 4
  • Project 1 due Wed, 9/15/99 _at_ midnight
  • www.bama.ua.edu/truit002
  • Will eventually be on cs.ua.edu/325
  • Test 1 Tuesday, 9/14/99
Write a Comment
User Comments (0)
About PowerShow.com