Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 5-2 7410:39 29112929 @ 100% + SECTION 5.3 POINTERS AND ARRAYS 97 #include int getch(void); void ungetch(int); 1. getint: get next integer from input

Exercise 5-2 image text in transcribed

image text in transcribed
7410:39 29112929 @ 100% + SECTION 5.3 POINTERS AND ARRAYS 97 #include int getch(void); void ungetch(int); 1. getint: get next integer from input into .pn / int getint(int .pn) int c, sign; while (isspacec - getch())) 1+ skip white space +/ if (lisdigit(c) && c I= EOF && C. && != '') ungetch(c): / it's not a number / return 0; } sign = (c ='')? -1 : 1; 1f (c == ' II c == '-') c = getch(); for (.pn .0; isdigit(c); c= getch()) pn - 10 .pn . (c-'0'); *pn sign: if (c != BOP) ungetch(c); return c: Throughout getint, *pn is used as an ordinary int variable. We have also used getch and ungetch (described in Section 4.3) so the one extra character that must be read can be pushed back onto the input. Exercise 5-1. As written, getint treats a + or - not followed by a digit as a valid representation of zero. Fix it to push such a character back on the input. Exercise 5-2. Write getfloat, the floating-point analog of getint. What type does getfloat return as its function value? O 288 5.3 Pointers and Arrays In C, there is a strong relationship between pointers and arrays, strong enough that pointers and arrays should be discussed simultaneously. Any opera- tion that can be achieved by array subscripting can also be done with pointers. The pointer version will in general be faster but, at least to the uninitiated, EF 12:31 212 @ 100% + : 5.2 Pointers and Function Arguments Since C passes arguments to functions by value, there is no direct way for the called function to alter a variable in the calling function. For instance, a sorting routine might exchange two out-of-order elements with a function called swap. It is not enough to write swapla, b): where the swap function is defined as void aplint *, int y) WRONG int temp: temp - X: X-Y: y temp: > Because of call by value, swap can't affect the arguments a and b in the rou tine that called it. The function above only swaps copies of a and b. The way to obtain the desired effect is for the calling program to pass pointers to the values to be changed: sapka, kb); Since the operator & produces the address of a variable, &a is a pointer to a in swap itself, the parameters are declared to be pointers, and the operands are accessed indirectly through them. 96 POINTERS AND ARRAYS CHAPTER 3 yold swaplint px, intoy) / Interchange px and opy / int tempi 100 28 tesp px: px - pyt .py - tempi 1 Pictorially: in caller: a

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

Fundamentals Of Database Systems

Authors: Sham Navathe,Ramez Elmasri

5th Edition

B01FGJTE0Q, 978-0805317558

More Books

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago