Sign In
Not register? Register Now!
You are here: HomeOther (Not Listed)Engineering
Pages:
3 pages/≈825 words
Sources:
No Sources
Level:
Other
Subject:
Engineering
Type:
Other (Not Listed)
Language:
English (U.S.)
Document:
MS Word
Date:
Total cost:
$ 16.2
Topic:

C++ Code (Other (Not Listed) Sample)

Instructions:
As I am a mechtronics engineer, i also accept programming orders. like this one. i was asked to write a c++ code for the question and add explaination as well. the question statement is below and attached is the code i wrote. Requirements: "Write a function using the following specification Function Name: printDigit() Arguments: One integer input parameter The valid inputs are numbers from 0 to 100 inclusive. The function should display output for each number using the word associated with the digit. For the numbers 20-99 the function MUST use an algorithm to determine the tens word to display and the ones word to display. It must not use a decision or selection statement that matches each number in the range between 20-99. The function should return an integer value as follows: 0 if the function return is success -1 if the function encountered an error (incorrect input for example). The program you submit should include the code for the function described above along with code in main to test the function. Test your program using values from -1 to 101. Use a for loop to call the function with each of the values. -1 and 101 should result in the function returning an error and the function should not print anything in those cases. Your test code should display the return value from the function each time it is called. The program should be well commented and contain all of the test cases in the main function. The program should use the cout object for output." source..
Content:
#include <iostream> using namespace std; /* Function name: printDigit Parameters: A single integer parameter Returns: 0 if the function return is success -1 if the function encountered an error (incorrect input for example). */ //Function prototypes int printDigit(int); int main() { int num; int ret; //Test Cases for (int x = -1; x < 102; x++) { num = x; ret = printDigit(num); cout << "Result = " << ret << endl; } return 0; } /* Function name: printDigit Parameters: A single integer parameter Returns: 0 if the function return is success -1 if the function encountered an error (incorrect input for example). */ int printDigit(int num) { //Initialize variables string ones[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; string teens[10] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; string tens[9] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; //Check for valid input if (num < 0 || num > 100) { return -1; } //Check for specific numbers else if (num == 0) { cout << "Zero" << endl; } else if (num == 10) { cout << "Ten" << endl; } else if (num == 100) { cout << "One Hundred" << endl; } //Check for number between 1 and 9 inclusive else if (num < 10) { cout << ones[num] << endl; } //Check for teens else if (num > 9 && num < 20) { cout << teens[num - 10] << endl; } //Check for numbers between 20 and 99 inclusive else if (num > 19 && num < 100) { cout << tens[num / 10 - 2]; if (num % 10 != 0) { cout << "-" << ones[num % 10]; } cout << endl; } cout << "There are " << num / 10 << " tens." << endl; return 0; } Explanation 1. int printDigit(int); This line declares a function called printDigit that takes a single integer parameter. 2. int num; This line declares a variable called num of type int. 3. int ret; This line declares a variable called ret of type int. 4. for (int x = -1; x < 102; x++) This line creates a for loop that iterates from -1 to 102. 5. num = x; This line assigns the value of x to the variable num. 6. ret = printDigit(num); This line calls the function printDigit with the parameter num and assigns the return value to the variable ret. 7. cout << "Result = " << ret << endl; This line prints the string "Result = " followed by the value of the variable ret and a newline. 8. return 0; This line returns the value 0 from the main function. 9. } This line ends the main function. 10. int printDigit(int num) This line begins the definition of the function printDigit. 11. { This line begins the body of the function printDigit. 12. string ones[10] = {"zero", "one", "two", "three", This line declares an array of strings called ones with 10 elements. The first element is "zero", the second element is "one", and so on. 13. "four", "five", "six", "seven", "eight", "nine"}; This line continues the declaration of the array of strings called ones. The fourth element is "four", the fifth element is "five", and so on. 14. 15. string teens[10] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", This line declares an array of strings called teens with 10 elements. The first element is "ten", the second element is "eleven", and so on. 16. "sixteen", "seventeen", "eighteen", "nineteen"}; This line continues the declaration of the array of strings called teens. The seventh element is "sixteen", the eighth element is "seventeen", and so on. 17. 18. string tens[9] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", This line declares an array of strings called tens with 9 elements. The first element is "twenty", the second element is "thirty", and so on. 19. "eighty", "ninety"}; This line continues the declaration of the array of strings called tens. The eighth element is "eighty", the ninth element is "ninety". 20. 21. //Check for valid input This line is a comment that says, "Check for valid input". 22. if (num < 0 || num > 100) This line checks if the value of the variable num is less than 0 or greater than 100. 23. { This line begins the body of the if statement. 24. return -1; This line returns the value -1 from the function printDigit. 25. } This line ends the body of the if statement. 26. //Check for specific numbers This line is a comment that says, "Check for specific numbers". 27. else if (num == 0) This line checks if the value of the variable num is equal to 0. 28. { This line begins with the body of the else if statement. 29. cout << "Zero" << endl; This line prints the string "Zero" followed by a newline. 30. } This line ends the body of the else if statement. 31. else if (num == 10) This line checks if the value of the variable num is equal to 10. 32. { This line begins the body of the else if statement. 33. cout << "Ten" << endl; This line prints the string "Ten" followed by a newline. 34. } This line ends the body of the else if statement. 35. else if (num == 100) This line checks if the value of the variable num is equal to 100. 36. { This line begins the body of the else if statement. 37. cout << "One Hundred" << endl; This line ...
Get the Whole Paper!
Not exactly what you need?
Do you need a custom essay? Order right now:

Other Topics:

  • Fog Computing: A Comprehensive Literature Review
    Description: The introduction of fog computing is necessitated by the considerable drawbacks associated with cloud computing architectures. Cloud computing refers to the process of running computer services and resources such as memory in remote servers hosted by third-parties. The cloud paradigm has been widel...
    10 pages/≈2750 words| 30 Sources | Other | Engineering | Other (Not Listed) |
  • An Example of a Placement Report
    Description: The industrial attachment training is an essential component of the curriculum of The Technical University of Kenya final year. The mission of this program is to prepare and motivate students in their respective disciplines so equipped to gain entrance into and succeed chosen work areas after graduation. ...
    65 pages/≈17875 words| 5 Sources | Other | Engineering | Other (Not Listed) |
  • Astronomy and Astrophysics
    Description: Question #1 Given data Semi major axis of the mars, a=1.5 AU At this point, we can apply Kepler’s Third Law, which states that the ratio of the squares of the orbital period for two planets is equal to the ratio of the cubes of their mean orbit radius T2=a3 Where T is the Time period in Earth Years and...
    1 page/≈275 words| No Sources | Other | Engineering | Other (Not Listed) |
Need a Custom Essay Written?
First time 15% Discount!