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
  • Write a program that performs the following tasks: Display a friendly greeting to the user Prompt...

    Write a program that performs the following tasks: Display a friendly greeting to the user Prompt the user for the value to convert (a String) Accept that String Prompt the user for the value of the initial base (an integer) Accept that integer Prompt the user for the desired base of the output (an integer) Accept that integer If the String is not a legal expression of a number in the initial base, display an error message and exit the...

  • Comp Sci JAVA Log program using controls and loops 1. (1 point) Asks the user to...

    Comp Sci JAVA Log program using controls and loops 1. (1 point) Asks the user to enter an integer base b > 1. (1 point) If the user enter anything less than or equal to 1, you should quit the program. 2. (1 point) Asks the user to enter a positive integer x greater than 0. (2 points) If the user enter anything less than or equal to 0, you should keep asking the user to re-enter a new number...

  • 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...

  • Java Code please read comments and add the code to required lines....LINE 1 to LINE 5...

    Java Code please read comments and add the code to required lines....LINE 1 to LINE 5 ********************************************************************** * Program Summary: This program demonstrates these basic Java concepts: * - Creating an array based on user input * - Accessing and displaying elements of the array * * The program should declare an array to hold 10 integers. * The program should then ask the user for an integer. * The program should populate the array by assigning the user-input integer...

  • 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)        {...

  • I need help getting my program to reject a negative input from the user. Here is...

    I need help getting my program to reject a negative input from the user. Here is what I have: #short description of what the program will do print("Hello, this program will ask you to enter a non-negative decimal integer. The program will then show the binary equivalent of the integer you choose.") #ask the user to enter the non-negative decimal integer number= int(input("Enter a non-negative decimal integer: ")) while number< 0: print("Number Cannot Be Negative!! Try again...") #create a variable...

  • 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...

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