Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CODE IS IN C DESCRIPTION You are to write a simple C program that will take a decimal number and a base as command line

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

CODE IS IN C

DESCRIPTION You are to write a simple C program that will take a decimal number and a base as command line arguments and output the decimal number in that specified base. For example: $ ./cony 13 2 1101 $ ./conv 236709 8 716245 $ ./cony 1352275 11 843A91 $ ./conv 38572856 64 219Cu $ ./conv 3232236286 256 192.168.2.254 $ ./conv Usage: conv $ ./cony 11111 65 INVALID BASE This is just example output, you should test against as many possible inputs as you can 1. Your program should be able to handle all bases from 2 to 64 and 256. 2 It must have 4 defined functions: int main(int arge, char *argv[]) (every C program must have this) *void other_base (unsigned decimal, unsigned base) (for bases that are not a power of 2 or 256) * void power2_base (unsigned decimal, unsigned base) (for bases that are a power of 2 but still not 256) void base_256(unsigned decimal, unsigned base) (for base 256 only) 3. Call only one of these functions during the execution of your program using the criteria given in parenthesis above. 4. Submit only the following files: Your conv.c source code file 5. Attempt to handle errors. See the example output for cases where no arguments are provided or an invalid base is given. Never trust user input. 6. Use a buffer of 32 characters. This will give you 31 locations for characters and the final location should be a null. 7. Remember: you have to put the digits into the buffer backwards and when the algorithm is finished, you have to printf() from the correct location in that buffer. Converting Integers to Characters Think about how to use this character array: char *ascii = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; The character 'i' is _not the same as the number 1. If you need to know which numeric values correspond to which on screen characters, look at man ascii in your terminal. Division & Modulo This algorithm is suitable for any base that is not a power of two and is not base 256 (other_base () function) Let's see an example of converting the decimal number 29 into base 5. How many different values are there for base 5: 0, 1, 2, 3 and 4. So you should expect all of our digits to be one of those numbers. Get the first digit by calculating the remainder of 29 / 5 5 5 29 25 The result of 29/5 is 5 with a remainder of 4 There is a special operator for this in almost all programming languages: 3 (modulo) operator. This will give the remainder instead of the quotient. 29 $ 5 = 4 This is the first digit (ones digit) of the base 5 number. To continue, you start from the quotient of this operation. Division in C is integer division (just like in Java), so the remainder is discarded when you do: 29 / 5 = 5 Now, continue with the algorithm: 51 5 5 0 The result of 5/5 is 1 with a remainder of 0. 5 $ 5 = 0 This is the second digit (fives digit) of the base 5 number. 5 / 5 = 1 The algorithm continue until this division results in 0: 0 51 1 0 1 Now we have a remainder of 1 and the result of the integer division is 0 The number 29 in base 5 is 104 Mask & Shift This algorithm is suitable for any base that is a power of two and is not base 256 (power2_base () function). What if you are trying to convert the number 7 into base 4? How many bit patterns are there for base 4:00, 01, 10 and 11 which is 0, 1, 2 and 3. Let's see how masking works: 0111 = 7 & 0011 = 3 0011 = 3 You've "masked off the first digit, which is 3, by using the & bitwise AND) operator. Now you can shift those bits to the right using the >> bitwise RIGHT SHIFT) operator like so: 7 >> 2 = 01 And continue with the algorithm: 01 = 1 & 11 = 3 01 = 1 The number 7 in base 4 is 13. Base 256 This algorithm will only be used for base 256 (base_256() function). It will involve using both previous algorithms. Since this is a power of 2, mask & shift must be utilized. However, the values you get will be numbers between 0 and 255. The ascii character array doesn't have that many different characters in it, so instead you will represent base 256 numbers in the same format as IP addresses: 172.16.1.255 Each place value is separated by a character (period, dot). The division & modulo algorithm will be used to calculate each digit of the decimal number for each place value. In the above example, the ones place shows the number 255 which would have been calculated using mask & shift from the input number. Then that value needs to be placed in the buffer as a three digit decimal number by using the division & modulo algorithm. Submission Format Submit your solution as a file called conv.c. Automated Tests The automated tests for this assignment are the following: Compilation This test will simply test that your program builds when make is called. Invalid Input Your program should be able to handle cases where invalid input is passed to the program. In general, it is good practice to never trust user input. You should catch the following cases: No Input If the program is run without any command line arguments, your program should detect it and report it by printing "Usage: ./conv ". Print this string exactly. Also, make sure your program exits with a non-zero exit code. The exit code is set when you return in the main function. Example: $ ./cony Usage: ./conv Invalld Base Example: $ ./conv Usage: ./conv Invalid Base If the user supplies a number for the base argument that is outside our supported range of 2-64 + 256, report it and exit with a non-zero exit code. Print "INVALID BASE" exactly. Example: $ ./conv 12345 77 INVALID BASE $ ./conv 67890 1 INVALID BASE Invalid Number If the user supplies a non-number argument to the argument, print "INVALID NUMBER" exactly and exit with a non-zero exit code. Example: $ ./conv nan 14 INVALID NUMBER Power of 2 bases This test will test specifically bases 2, 4, 8, 16, 32, and 64. You should handle these bases specially in your program with the mask and shift method detailed above. Base 256 This test will test specifically base 256. All Bases This test will test all other bases not tested above, possibly with some overlap (that is, base 4 might still get tested here too). 1 #include 2 #include 3 #include 5 // TODO: what are function prototypes? 6 void other_base(unsigned, unsigned); 7 void power2_base (unsigned, unsigned); 8 void base_256(unsigned, unsigned); 9 10 // this will not change, so we can declare it constant 11 const char *ascii = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; 12 13 // this will change, however 14 char buffer[32]; 15 16 // every c program must implement the main() function 17 int main(int argc, char *argv[]) { // TODO: error check // if (args = 3) then.... V/ properly NULL terminate the buffer buffer[31] = 0; // use atoi() function to translate characters to numbers unsigned decimal = atoi (argv[1]); unsigned base = atoi (argv[2]); 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 decimal++; base++; // TODO: decide which function to call based on 11 the user's desired base other_base(123, 5); // here are two ways to print from certain // location in the buffer, use one of them printf("%s ", buffer + 28); printf("%s ", &buffer [28]); // remember there should only be a SINGLE call // to printfo' in your entire program 41 42 untitled conv.cx w 32 VI TODO: decide which function to call based on 33 // the user's desired base 34 other_base(123, 5); 35 36 V/ here are two ways to print from certain 37 // location in the buffer, use one of them 38 printf("%s ", buffer + 28); 39 printf("%s ", &buffer[28]); 40 VI remember there should only be a SINGLE call 41 // to printf() in your entire program 42 43 // exit with @ if the program was successful return ; 45 } 46 47 void other_base (unsigned decimal, unsigned base) { 48 49 // TODO: for bases that are not a power of 2 or 256 50 51 Il fill the buffer from the end, toward the start 52 buffer [30] = '2'; 53 buffer [29] '1'; 54 buffer [28] = '0'; 55 56 return; 57 } 58 59 void power2_base (unsigned decimal, unsigned base) { 60 61 // TODO: for bases that are a power of 2 but still not 256 62 63 return; 64 } 65 66. void base_256(unsigned decimal, unsigned base) { 67 68 V/ TODO: for base 256 only 69 70 return; 71 } 72 73

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_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

What are oxidation and reduction reactions? Explain with examples

Answered: 1 week ago

Question

4. What advice would you give to Carol Sullivan-Diaz?

Answered: 1 week ago