Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please use C language. I need a program that can convert any number into any base number between 2 and 36. Strings: print_integer(...) Goals This

Please use C language. I need a program that can convert any number into any base number between 2 and 36.

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Strings: print_integer(...) Goals This assignment has the following goals: Goals Overview Prepare Instructions Pro tips (optional) Test-driven development Requirements Pre-tester 1. Understand number bases 2. Understand how printed representations of values relate to in-memory representations 3. Practice designing simple algorithms 4. Get a gentle introduction to test-driven development Overview You will create a function called print_integer (.) that prints a given integer in a specified number base. This is the first step toward future assignments in which you will implement your own version of printf(..) and sprintf(..). You will use your code from this assignment in the future assignments. Prepare Read this assignment description carefully, including the Code Quality Standards. Next, make sure you understand each of the topics below. You may need to do a bit of external reading. We have suggested several concise references to help you get up to speed. #include The #include directive is effectively replaced with the entire contents of the file it refers to. See pp. 90-91 in the textbook or the links on the Resources page. #include guards You will not need to use #include guards for this assignment, but you will see them in the header file (hw02.h). See the link on the Resources page. number bases For this assignment, you will need a good understanding of number bases. You will be writing code to express integers in a variety of number bases (e.g., binary, hexadecimal, etc.). However, most of the algorithms you see online will give you the digits from right-to-left. You will need to print them left-to-right. See pp. 134-137 in the textbook or the links on the Resources page. fputc..., stdout) To print a single character (ch) to the console (stdio), use this code: fputc(ch, stdout); That's all you need to know, for now. Some may ask how this is different from putchar(..). It is equivalent, but please use the fputc(...) form. If you would like to know more about fputc(..), see p. 659 in the textbook, the link on the Resources page, or simply type man fputc from bash. Instructions Start by getting the files. Type 264get hwO2 and then cd hwo2 from bash. You will find only one file: hw02.h. That is the header file. It defines the signature of the function you will implement: print_integer(.). You will be creating three new files: hw02.c, hwo2test.c, and hwo2test.txt. There is no starter code (other than the header file). hwo2.c is where your print_integer (...) implementation will go. To start this file, simply copy the function signatures from hw02.h into a new file called hw02.c and then add the braces to make them into functions. hwo2test.c will contain a main() function that will test your print_integer (.). hwo2test.txt will contain the expected output from running your tests in hwo2test.c. It will be a simple text file. To test if your code works perfectly, you will run the following: # compile your test together with your hwo2.c to create an executable called hwo2test GCC -o hwo2test hwo2test.c hw02.c # run your executable and send the output to a file called hwo2test.actual.txt (That filename is arbitrary.) ./hw02 test > hwO2test.actual.txt # compare the actual output with the expected output using the diff command diff hwo2test.actual.txt hw02 test.txt The diff command prints the differences so if you see any output at all, then your test failed. If you see no output, then it passed. Test-driven development The most efficient way to complete this assignment will be to follow a the test-driven development method. First, you will need to set up a skeleton hw02.c, hwo2test.c, and hwo2test.txt. Your print_integer (..) will do nothing. Your hwo2test.txt will be empty. Therefore, the test procedure described above will appear to pass. Now, repeat the following 2-step process until the assignment is complete. 1. Add a single test to your hwo2test.c and hwo2test.txt files. You will want to start with print_integer (...), so add a single call to print_integer(..) to your main((.) and a corresponding line in your hwo2test.txt. 2. Add just enough to your print_integer (.) so that it can pass that test. Hint: To print the digit 9, you can simply add the character literal for 'O' to the number 9, i.e., 'O' + 9. To the C compiler, there is almost no difference between 'O' and the number 48 (ASCII value of 'O'), so to that expression is the same as 48 + 9, which would give you the ASCII value for the digit '9'. 3. Save a copy of your code by running 264submit hwo2 hw02* every time you get a new part of the functionality working. You will want to start with very simple test cases, e.g., print_integer(1, 20, ""). Then, add a test for something slightly more complicated (e.g., multi-digit number) and implement enough of print_integer (.) to make that work. Keep adding tests and the corresponding code until print_integer(..) handles everything. hwo2test.c functions main(int argc, char* argv[]) return type: int Test your print_integer(-). Your main() must return EXIT_SUCCESS. hwo2test.txt output Expected output from running your hwo2test.c. Either type the function signatures into your hw02.c or copy from your hw02.c. Copying from this web page won't work. 2. Your hwo2test.c must exercise all functionality (e.g., negative numbers, any radix between 2 and 36, largest possible n, smallest possible n, etc.). 3. Only the following external header files, functions, and symbols are allowed in your hw02.c. That means you may use printf(.) in your hwo2test.c but not in your hw02.c. You may use fputc() and stdout in either one (or both). header functions/symbols allowed in... stdio.h fputc,stdout hwo2.c, hwo2test.c stdio.h printf hwo2test.c stdlib.h EXIT_SUCCESS hwo2test.c limits.h INT_MAX, INT_MIN hwO2test.c stdbool.h bool, true, false hwo2.c, hwo2test.c All others are prohibited unless approved by the instructor. Feel free to ask if there is something you would like to use. 4. Repeat: Do not call printf(..) 5. Do not make any assumptions about the size of an int. 6. Submissions must meet the code quality standards and the course policies on homework and academic integrity. How much work is this? This is a little harder than it looks. Getting your print_integer (9) to work properly with any valid int is a puzzle that you need to solve. However, it does not require very much code. For example, the instructor's solution for print_integer (..) is only 20 sloc*. cloc reports 20 sloc for the whole file. * sloc = "source lines of code" (excluding comments and blank lines) Here's a screenshot of all three files that you will be turning in. (This screenshot was taken with a version of the code that had a bug. It didn't handle INT_MIN properly. Fixing the bug added another 4 lines to the code.) Your test cases must be your own. Do not copy test cases from here or anywhere else. We are sharing a few test cases so you can see how this works. In general, our code is not fair game unless explicitly stated otherwise. See the Syllabus. 1 #include 2 #include "hw02.h" void print_integer(int n, int radix, char* prefix) { 18 19 20/ /* vim: set tabstop=2 shiftwidth=2 noexpandtab 1 NORMAL >> hw02.c 1 #include 2 #include 3 #include "hw02.h" C hwO2test.actual.txt # compare the actual output with the expected output using the diff command diff hwo2test.actual.txt hw02 test.txt The diff command prints the differences so if you see any output at all, then your test failed. If you see no output, then it passed. Test-driven development The most efficient way to complete this assignment will be to follow a the test-driven development method. First, you will need to set up a skeleton hw02.c, hwo2test.c, and hwo2test.txt. Your print_integer (..) will do nothing. Your hwo2test.txt will be empty. Therefore, the test procedure described above will appear to pass. Now, repeat the following 2-step process until the assignment is complete. 1. Add a single test to your hwo2test.c and hwo2test.txt files. You will want to start with print_integer (...), so add a single call to print_integer(..) to your main((.) and a corresponding line in your hwo2test.txt. 2. Add just enough to your print_integer (.) so that it can pass that test. Hint: To print the digit 9, you can simply add the character literal for 'O' to the number 9, i.e., 'O' + 9. To the C compiler, there is almost no difference between 'O' and the number 48 (ASCII value of 'O'), so to that expression is the same as 48 + 9, which would give you the ASCII value for the digit '9'. 3. Save a copy of your code by running 264submit hwo2 hw02* every time you get a new part of the functionality working. You will want to start with very simple test cases, e.g., print_integer(1, 20, ""). Then, add a test for something slightly more complicated (e.g., multi-digit number) and implement enough of print_integer (.) to make that work. Keep adding tests and the corresponding code until print_integer(..) handles everything. hwo2test.c functions main(int argc, char* argv[]) return type: int Test your print_integer(-). Your main() must return EXIT_SUCCESS. hwo2test.txt output Expected output from running your hwo2test.c. Either type the function signatures into your hw02.c or copy from your hw02.c. Copying from this web page won't work. 2. Your hwo2test.c must exercise all functionality (e.g., negative numbers, any radix between 2 and 36, largest possible n, smallest possible n, etc.). 3. Only the following external header files, functions, and symbols are allowed in your hw02.c. That means you may use printf(.) in your hwo2test.c but not in your hw02.c. You may use fputc() and stdout in either one (or both). header functions/symbols allowed in... stdio.h fputc,stdout hwo2.c, hwo2test.c stdio.h printf hwo2test.c stdlib.h EXIT_SUCCESS hwo2test.c limits.h INT_MAX, INT_MIN hwO2test.c stdbool.h bool, true, false hwo2.c, hwo2test.c All others are prohibited unless approved by the instructor. Feel free to ask if there is something you would like to use. 4. Repeat: Do not call printf(..) 5. Do not make any assumptions about the size of an int. 6. Submissions must meet the code quality standards and the course policies on homework and academic integrity. How much work is this? This is a little harder than it looks. Getting your print_integer (9) to work properly with any valid int is a puzzle that you need to solve. However, it does not require very much code. For example, the instructor's solution for print_integer (..) is only 20 sloc*. cloc reports 20 sloc for the whole file. * sloc = "source lines of code" (excluding comments and blank lines) Here's a screenshot of all three files that you will be turning in. (This screenshot was taken with a version of the code that had a bug. It didn't handle INT_MIN properly. Fixing the bug added another 4 lines to the code.) Your test cases must be your own. Do not copy test cases from here or anywhere else. We are sharing a few test cases so you can see how this works. In general, our code is not fair game unless explicitly stated otherwise. See the Syllabus. 1 #include 2 #include "hw02.h" void print_integer(int n, int radix, char* prefix) { 18 19 20/ /* vim: set tabstop=2 shiftwidth=2 noexpandtab 1 NORMAL >> hw02.c 1 #include 2 #include 3 #include "hw02.h" C

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2000 Database Design

Authors: Louis Davidson

1st Edition

1861004761, 978-1861004765

More Books

Students also viewed these Databases questions