Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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
 

ulbador

macrumors 68000
Feb 11, 2010
1,554
0
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
 

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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!
 

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
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.
 

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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!!;)
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
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.

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
 

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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.

Here's the 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).
 

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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

Looks like a great website, thanks!! I'm going to do some of the tutorials.
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
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.

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
 

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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).

B


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!!
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
I wish I had a teacher, because they speak and tell you stuff when your learning, that helps a lot!
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
 

naples98

macrumors member
Sep 9, 2008
95
3
Houston
The second set the - sign applies to the entire fraction, so neg divided by neg is pos.
Thanks

-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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.