Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write in C and use #include and #include Basic version (Graded out of 100) Write a program that loops. At each loop iteration, the

image text in transcribedimage text in transcribedPlease write in C and use #include and #include

Basic version (Graded out of 100) Write a program that loops. At each loop iteration, the program prompts the user to input a value for the base b (b could be any value from 2 to 10), then a string of characters that represent a number in base b. The program then converts the number to a decimal representation, and prints the result. For example, if the user types the string "15", and the base is b = 8, the program will convert it to 13 in decimal and print 13. The max number of characters accepted by the program is MAX_NO_CHARACTERS. The program exits the loop when the user types the value-1 for the base, prints "Exiting ", and terminates. 1. Additional requirements You must read the input number as a string of characters. You are not allowed to read as an integer (by using one of the integer specifiers). However, you are allowed to read the base as an integer. You must use the function convertToDecimal, defined below. Notes . . For your program, use a constant variable for MAX_NO_CHARACTERS and set it to 6. Assume the user does not type more than MAX_NO_CHARACTERS characters. Assume the user does not type a string representing a negative number. Hints: o You will need to use %n in scanf to determine the number of characters typed. o At some point in the program, you may need to loop over the characters typed, and a suggestion is to use the "for" loop. The "for" loop in C is like in C++ Pseudocode for the basic version // main function Define an array of char, called charArray Define an int called numberTyped Prompt the user to input the base b While (base b not equal -1) Prompt the user to input the number Read the number (as a string) into charArray and the number of characters typed into numberTyped, using the appropriate scanf specifiers Convert the content of charArray into the corresponding decimal value Print the result of the conversion Prompt the user to input the base b End while (base b not equal -1) Print "Exiting " 2. convertToDecimal function To convert the array into a decimal value, implement and use this function in your program long int convertToDecimal(char charString[], int numberChars, int base); Arguments: - name of a char array that contains the string representing the number. The first element of the array is the most significant digit of the number. The function should not assume there is a NULL character ('\0') after the last digit - number of characters (digits) of the number. The function should use this argument to determine the end of the string representing the number, instead of assuming there is a NULL character at the end the base. Return: the number in decimal, as a long int. Assume the chars in the array are valid and therefore there is no need for input validation. . If you don't implement exactly the above arguments and return type, you will get a compilation error on zylabs. // Definition of function convert ToDecimal Set sum to zero Set powerofBase to 1 Loop to read the characters in the array one by one, starting from the last digit Convert the character to digitVal, its numerical value. For example, the numerical value of 'l' is 1 Set sum = sum + digitval * powerOfBase Set powerOfBase = powerOfBase * base End loop Return sum Hints: To convert a character to its corresponding numerical value, you can do the following (assuming the character represents a valid digit between 0 and 9): Get the ASCII code of the character. To get the ASCII code, type cast the character into an int. Get the ASCII code of the character 'O' To get the ASCII code, type cast 'O' into an int. Subtract to get numerical value = ASCII code of the character - ASCII code of 'o' 1 1

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Concepts Of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

4th Edition

0619064625, 978-0619064624

More Books

Students also viewed these Databases questions

Question

Write an elaborate note on marketing environment.

Answered: 1 week ago