Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

lab3myatoi.c contains the following code to beaugmented: #include #define SIZE 10 int main(){ int a,b; char arr [SIZE]; printf(Enter a word of postive number or

imageimage

lab3myatoi.c contains the following code to beaugmented:

#include #define SIZE 10 int main(){  int a,b;  char arr [SIZE];  printf("Enter a word of postive number or 'quit': " );  scanf("%s", arr);  while(   )  {    printf("%s", arr);    a = atoi(arr);    printf("atoi:    %d (%#o, %#X)t%dt%d", a,a,a, a*2, a*a);    b = my_atoi(arr);    printf("my_atoi: %d (%#o, %#X)t%dt%d", b,b,b, b*2, b*b);     }  return 0;}/* convert an array of digit characters into a decimal int *//* textbook did scan from left to right. Here you should scan from right to left. This is a little complicated  but more straightforward (IMHO) */int my_atoi (char c[]){}

Standard library defines a library function atoi. This function converts an array of digit characters, which represents a decimal integer literal, into the corresponding decimal integer. For example, given a char array (string) s of 134 , internally stored as 1 3 4 \0 atoi (s) returns an integer 134. Implement your version of atoi, call it my_atoi, which does exactly the same conversion. Download the partially implemented program lab3myatoi.c. For each input, which is assumed to be a valid integer literal, the program first prints it as a string, and then call atoi and myatoi to convert it, and output its numerical value in decimal, hex and oct, followed by double the value and square of the value. The program keeps on reading from the user until quit is entered. Complete the while loop in main (), and implement function my_atoi. Page 43 of the recommended textbook The C programing language describes an approach to convert a character array into decimal value, this approach traverses the array from left to right. A more intuitive approach, which you should implement here, is to calculate by traversing the array from right to left, following the traditional concept 2 1 3 4 \0 ..... 10 10 10 10 Hint: the loop body you are going to write is different from, and slightly more complicated than that in the recommended textbook, but the logic is clearer (IMHO). For detecting quit, strings cannot be compared directed. You can use the isQuit () function you implemented in lab2 or given in lab3A. c, but you are also encouraged to explore the string library function strcmp (). You can issue man strcmp to view the manual. Note that strcmp() returns 0 (false) if the two argument strings are equal. Extend the program you developed above, so that myatio () function can convert both decimal and hexadecimal characters. (The library function atoi does not handle Oct and Hex literals correctly.) Assume the input string is either a valid decimal literal which begins with 1~9, or a valid hexadecimal literal, which begins with OX or Ox. So 0x12 should be treated as a hexadecimal integer literal whose decimal value is 18. (atoi will return 0). Assume that a hexadecimal literal is always valid. That is, if an input starts with OX or 0x, it contains digits 0-9 and letters A - F and a - f only. For finding the right end of string, you can use your length () function from lab2. You also explore the string library function strlen(). If you need, you can implement a helper function power (int base, int n) to calculate the power. In next class we will learn to use math library functions. Don t use Math library function here. Note: 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 (). Sample Inputs/Outputs: red 127 % a.out. Enter a word of positive number or quit : 9 9 atoi: 9 (011, 0X9) my_atoi: 9 (011, 0X9) 0 (0, 0) my_atoi: 9 (011, 0x9) Enter a word of positive number or quit : 0x9 0X9 atoi: 18 18 Enter a word of positive number or quit : 12 12 atoi: 12 (014, 0XC) 24 my_atoi: 12 (014, 0XC) 24 0 (0, 0) 0 my_atoi: 18 (022, 0X12) 36 0 18 Enter a word of positive number or quit : 0x12 0X12 atoi: 0 (0, 0) 0 my_atoi: 27 (033, 0X1B) 54 0 (0, 0) 0 my_atoi: 47 (057, 0X2F) 94 Enter a word of positive number or quit : 0X1B 0X1B atoi: 0 (0, 0) 0 my_atoi: 47 (057, 0X2F) 94 atoi: 75 (0113, 0X4B) my_atoi: 75 (0113, 0X4B) 81 81 Enter a word of positive number or quit : 0X2F 0X2F atoi: 0 (0, 0) my_atoi: 117 (0165, 0x75) 0 81 100 (0144, 0X64) my_atoi: 100 (0144, 0X64) 144 144 Enter a word of positive number or quit : 0x2f 0x2f atoi: 0 (0, 0) my_atoi: 251 (0373, 0XFB) 0 324 0 Enter a word of positive number or quit : 75 75 0 729 0 0 2209 Enter a word of positive number or quit : 0x75 0X75 atoi: 0 2209 150 150 0 234 Enter a word of positive number or quit : 100 100 atoi: 5625 5625 200 200 13689 Enter a word of positive number or quit : 0xfB OXFB atoi: 0 502 10000 10000 63001

Step by Step Solution

3.45 Rating (148 Votes )

There are 3 Steps involved in it

Step: 1

include include include define SIZE 10 int myatoi char int powerintint int hexconvertchar int isquit... 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

Computer Networks

Authors: Andrew S. Tanenbaum, David J. Wetherall

5th edition

132126958, 978-0132126953

More Books

Students also viewed these Electrical Engineering questions