# Help with a question...

Discussion in 'Mac Programming' started by cybrscot, Jan 6, 2011.

1. ### cybrscot macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
#1
I'm doing the exercises in my book, but I'm not sure how to answer this question. C programming

If i and j are positive integers does (-i) /j always have the same value as............ -(i /j )? Why?

I don't quite understand this, if we put a pos integer in the first set, there is
a - in front of it (in parentheses) so that makes the numerator negative, the dem is pos, so the value is negative.

The second set the - sign applies to the entire fraction, so neg divided by neg is pos.

But I'm not sure if this is right in programming? Because the question says that i and j are positive integers, so how can we put a - in front?

Thanks

Joined:
Feb 11, 2010
#2
This is basic math:

4/2 = 2

(-4)/2 = -2

-(4/2) = -2 ... which is the same as:

-1 * (4/2) = -2

-1 * (-4/2) = 2 .... The negatives effectively cancel one another out

3. ### cybrscot thread starter macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
#3
Yeah I know it's basic math, but that's why I asked if it's diff in programming, my book has a * next to the question, which means it's a tricky question. Programming is new to me, and this is a diff. world. Thanks for helping!

4. ### chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
Sailing beyond the sunset
#4
This is the book by K. N. King, right? What chapter? What page?

Have you visited the website associated with the book?

5. ### cybrscot thread starter macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
#5
Yeah, right book, didn't know there was a website for the book, it doesn't mention in anywhere in the beginning pages. Chapter 4 p60 Exercises Question *2.

Page 44 at the bottom says "starred * exercises are tricky" The obvious answer is usually not the correct one. So this question seemed too obvious and he gave it a *. So that's why I wasn't certain it was the same answer as basic math.

6. ### chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
Sailing beyond the sunset
#6
Website was easy to find. I googled: king c programming
Result:
http://knking.com/books/c2/index.html

Chapter 4 is Expressions.
http://knking.com/books/c2/content.html#ch4

So that means you've already know about types. It also means you should know about limits in the ranges of different types. If not, then you should review that material and take careful notice of any asymmetries in the positive and negative limits, then consider the consequences at the extremes. At least that's my guess about what the question is asking, especially if the author thinks it's tricky.

Incidentally, a lot of bugs occur "at the extremes", so thinking about what happens in those conditions (also called "edge cases") is an important way to improve quality.

7. ### cybrscot thread starter macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
#7
Thanks for the help, I'll check out the websites. I don't understand what you were saying at all, sorry! (types, extremes, limits in the ranges of different types???) I have no idea what all that means. The chapter covered arithmetic operators, (which this question is not), covered the % rem, covered precedence and associativity, and a lot of my brain power went into understanding ++1, and 1++, and --1, and 1-- (increment and decrement)

But hey! Thanks, I'll keep digging!!

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#8
Here's a quick data type reference courtesy of Google: http://www.exforsys.com/tutorials/c-language/c-programming-language-data-types.html

Each data type (e.g. char) has its own range of values that can be stored in it.

chown33 is saying that funny things may happen when the values are right at the extremes of those ranges.

(e.g. char can only hold -128 to +127 does that change the basic math).

B

9. ### cybrscot thread starter macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
#9
Ulbador, you were not quite right. But thanks for trying to help anyway.

I just found the answer to my question on the website for the book. But I don't know the difference between C89 and C99. But that's no biggie. But I would like to know which one I'm learning? Is it 89 or 99? It doesn't specify on the book, only says C programming, a modern approach.

I get where below it can be -1, because 9 divided by 7 as integers will be 1 for the program. I can also see where a 2 "could" come from if we were talking about 9 % 7, but we're not. So how did they get the two? What implementation are they referring to?

Question again: If i and j are positive integers, does (-i) / j always have the same value as -(i / j) ? Justify your answer.

Not in C89. Suppose that i is 9 and j is 7. The value of (-i)/j could be either 1 or 2, depending on the implementation. On the other hand, the value of -(i/j) is always 1, regardless of the implementation. In C99, on the other hand, the value of (-i)/j must be equal to the value of -(i/j).

10. ### cybrscot thread starter macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
#10
Looks like a great website, thanks!! I'm going to do some of the tutorials.

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#11
It's a matter of rounding. -1.28 is between -1 and -2, some implementations will always round down to the next integer, which would take you -2.
(Not up to -1).

EDIT: I had to double check, but gcc's implementation of C99 features is still incomplete, so assume you are still using C89. http://gcc.gnu.org/gcc-4.2/c99status.html gives you the status of individual features.

B

12. ### cybrscot thread starter macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
#12

Oh, oh, oh. I see. Wow, there is sooo much to learn! Holy Cow!! I wish I had a teacher, because they speak and tell you stuff when your learning, that helps a lot! I'm just trying to figure it out from the book!

Thanks so much for taking the time. I hope one day I am the master, I'd love to help others!!

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#13
Nice thing is, here, you have a bunch of teachers.

I'm sure you can find some kind of iTunes U/Podcast/Webcast on C that could be good to follow along with.

B

14. ### naples98 macrumors member

Joined:
Sep 9, 2008
Location:
Houston
#14
-2/-2 != -(2/2)

Next, while it is important to understand how different implementations do things, I think this is a poor question focusing on the minutiae of two C standards and not a good teaching example for someone new to the language. However, at least you aware of things like this now.