lab3myatoi.c contains the following code to beaugmented: #include #define SIZE 10 int main(){ int a,b; char arr
Fantastic news! We've Found the answer you've been seeking!
Question:
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[]){}
Transcribed Image Text:
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 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
Expert Answer:
Answer rating: 100% (QA)
include include include define SIZE 10 int myatoi char int powerintint int hexconvertchar int isquit... View the full answer
Related Book For
Posted Date:
Students also viewed these electrical engineering questions
-
Convert the do-while loop in the following code to a while loop: Scanner keyboard = new Scanner(System.in); String input; char sure; do { System.out.print("Are you sure you want to quit? "); input =...
-
1. How do I fix the following code to print the mark per subject with its corresponding symbol and code on separate lines? 2. How do i fix the code to print distinction details per subject? 3. How do...
-
Debug the following code to get it to work correctly. // Displays list of payment options // - credit card, check or cash // Displays fee for using each - 5%, 2% or 0% import javax.swing.*; import...
-
You mentioned that Amazon has sustainability topics within us supply chain. How we deal with the criticism that Amazon itself tribute to overconsumption that's antithetical to sustainability
-
The cost of debt capital is lower after taxes than before taxes. The cost of equity capital is more difficult to estimate using the dividend method or the CAPM model, for example, yet the after-tax...
-
In Section 6.3, we provided an outline of the derivation of the efficiency of slotted ALOHA. In this problem well complete the derivation. a. Recall that when there are N active nodes, the efficiency...
-
Environmental accounting systems have been proposed for sovereign nations as well as for companies. Consider how a macro (national) and a micro (company) view of environmental accounting might differ.
-
How is an organization like an iceberg? Use the iceberg metaphor to describe the field of organizational behavior.
-
1 . A counterflow heat exchanger is used to transfer heat from a hot fluid at 1 2 0 \ deg C to a cold fluid at 4 0 \ deg C . The hot fluid enters the exchanger at a flow rate of 0 . 2 kg / s and...
-
Able and Body are unrelated individuals. In September of the present year, they decide to form I Can Too Corporation. Able contributes a building and land with a basis of $35,000 and and a fair...
-
Question 2 (20 marks) The wavefunction of a particle is given as: p(x) = 1 - exp(_la!). 206 A Calculate the probability that this particle will be found outside the Bohr radius a.
-
In terms of conflict dialogue, describe any ways you think your culture might be misunderstood by other cultures.
-
Mahon Corporation has two production departments, Casting and Customizing. The company uses a job-order costing system and computes a predetermined overhead rate in each production department. The...
-
1.What type of target marketing do you think Minneapolis Sculpture garden is using to attract people to the garden? Differentiated, undifferentiated, or concentrated?
-
The altitude of a triangle is increasing at a rate of 1.5 centimeters/minute while the area of the triangle is increasing at a rate of 1 square centimeters/minute. At what rate is the base of the...
-
What do you think about CEO Jeff Bezos' controversial approach to starting Amazon.com?What was the approach successful?
-
The Lotus Point Condo Project will contain both homes and apartments. The site can accommodate up to 10,000 dwelling units. The project must contain a recreation project: either a swimming-tennis...
-
Write the given system without the use of matrices. D) - ()- d (x sin t + 8 (2+ 1)
-
A modem constellation diagram similar to Fig. 2-23 has data points at the following coordinates: (1, 1), (1, 1), (1, 1), and (1, 1). How many bps can a modem with these parameters achieve at 1200...
-
In Fig. 4-48, the switch in the legacy end domain on the right is a VLAN-aware switch. Would it be possible to use a legacy switch there? If so, how would that work? If not, why not? Figure 4-48...
-
Consider a 50,000-customer video server, where each customer watches three movies per month. Two-thirds of the movies are served at 9 P.M. How many movies does the server have to transmit at once...
-
Give examples of non-current (or fixed) assets. In what circumstances would some of the items you have listed be current assets?
-
What is the difference between a non-current asset and a current asset?
-
You are given the following simplified statement of financial position of the Sandin Castle Company: a Calculate the current ratio. b Calculate the liquidity ratio. c After preparing the statement of...
Study smarter with the SolutionInn App