324_Book

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ce on Signed vs. Unsigned As we have seen, the implicit casting of signed to unsigned leads to some nonintuitive behavior. Nonintuitive features often lead to program bugs, and ones involving the nuances of implicit casting can be especially difficult to see. Since the casting is invisible, we can often overlook its effects. Practice Problem 2.16: Consider the following code that attempts to sum the elements of an array a, where the number of elements is given by parameter length: 1 2 3 4 5 6 7 8 9 10 /* WARNING: This is buggy code */ float sum_elements(float a, unsigned length) { int i; float result = 0; for (i = 0; i <= length-1; i++) result += a[i]; return result; } 2.3. INTEGER ARITHMETIC When run with argument length equal to 0, this code should return ¼ ¼. Instead it encounters a memory error. Explain why this happens. Show how this code can be corrected. 53 One way to avoid such bugs is to never use unsigned numbers. In fact, few languages other than C support unsigned integers. Apparently these other language de...
View Full Document

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online