Question

please add comments and make the progrom work with negative and simple as much as possible...

please add comments and make the progrom work with negative and simple as much as possible

Write a C program utilizing printf and scanf (do not use cout or cin) that does the following:

0. Using the division algorithm introduced in class to convert between base 10 and any other number base:

1. Prompts the user to enter an unsigned integer in base 10 (decimal) from the keyboard.

2. Prompts the user a new base ( greater than or equal to 2 and less than or equal to 36** ) to convert the base 10 unsigned integer into (do not allow values less than 2 or greater than 36, if the user enters a value out of this range, prompt them to re-enter a base).

3. Constructs a string of digits that represents the user entered base 10 integer in the user entered new base.

4. Output the constructed string (do this as a char array of 32 digits instead of the C++ string class [makes possible converting to Assembly Language much easier]).

** for bases greater than 10, you will want to follow the notation that hexadecimal does (10=A, 11=B, 12=C, 13=D, 14=E, 15=F ) meaning that a base 36 number system, the number 35 would be represented with Z.

also your program can properly handle negative values for the number you are converting)

Here are some test cases for your program:

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

Enter the base 10 number you would like to convert: 12345
Enter the base you would like to convert 12345 to: 16
12345 base 10 is 3039 base 16
----------------------------------------------------------------------------------------

Enter the base 10 number you would like to convert: 123456
Enter the base you would like to convert 123456 to: 19
123456 base 10 is HIID base 19
---------------------------------------------------------------------------------------

Enter the base 10 number you would like to convert: 123456789
Enter the base you would like to convert 123456789 to: 32
123456789 base 10 is 3LNJ8L base 32
------------------Negative Number Example -------------------

Enter the base 10 number you would like to convert: -1234567
Enter the base you would like to convert -1234567 to: 9
-1234567 base 10 is 88888888888888888888888886607438 ba
0 0
Add a comment Improve this question Transcribed image text
Answer #1

from the given data

# include < stdio.h >

# include < string.h>

void base 10 ti any base(
int num, int base, char * converted _str)

{

int index = 0;

int length =0;

int rem = 0;

char result (32) = {0};

// loop till num is greater than 0

while (num > 0)

{

rem = ( num % base);

// calculate remainder using modulus operator

num = num/ base

// divide the number by base

if (base >= 16 && rem > 9)

// if base is >= 16 and remainder > 9

result (index ++) = rem + `A` -10;

// add ascii value of A and subtract 10 to offset 0-9

else

result (index ++) = rem + `0`;

// remainder <= 9, just add ascii value `0`

}

// ned to reverse the result to get correct converted string

// loop till index > 0, index is length of the result string

while (index > 0)

{

// put the character starting from index to 0 in converted _str from 0

// increment length and decrement index

converted_str(length++) = result (-- index);

}

}

int main()

{

int num = 0;

int base = 0;

char converted _str (32) =(0);

// prompt for base 10 number

printf(" \n Enter the base 10 number you would like to convert;");

scanf("%d", & num);

// loop till valid base not entered

while (1)

{

// prompt for base value

printf(" Enter the base you would like to convert %d to : ", num);

scanf("%d", & base);

if (!(base > = 2 && base <= 36))

{

// continue if invalid base entered

printf(" invalid base value. please enter the base >= 2 and <= 36);

continue;

}

else

{

break;

}

}

return0;

}

Add a comment
Know the answer?
Add Answer to:
please add comments and make the progrom work with negative and simple as much as possible...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement a Java program using simple console input & output and logical control structures such that...

    Implement a Java program using simple console input & output and logical control structures such that the program prompts the user to enter 5 integer scores between 0 to 100 as inputs and does the following tasks For each input score, the program determines whether the corresponding score maps to a pass or a fail. If the input score is greater than or equal to 60, then it should print “Pass”, otherwise, it should print “Fail”. After the 5 integer...

  • For Python: A prime number is defined as an integer greater than 1 that has no...

    For Python: A prime number is defined as an integer greater than 1 that has no positive divisors other than 1 and itself. Write a program that prompts the user for an integer > 1. Validate the value is > 1 (if not, ask for another). Use a loop to determine if the number is prime or not. Issue an appropriate message. [complete this part before proceeding]. Add a loop that continues to ask the user if they would like...

  • Here is the code I made, but the test case is not working, it goes wrong...

    Here is the code I made, but the test case is not working, it goes wrong when the binary string convert to decimal. please help. #include "stdafx.h" #include <iostream> #include <string> #include <math.h> #include <locale> using namespace std; // function for option 1 void decToBin(int number) {        int array[16];        int i = 0;        for (int counter = 0; counter < 16; counter++)        {               array[counter] = 0;        }        while (number > 0)        {...

  • C++ with comments please! // numberverifier.cpp #include <iostream> #include <exception> using std::cout; using std::cin; using std::endl;...

    C++ with comments please! // numberverifier.cpp #include <iostream> #include <exception> using std::cout; using std::cin; using std::endl; #include <cmath> #include <cstring> class nonNumber : public exception { public: /* write definition for the constructor */ -- Message is “An invalid input was entered” }; int castInput( char *s ) { char *temp = s; int result = 0, negative = 1; // check for minus sign if ( temp[ 0 ] == '-' ) negative = -1; for ( int i...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • (Python) Please do NOT solve it by using special functions(like max min, divmod), I have to...

    (Python) Please do NOT solve it by using special functions(like max min, divmod), I have to ask the same questions again and again otherwise, thanks for your understanding. 1. Write a program that prompts the user for an integer, calculates the factorial of the input integer and displays the result as a string with a comma at every third position, starting from the right. Assume that user input is valid. Hint: Think about representing a number as a string. Sample...

  • In C please : The tasks in this exam review exercise are structured in levels. You...

    In C please : The tasks in this exam review exercise are structured in levels. You can see the point distribution for the levels at the end. It is strongly recommended that you ensure you have passed all test cases of lower levels before moving up to a higher level. For example, ensure you have completed all Level 0 and 1 tasks before even looking at Level 2 Tasks. MESSAGE ENCODER LEVEL 0: (test cases - 50 points) Start by...

  • You work at a bank and needs to automate the bills that are given to a...

    You work at a bank and needs to automate the bills that are given to a customer when they cash a check. When the check is cashed, the system should automatically break down the dollar amount into the smallest number of $20, $10, $5, and $1 bills. Specifications: Write a program that prompts the user to enter a U.S dollar amount and then shows how to pay the customer using the smallest number of bills. After the program runs, prompt...

  • This homework is an approach to generating random numbers. In this technique a seed value is...

    This homework is an approach to generating random numbers. In this technique a seed value is used to construct a new (seemingly random) value in the following manner:  The seed, s, is written down in n-bit binary.  Several bit positions, called taps, are used to generate a feedback bit. Bit n-1 is always one of the taps.  The feedback bit, f is the exclusive-or of the tap bit values  The seed is shifted to the left...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT