# Use C and please show the output and complete the format of the programs are given,thank you.

Stuck on a homework question? Our verified tutors can answer all questions, from basic math to advanced rocket science!

there are 2 parts ,don’t not forget complete the format of the programs are given,

Part1(ConvertProb) format of code:

/*

Number converter

Convert between integer, binary, octal and hexadecimal

This program should accept numeric values in hexadecimal,

decimal, octal and binary formats as:

Hex 0x0 to 0xFFFFFFFF

Dec 0 to 4294967295

Oct o0 to o37777777777

Bin b0 to b11111111111111111111111111111111

After a value is input the code in main will interpret the

data types above an process the conversion to an unsigned

int. The unsigned int will be used to convert the input to

strings containing hexadecimal, octal and binary strings.

*/

#include <stdio.h>

#include <string.h>

//int input_to_decimal(char *input);

unsigned int bin_to_uint(char *input);

unsigned int oct_to_uint(char *input);

unsigned int hex_to_uint(char *input);

unsigned int dec_to_uint(char *input);

void uint_to_hex(unsigned int n, char *output);

void uint_to_oct(unsigned int n, char *output);

void uint_to_bin(unsigned int n, char *output);

int main(){

char input;

unsigned int n = 0;

char output;

// Write code here to test your functions

// Uncomment code below when done

/* printf(“Enter a binary, octal, decimal or hexadecimal numbern”);

printf(“convert > “);

gets(input);

// Detect input data type

if(input == ‘0’ && input == ‘x’){

n = hex_to_uint(input);

}

// Decimal

else if(input >= ‘0’ && input <= ‘9’){

n = dec_to_uint(input);

}

// Octal

else if(input == ‘o’){

n = oct_to_uint(input);

}

// Binary

else if(input == ‘b’){

n = bin_to_uint(input);

}

// Unknown

else{

printf(“ERROR: Unknown data type: %sn”, input);

}

// Print results

printf(“The decimal value of %s is %un”, input, n);

uint_to_hex(n, output);

printf(“The hexadecimal value of %s is %sn”, input, output);

uint_to_oct(n, output);

printf(“The octal value of %s is %sn”, input, output);

uint_to_bin(n, output);

printf(“The binary value of %s is %sn”, input, output);

*/

return 0;

}

/*

This function converts the value part of the hex

string to an unsigned integer value. The first

two chars are 0x, which tells that the string is

in hex. Start processing the value at index 2 until

the null, calculating the int value as you would on

paper. Try on paper first.

*/

// Convert a hexadecimal char array to uint

unsigned int hex_to_uint(char *input){

// Declare result and set to zero

unsigned int res = 0;

// Declare and set multiplier to 1

// Declare iterator

// Loop through value part of input string

// If between 0 and 9 add 0 to 9 to res with multiplier

// If between A and F add 10 to 15 to res with multiplier

// Error – exit

// Advance multiplier to next position value

return res;

}

/*

Copy hex_to_uint() and modify for decimal input.

*/

// Convert a unsigned integer char array to uint

unsigned int dec_to_uint(char *input){

// Declare result and set to zero

unsigned int res = 0;

return res;

}

/*

Copy dec_to_uint() and modify for octal input.

*/

// Convert a octal char array to uint

unsigned int oct_to_uint(char *input){

// Declare result and set to zero

unsigned int res = 0;

return res;

}

/*

Copy oct_to_uint() and modify for binary input.

*/

// Convert a binary char array to unsigned int

unsigned int bin_to_uint(char *input){

// Declare result and set to zero

unsigned int res = 0;

return res;

}

/*

This function converts from unsigned int to a hex

char array. Try this on paper before coding.

*/

// Convert a unsigned integer char array to hexadecimal

void uint_to_hex(unsigned int n, char *output){

// Declare a uint for remainder

// Declare an int for division

// Declare a char array buffer

// Use a loop to generate a hex string – string will be reverse

// Get last hex char

// Put null at end of buffer

// Copy 0x to output string

// Copy chars from buffer in reverse order to output string

return;

}

/*

Copy uint_to_hex() and modify for octal

*/

// Convert a unsigned integer char array to octal

void uint_to_oct(unsigned int n, char *output){

return;

}

/*

Copy uint_to_oct() and modify for binary

*/

// Convert a unsigned integer char array to binary

void uint_to_bin(unsigned int n, char *output){

return;

}

Part2(SafeCalcProb) format of code:

/*

Safe integer calculator – warns if an overflow or

underflow error occurs.

Negation

Safe unsigned ops

Safe signed ops

Arithmetic without + – * / % ++ or —

Code the functions below and uncomment the code in main()

when completed.

Code the functions in order as they appear. Subsequent

functions depend on previous functions. You cannot use

any math operators (+ – * / % ++ or –) except simple

assignment (=). However, you may use relational and

logical operators.

The _add() function should only use bitwise operators. All

other functions can call functions necessary to complete the

required operation. I added some hints above functions.

*/

#include <stdio.h>

// Prototypes

int sub(int a, int b);

int neg(int a);

int mul(int a, int b);

int div(int a, int b);

int mod(int a, int b);

int pow(int a, int b);

int convert(char *input);

// Main

int main(int argc, char *argv[]){

int res = 0; // Cumulative result – running total

int n = 0; // For number conversion from input string

char input; // Input string

input = ‘’; // Put null in operator char so loop works

// Write code here to test your functions

// Uncomment code below when done

// Loop until quit is selected

while(input != ‘q’ && input != ‘Q’){

// Print prompt with running total

printf(“nres = %d > “, res);

// Get input string

gets(input);

// Clear screen

system(“cls”);

// Switch on operator char input

switch (input){

case ‘+’:

break;

case ‘-‘:

res = sub(res, convert(input));

break;

case ‘*’:

res = mul(res, convert(input));

break;

case ‘/’:

res = div(res, convert(input));

break;

case ‘%’:

res = mod(res, convert(input));

break;

case ‘~’:

res = neg(res);

break;

case ‘^’:

res = pow(res, convert(input));

break;

case ‘c’:

case ‘C’:

res = 0;

break;

case ‘q’:

case ‘Q’:

printf(“Good-bye!n”);

break;

default:

printf(“Enter a valid operator and operandn”);

}

}

*/

return 0;

}

printf(“nSafe Integer Calculatorn”);

printf(“- x to subtractn”);

printf(“* x to multiplyn”);

printf(“/ x to dividen”);

printf(“%% x to modulusn”);

printf(“~ x to negaten”);

printf(“^ x to raise by power xn”);

printf(“c x to clear resultn”);

printf(“q x to quitn”);

return;

}

/*

This function should only use bitwise operators and

relational operators

*/

// Add operation using only bitwise operators

// Loop until b is zero

// Find carry 1 bits – a AND b assign to carry

// Find non carry 1 bits – a XOR b assign to a

// Multiply carry by 2 by shift and assign to b

return a;

}

/*

overflow and underflow errors.

*/

// Declare int for result

int res = 0;

// Call to _add() a and b and assign to result

// Check for overflow – look at page 90 in book

// Check for underflow – look at page 90 in book

return res;

}

/*

Negate a by using a bitwise operator and safe add().

Look on page 95 in book.

Replace the zero with an expression that solves this.

*/

// Define negation with ~ and safe add

int neg(int a){

// Return negation of a and add 1

return 0; // Replace 0 with code

}

/*

Remember that subtraction is the same as addition

if you negate one of the operands.

Replace the zero with an expression that solves this.

*/

// Define safe subtract by safe add – negate b

int sub(int a, int b){

return 0; // Replace 0 with code

}

/*

Safe mul() uses an iterative call to safe add()

to calculate a product. Remember that

5 x 4 = 5 + 5 + 5 + 5 = 20

*/

// Define safe multiply by calling safe add b times

int mul(int a, int b){

// Declare and initialize cumulative result

int res = 0;

// Declare sign of product – initially assume positive

// For efficiency – smaller number should be multiplier

// Absolute value of a and flip sign

// Absolute value of b and flip sign

// Accumulate result

// Set sign to output

return res;

}

/*

Safe div() repeatedly subtracts b from a, counting the

number of subtractions until a < b, which it returns.

*/

// Define safe divide by calling safe subtract b times

int div(int a, int b){

// Declare int to count how many times can b be subtracted from a

int cnt = 0;

// Declare sign

// Absolute value of a and flip sign

// Absolute value of b and flip sign

// loop to calculate how many times can b be subtracted from a

// Set sign to output

return cnt;

}

/*

Safe mod() repeatedly subtracts b from a until a < b, returning a.

*/

// Define safe modulus by calling safe subtract

int mod(int a, int b){

// Absolute value of a

// Absolute value of b

// Find remainder by repeated subtraction a – b

return a;

}

/*

Safe pow() calculates as the math pow function but

only uses the safe operations.

res = n^exp

Loop until exp is zero

res = res * n

exp = exp – 1

Remember the special case for n^0

*/

// Define safe pow by calling safe multiply exp times

int pow(int n, int exp){

// Declare int for result of n^exp

int res = 0;

// Loop and multiply to calculate n^exp

return res;

}

/*

This function extracts the integer value from the input string.

If input = “+ -123”, res = -123.

If input = “* 987654”, res = 987654.

The best way to solve complicated problems is to work them out

on paper first.

*/

// Extract the integer from the input string and convert to int

int convert(char *input){

// Declare int for result extracted from input

int res = 0;

// Declare int for sign of result

// Declare two iterators

// Declare a buffer for numeric chars

// Set error to zero – no error found yet

// Check for space in element 1

// Check for negative integer at element 2

// Loop to copy all numeric chars to buffer

// i is iterator for input string and should start at first numeric char

// j is iterator for buffer where numeric chars are copied

// This must test for chars between 0 and 9

// i gets position of last numeric char in buffer

// j is now used for pow function – start at zero

// Construct integer from buffer using pow j increases and i decreases

// Set sign for output

return res;

}