Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PROGRAMMING LANGUAGE-C When you use “scanf(\"%d\", &n)” to read an integer, it r

ID: 3765329 • Letter: P

Question

PROGRAMMING LANGUAGE-C When you use “scanf("%d", &n)” to read an integer, it reads digits from the standard input up to the first non-digit. So, for example, the input “234” will cause n to be set to 234, and the input “56xy7” will cause n to be set to 56, with the next character to be read being ‘x”. This sometimes causes problems, because “56xy7” is not a number. Your problem is to write a function to fix this. We define an integer as follows: 0 or more leading white spaces, followed by … an optional ‘+’ or ‘-’, followed by … 1 or more digits, followed by a non-alphanumeric, but not a ‘.’ followed by 1 or more digits. So for example, “1234”, “ 1234 ”, “1234.”, “ +1234 ”, and “ -1234 ” are all integers, and none of “1234e5”, “e1234”, “1234.56”, and “1234abc” are. For the purposes of this function, assume that the next line of input was read into an array, for example by fgets, before your function is called. Your function is to have the interface int getint(char *inputline, int *value, char **nextchar) { … } The character pointer inputline points to the input line. Generally, your function is to return the number read in the location that value, and the address of the first character after the last digit of the integer is stored in the location pointed to by nextchar. More specifically: If any of the arguments to getint are invalid (see the first hint), the function returns 1. If getint finds an integer is the first thing in the inputline, it stores the integer in the location pointed to by valueand the location of the character immediately after the last digit at the location pointed to by nextchar, and returns 1. However: If the integer is too large (positive — see the second hint), it reads all the digits, and returns 2; the value stored in the location pointed to by value need not be correct. If the integer is too small (negative — see the second hint), it reads all the digits, and returns 3; the value stored in the location pointed to by value need not be correct. Otherwise, getint does not change the values stored at the location pointed to by value, but it stores the value of inputline in the location that nextchar points to, and returns 0. Your function is to be in a file called “getint.c”. Hint: Think back to the “if argument n is invalid” comment in assignment 2 problem 6. In a similar way, the arguments passed into this function could be invalid. Use what you have learned in lectures 9–14 about pointers, arrays, strings, and buffers, to think about how these arguments could be given invalid values, and how to detect those invalid values. Hint: The maximum positive integer is represented by the macro INT_MAX, and the minimum negative integer is represented by the macro INT_MIN, both of which are defined in the include file “limits.h”.

Explanation / Answer

PROGRAMMING LANGUAGE-C When you use “scanf("%d", &n)” to read an integer, it r

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote