Question: C code no print statements AGAIN NO PRINTF STATEMENTS TEST FILE AT BOTTOM Return the character corresponding to a value. @param radix - the base

C code no print statements AGAIN NO PRINTF STATEMENTS

TEST FILE AT BOTTOM

Return the character corresponding to a value. @param radix - the base you are working in (2-36) @param value - a value in the range of 0 - (radix-1) @return - character '0'..'9' or 'A'..'Z' corresponding to the given value if the value is legal in the given radix. Otherwise return character '?' char int2char (int radix, int value);

----------------------------------------------------------------------------------------

Return the value corresponding to a character. @param radix - the base you are working in (2-36) @param digit - character '0'..'9' or 'A'..'Z' or 'a'..'z' @return - value in the range of 0 - (radix-1) corresponding to the given digit if the digit is valid in the given radix. Otherwise return -1 int char2int (int radix, char digit);

--------------------------------------------------------------------------------------------------

Calculate both the quotient and remainder of a division and return the values via pointers. You may use the C operators for division ( / ) and modulus ( % ). However, you might want to write a loop to perform repeated subtraction to help you understand how you would implement this in LC3 assembly language (which has no operators for division or modulus). @param numerator - non-negative value for numerator @param divisor - a positive value to divide by @param quotient - a pointer to the location in which to store the quotient @param remainder - a pointer to the location in which to store the remainder

void divRem (int numerator, int divisor, int* quotient, int* remainder);

----------------------------------------------------------------------------------

/** Convert a string to an integer value. The string comes from the standard * input. You must read one character at a time by using the * built-in getchar() function which simply returns the next * character available in the standard input. The end of the string is signaled * by a newline character ('\ '). You MUST implement this function * recursively. That means you shouldn't have loops. For information on the * algorithm, see the Example revisited (page 3) on Sanjay's handout titled * "Number Systems" (referenced in the main page). You may assume that the * string is legal in the given radix (letters may be uppercase or lowercase). * You can also assume that the string represents a non-negative number. * Here's an example for how to test this function: *

echo "48A6" | ./testConv a2i 12

* This ensures that the string "48A6" is available for you to read from the * standard input. The first time you call getchar(), it will * return '4'. The second time, it will return '8', and so on. Eventually, it * will return '\ ' (because the echo command automatically * appends a newline character). *

This function should not print anything (you * will lose all points for this function if it does). However, you may print * stuff for your own debugging purposes as long as you remove any printing * statements before you submit.

* @param radix - the base you are working in (2-36) * @param valueOfPrefix - the integer value of the digits processed so far (the * prefix). For example, if the string is "48A6" in base 12 and you're about to * read 'A', the prefix is "48" and the integer value of this prefix is 56. * When your function is called by the main program (the testConv.c file), * this parameter is 0. * @return the number represented by the string */ int ascii2int (int radix, int valueOfPrefix);

/** Print a number in the specified radix. Use the C call putchar() * to print a single character obtained using int2char(). Do not * use any other output functions. This function MUST be implemented * recursively. Your program can not use arrays or strings in C, even if you * know about them. For information on the algorithm, see Section 3 (page * 5) on Sanjay's handout titled "Number Systems" (referenced in the main * page). See also Section A2 in the "Number Conversion" notes by Fritz * (referenced in the main page). *

This function should only print the digits * of the number. If it prints extra stuff, you will lose all points for this * function. Do not print a newline character at the end. You may print stuff * for your own debugging purposes as long as you remove any extra printing * statements before you submit.

*

Also, make sure this function does not * print leading zeroes (if it does, you will lose all points for this * function).

* @param radix - the base you are working in (2-36) * @param value - the number to convert (it will be non-negative) */ void int2ascii (int radix, int value);

--------------------------------------------------------------------

char int2char (int radix, int value) {

//Check for valid radix and value.

if (value >= 0 && value <= radix - 1){

if (value <= 9){

char c = value + 48;

return c;

}

else{

char c = 'A';

c = c + (value - 10);

return c;

}

}

return '?';

}

/** @todo implement in numconv.c based on documentation contained

* in numconv.h.

*/

int char2int (int radix, char digit) {

return -1;

}

/** @todo implement in numconv.c based on documentation contained

* in numconv.h.

*/

void divRem (int numerator, int divisor, int* quotient, int* remainder) {

}

/** @todo implement in numconv.c based on documentation contained

* in numconv.h.

*/

int ascii2int (int radix, int valueOfPrefix) {

return -1;

}

/** @todo implement in numconv.c based on documentation contained

* in numconv.h.

*/

void int2ascii (int radix, int value) {

}

/** @todo implement in numconv.c based on documentation contained

* in numconv.h.

*/

double frac2double (int radix) {

return -1.0;

}

----------------------------------------------------------------------------

#include #include #include #include "numconv.h"

static void usage() { puts("Usage: testConv i2c radix number"); puts(" testConv c2i radix digit"); puts(" testConv dnr numerator divisor"); puts(" testConv a2i radix (the string is entered through the standard input)"); puts(" testConv i2a radix number"); puts(" testConv f2d radix (the string is entered through the standard input)"); puts("The radix and number parameters are always base 10 numbers."); exit(1); }

/** Entry point of the program * @param argc count of arguments, will always be at least 1 * @param argv array of parameters to program argv[0] is the name of * the program, so additional parameters will begin at index 1. * @return 0 the Linux convention for success. */ int main (int argc, char* argv[]) { if (argc < 3) usage(); char* op = argv[1]; int radix = atoi(argv[2]);

if (strcmp(op, "c2i") == 0) { if (argc != 4) usage(); char c = argv[3][0]; printf("char2int(%d, '%c') returns %d", radix, c, char2int(radix, c)); }

else if (strcmp(op, "i2c") == 0) { if (argc != 4) usage(); int n = atoi(argv[3]); printf("int2char(%d, %d) returns '%c'", radix, n, int2char(radix, n)); }

else if (strcmp(op, "dnr") == 0) { if (argc != 4) usage(); int num = atoi(argv[2]); int denom = atoi(argv[3]); int quotient, remainder;

printf("divRem(%d, %d) ", num, denom); divRem(num, denom, "ient, &remainder); printf("quotient: %d remainder: %d", quotient, remainder); }

else if (strcmp(op, "a2i") == 0) { if (argc != 3) usage(); printf("ascii2int(%d, 0) returns %d", radix, ascii2int(radix, 0)); }

else if (strcmp(op, "i2a") == 0) { if (argc != 4) usage(); int n = atoi(argv[3]); printf("int2ascii(%d, %d) returns \"", radix, n); int2ascii(radix, atoi(argv[3])); printf("\""); }

else if (strcmp(op, "f2d") == 0) { if (argc != 3) usage(); // Read the "0." getchar(); getchar(); printf("frac2double(%d) returns %f", radix, frac2double(radix)); } else usage(); printf(" "); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!