150 likes | 163 Views
This text covers embedded functions in C++, including exit(), abs(), pow(), sqrt(), srand(), and rand(). It also discusses validation functions and provides a homework assignment on converting minutes to hours/minutes using functions. Additionally, it includes topics for the midterm review, such as hardware, software, programming structure, data types, conditionals, looping, and functions.
E N D
MSAS5104Programming with Data Structures and Algorithms Week 8 Scott Marino Scott Marino MSMIS Kean University
Embedded Functions Validation Functions Homework Midterm Review Topics Scott Marino MSMIS Kean University
Embedded Functions • Embedded functions are functions that are pre-written and brought into your program in standard header files • C++ has many embedded functions in it’s standard header files • Additional functions can be purchased from third party vendors Scott Marino MSMIS Kean University
Embedded Functions • #include <cstdlib> • Includes an exit function for returning control to the operating system upon program termination • Syntax: exit(PREDEFINED-CONSTANT); • Predefined constants include EXIT_SUCCESS and EXIT_FAILURE • The available constants are usually included with the documentation for the version of the compiler being used Scott Marino MSMIS Kean University
Embedded Functions • #include <cmath> • Included functions for absolute values, exponents, square roots and other trigonometric functions • abs(number-variable); • pow(base-value, exponent); • sqrt(number-variable); Scott Marino MSMIS Kean University
Embedded Functions • Random numbers • srand(seed-value); • rand(); • The C++ random number generator needs a seed value to prime the algorithm • The default seed will generate the same number(s) each time unless a different seed is used Scott Marino MSMIS Kean University
Embedded Functions • The time function makes a good seed value for the random number function • #include <ctime> • srand(time(NULL)); • Will seed the random number generator with a value from the system clock Scott Marino MSMIS Kean University
Embedded Functions • srand(time(NULL));for (ctr = 1; ctr <= 3; ctr++) { cout << rand() << " ";}cout << endl; • The above code will seed the random number generator with a value from the time function, then generate 3 random numbers Scott Marino MSMIS Kean University
Validation Functions • Functions can be used to validate the values contained in variables • Validation functions can return a value of true or false by returning a 1 or a 0 • The invoking program logic can conditionally alter the execution based upon the results of the validation result Scott Marino MSMIS Kean University
Validation Functions • int valid_work_hours(int hours) { if ( (hours < 80) && (hours > 0) ) { return 1; // true } else { return 0; // false }} // end valid_work_hours • If work hours less than 80 and greater than 0, then return true (1) else return false (0) Scott Marino MSMIS Kean University
Validation Functions • if ( valid_work_hours(30) ) { cout << “Valid value” << endl;} else { cout << “Invalid value” << endl; return (0);} • 30 Hours is a valid value so program execution would continue • Invalid values would cause the program to stop early Scott Marino MSMIS Kean University
Homework • All homework from the first half of the semester is due before the midterm • During the second half of the semester, all homework must be handed-in within one week of the original due date Scott Marino MSMIS Kean University
Homework • Write a program to convert minutes into hours/minutes using functions • Prompt the user for the minutes to convert (function) • Calculate the number of hours (function) • Calculate the number of minutes (function) • Print the calculated result • Show test results for 0, 59, 60, 61, 1439 and 1440 • Due THE WEEK AFTER the exam Scott Marino MSMIS Kean University
Midterm Review • Format • Approximately 45 total short answer, true/false and multiple choice questions • 2 blocks of code / partial programs • What to review • Slides - Weeks 1 through 8 • Text - Chapters 1 through 6 Scott Marino MSMIS Kean University
Midterm Review • Topics • Hardware, software, computing history and numbering systems • Programming structure, program design, languages, values and variables • Data types and arithmetic • Conditionals • Looping • Functions, global and local variables Scott Marino MSMIS Kean University