ICOM 4015 Advanced Programming - PowerPoint PPT Presentation

About This Presentation
Title:

ICOM 4015 Advanced Programming

Description:

12/23/09. ICOM 4015. 1. ICOM 4015. Advanced ... template class T swap(T& a, T& b) ... Define local array to hold input data. const int ArrayLength = 100; ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 8
Provided by: ValuedGate2254
Learn more at: http://www.ece.uprm.edu
Category:

less

Transcript and Presenter's Notes

Title: ICOM 4015 Advanced Programming


1
ICOM 4015 Advanced Programming
  • Lecture 11
  • Static Arrays II
  • Reading Chapter 4

Prof. Bienvenido Velez
2
Static Arrays II Outline
  • Array parameters

3
Example 1 - main function
// Standard C header files include
ltcmathgt include ltiostreamgt // Forward
declarations of local functions template ltclass
Tgt swap(T a, T b) template ltclass Tgt int
readData(istream source, T data, int
maxlen) template ltclass Tgt void
printArray(ostream sink, T a, int
length) template ltclass Tgt void reverseArray(T
a, int length) // Main function int main()
// Define local array to hold input data const
int ArrayLength 100 float numbersArrayLength
0.0 // Inititalize all cells to 0 //
Read data from standard input int counter
readData(cin, numbers, ArrayLength) cout ltlt
"Read " ltlt counter ltlt " numbers" ltlt endl if
(counter 0) return 0 cout ltlt "Original
Array" ltlt endl printArray(cout, numbers,
counter) reverseArray(numbers, counter)
cout ltlt "Reversed Array" ltlt endl
printArray(cout, numbers, counter)
array-params.cc
4
Example 1 - Auxiliary Functions
// Auxiliary local functions template ltclass
Tgt int readData(istream source, T data, int
maxlen) int counter 0 while(counter lt
maxlen) cout ltlt "Next datum " float
nextDatum source gtgt nextDatum if
(source.eof()) break datacounter
nextDatum return counter template
ltclass Tgt void printArray(ostream sink, T a,
int length) for (int i0 iltlength i)
sink ltlt ai ltlt endl template ltclass
Tgt void reverseArray(T a, int length) int
iterations length / 2 for (int i0
iltiterations i) swap(ai, alength - i
- 1) template ltclass Tgt swap(T a, T b)
T temp a a b b temp
array-params.cc
5
Example 1 Output
bvelez_at_amadeus /icom4015/lec11
gtgtarray-params Next datum 1 Next datum 2 Next
datum 3 Next datum 4 Next datum 5 Next datum
6 Next datum 7 Next datum 8 Next datum 9 Next
datum 10 Next datum Read 10 numbers Original
Array 1 2 3 4 5 6 7 8 9 10 Reversed
Array 10 9 8 7 6 5 4 3 2 1 bvelez_at_amadeus
/icom4015/lec11 gtgt
6
Anatomy of an Array Parameter Definition
array name
int f(int a)
number of cells unspecified
type of elements
7
Array Parameters Summary
  • Arrays are passed by reference by default
  • Compiler ignores number of elements in parameter
    definition
  • Programmer typically passes array length as extra
    parameter
Write a Comment
User Comments (0)
About PowerShow.com