{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

Dr. Serdar Kirli 1 Chapter 5 C++ Functions

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

View Full Document
Dr. Serdar Kirli 2 Software Engineering Creating software that Meets requirements Minimizes cost Maximizes reliability Maximizes understandability and modifiability
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.

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

View Full Document
Dr. Serdar Kirli 4 Software Engineering MAIN SOLVE GET DATA DISPLAY RESULTS FIND FEASIBLE FIND OPTIMAL FIND DIRECTION FIND DISTANCE
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 ; }

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

View Full Document
#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.
#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.

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

View Full Document
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.
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 ????????

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

View Full Document
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 ????????
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

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

View Full Document
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
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 ); ...

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online