1 / 15

Embedded Functions and Validation Functions: Homework, Midterm Review Topics

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.

vasquezm
Download Presentation

Embedded Functions and Validation Functions: Homework, Midterm Review Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MSAS5104Programming with Data Structures and Algorithms Week 8 Scott Marino Scott Marino MSMIS Kean University

  2. Embedded Functions Validation Functions Homework Midterm Review Topics Scott Marino MSMIS Kean University

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

More Related