Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Extend the program below, so that myatio() function can convert any base of 2~10. (The library function atoi can only handle decimal literals correctly.) This

Extend the program below, so that myatio() function can convert any base of 2~10. (The library function atoi can only handle decimal literals correctly.) This version of my_atoi takes two arguments: an integer literal and a base. Assume the input string is a valid integer literal of the specified base. For example, if the base is 2, the literal only contains 0 and 1. If the base is 5, the literals contains digit 0~4.

  • Use scanf("%s %d",...) to read in a string followed by an integer of 2~10.

  • The program terminates when use enter quit followed by any integer.

  • Apparently, you should not call atoi() in my_atoi().

  • In my_atoi(), you also should not call other functions declared in , such as

    atol(), atof(), strtol(), strtoul().

  • In my_atoi(), you also should not call library functions declared in , such as sscanf(), fscanf().

#include #include #define SIZE 10

int main() { int a, b; char arr[SIZE]; printf("Enter a word of positive number or 'quit': "); scanf("%s, arr"); while( ) { printf("%s ", arr); a = atoi(arr); printf("atoi: %d (%#o, %#X)\t%d ", a,a,a,a*2,a*a); b = my_atoi(arr); printf("my_atoi: %d (%#o, %#X)\t%d\t%d ", b,b,b,b*2,b*b); } return 0; }

int my_atoi(char c[]) { int solution = 0; base = 0; for(int x = length(c) - 1; x >= 0; x--) { solution += } }

Sample Inputs/Outputs:

red 127 % a.out Enter a word of positive number and base, or 'quit': 37 10

37 my_atoi: 37 (045, 0X25) 74 1369

Enter a word of positive number and base, or 'quit': 37 8

37 my_atoi: 31 (037, 0X1F) 62 961 

Enter a word of positive number and base, or 'quit': 122 4

122 my_atoi: 26 (032, 0X1A) 52 676

Enter a word of positive number and base, or 'quit': 122 5

122 my_atoi: 37 (045, 0X25) 74 1369

Enter a word of positive number and base, or 'quit': 122 7

122 my_atoi: 65 (0101, 0X41) 130 4225

Enter a word of positive number and base, or 'quit': 1101 2

1101 my_atoi: 13 (015, 0XD) 26 169

Enter a word of positive number and base, or 'quit': 1001100 2

1001100 my_atoi: 76 (0114, 0X4C) 152 5776

Enter a word of positive number and base, or 'quit': 345 6

345 my_atoi: 137 (0211, 0X89) 274 18769

Enter a word of positive number and base, or 'quit': 345 8

345 my_atoi: 229 (0345, 0XE5) 458 52441

Enter a word of positive number and base, or 'quit': 3214 5

3214 my_atoi: 434 (0662, 0X1B2) 868 188356

Enter a word of positive number and base, or 'quit': 11111111 2

11111111 my_atoi: 255 (0377, 0XFF) 510 65025

Enter a word of positive number and base, or 'quit': quit 4

red 128 %

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions