Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 16, 2017, 09:22:43 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Operations on Fractions (2)  (Read 1667 times)
daviddelmonte
Full Member
***
Posts: 123



WWW Email
« on: November 14, 2009, 11:10:10 AM »

I keep looking at the code to perform arithmetic on Fractions.

I understand (mostly) the capability to use numerator and denominator as instance variables to store both fractions.

However, couldn't I make integers for each numerator and each denominator? It seems to me that they would be easier to manage. What is wrong with that approach?

David
« Last Edit: November 14, 2009, 11:44:44 AM by daviddelmonte » Logged
toby
Full Member
***
Posts: 104


« Reply #1 on: November 14, 2009, 11:54:01 AM »

Sorry I don't follow, what exactly do you mean? Something like instead of having a fractions class, just have several integers in main that held a numerator and denominator for a couple of fractions?
Logged
daviddelmonte
Full Member
***
Posts: 123



WWW Email
« Reply #2 on: November 14, 2009, 11:56:52 AM »

Exactly..
Logged
toby
Full Member
***
Posts: 104


« Reply #3 on: November 14, 2009, 12:24:28 PM »

Off the top of my head two main reasons why you would structure your code this way is organisation and scalability. In terms of organisation you are grouping related values and the code that manipulates those values together, this keeps things quite clean and means that code is organised in a way that it can be reused in many programs. There are probably allot more valid points in organisation than I'm making but I'm pretty tired. So in terms of scalability, you can make however many int's you want in your main, which is pretty coolhowever how many fractions you can handle at the same time depend on how many int's you have created in main. If you make a fraction class (like you have) then you create as many instances as your program requires while its running and so as your user gives the program more fractions to compute you can create more instances to store them in, you can't really do that with int's. You could achieve this effect by using something like malloc (a c function) to allocate memory for an int, and store the memory address in a pointer, and then create a an array of pointers but arrays unless you are using objects are limited in size so you would really end up with a linked list, so you would have a structure containing int's and references all of which were dynamically put together and maintained by your program. This is a serious amount of work that is very similar to many of the things that actually happen when you create a new instance of an object and store it in a NSMutableArray, and its a serious amount more work to get the other flexibility offered by creating a class.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.