Chapter5 - Chapter 5 C Functions 1 Dr Serdar Kirli Software...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Dr. Serdar Kirli 1 Chapter 5 C++ Functions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dr. Serdar Kirli 2 Software Engineering Creating software that Meets requirements Minimizes cost Maximizes reliability Maximizes understandability and modifiability
Background image of page 2
Dr. Serdar Kirli 3 Software Engineering Strategy: Partition the problem into relatively independent subproblems. Modularization: Reduces complexity. Easier to understand and modify. Helps testing and verification. Promotes reusability Define key data structures.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dr. Serdar Kirli 4 Software Engineering MAIN SOLVE GET DATA DISPLAY RESULTS FIND FEASIBLE FIND OPTIMAL FIND DIRECTION FIND DISTANCE
Background image of page 4
Factorial Example #include <iostream> using namespace std; int main( void ) { int n, k, nfact; do { cout << "Enter n or 0 to quit : " ; cin >> n; if (n < 0 ) { cout << "Invalid n entered, " ; cout << "try again." << endl; } else if (n> 0 ) { nfact = 1 ; for (k= 2 ; k<=n; k++) { nfact*=k; } cout << n << "! = " << nfact <<endl; } } while (n != 0 ); return 0 ; }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
#include <iostream> #include <cstdlib> using namespace std; long double nfact( int n); // prototype int main( void ) { int n; do { cout << "Enter n or 0 to quit : "; cin >> n; if (n < 0 ) cout << "Invalid n.\n" ; else if ( n != 0 ) cout << n << "! = " << nfact(n) << endl; } while (n != 0 ); cout << nfact( 3.6 ) << endl; // 6 system( "PAUSE" ); return 0 ; } A Different Approach Using Functions.
Background image of page 6
#include <iostream> #include <cstdlib> using namespace std; long double nfact( int n); // prototype int main( void ) { int n; do { cout << "Enter n or 0 to quit : "; cin >> n; if (n < 0 ) cout << "Invalid n.\n" ; else if ( n != 0 ) cout << n << "! = " << nfact(n) << endl; } while (n != 0 ); cout << nfact( 3.6 ) << endl; // 6 system( "PAUSE" ); return 0 ; } Prototype nfact Function call A Different Approach Using Functions.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dr. Serdar Kirli 8 // Implementation of nfact long double nfact( int n){ long double _nfact; int k; _nfact = 1 ; for (k= 2 ; k<=n; k++) _nfact *= k; return _nfact; } A Different Approach Using Functions.
Background image of page 8
Dr. Serdar Kirli 9 Function: nfact() #include <iostream> #include <cstdlib> using namespace std; long double nfact( int n); int main( void ) { int n; do { cout << "Enter n or 0 to quit : "; cin >> n; if (n < 0 ) cout << "Invalid n.\n" ; else if ( n != 0 ) cout << n << "! = " << nfact(n) << endl; } while (n != 0 ); ... Variable Value n ????????
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dr. Serdar Kirli 10 Function: nfact() #include <iostream> #include <cstdlib> using namespace std; long double nfact( int n); int main( void ) { int n; do { cout << "Enter n or 0 to quit : "; cin >> n; if (n < 0 ) cout << "Invalid n.\n" ; else if ( n != 0 ) cout << n << "! = " << nfact(n) << endl; } while (n != 0 ); ... Variable Value n ????????
Background image of page 10
Dr. Serdar Kirli 11 Function: nfact() #include <iostream> #include <cstdlib> using namespace std; long double nfact( int n); int main( void ) { int n; do { cout << "Enter n or 0 to quit : "; cin >> n; if (n < 0 ) cout << "Invalid n.\n" ; else if ( n != 0 ) cout << n << "! = " << nfact(n) << endl; } while (n != 0 ); ... Variable Value n -2
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dr. Serdar Kirli 12 Function: nfact() #include <iostream> #include <cstdlib> using namespace std; long double nfact( int n); int main( void ) { int n; do { cout << "Enter n or 0 to quit : "; cin >> n; if (n < 0 ) cout << "Invalid n.\n" ; else if ( n != 0 ) cout << n << "! = " << nfact(n) << endl; } while (n != 0 ); ... Variable Value n -2 -2 is less than 0 if expression evaluates to true
Background image of page 12
Dr. Serdar Kirli 13 #include <iostream> #include <cstdlib> using namespace std; long double nfact( int n); int main( void ) { int n; do { cout << "Enter n or 0 to quit : "; cin >> n; if (n < 0 ) cout << "Invalid n.\n" ; else if ( n != 0 ) cout << n << "! = " << nfact(n) << endl; } while (n != 0 ); ...
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 128

Chapter5 - Chapter 5 C Functions 1 Dr Serdar Kirli Software...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online