|
Hey aren't you the keyboard goop guy?
|
# ? Aug 27, 2011 01:44 |
|
|
# ? Jun 10, 2024 01:34 |
|
I have a practice question for a final exam tomorrow that I'm a little confused about. We're asked to write a class that does all the major operations on large integers. I'm not sure how to store said large integers. All the help on Google says that I should use string objects, but the professor makes it seem like he doesn't want us to use a string (or a vector). Should I store the integers in a regular array? This seems kinda like a pain in the rear end to manipulate.
|
# ? Aug 27, 2011 02:08 |
|
Dicky B posted:Hey aren't you the keyboard goop guy? what's this story
|
# ? Aug 27, 2011 02:17 |
|
Good Will Punting posted:I have a practice question for a final exam tomorrow that I'm a little confused about. We're asked to write a class that does all the major operations on large integers. I'm not sure how to store said large integers. All the help on Google says that I should use string objects, but the professor makes it seem like he doesn't want us to use a string (or a vector). Should I store the integers in a regular array? This seems kinda like a pain in the rear end to manipulate. What does the professor say that makes it seem that way?
|
# ? Aug 27, 2011 02:18 |
|
TasteMyHouse posted:What does the professor say that makes it seem that way? The assignment was posted two weeks before the lesson on the string class & vectors. Echoing the previous "Nobody can teach C++". He gives us notes/really easy examples then hand us problems like this with no gap between them and no guidance whatsoever. Not to say this is hard, but there are things left to figure out that throw me off. It would be fine if it was an actual project, but he pulls this on the exams too and expects us to write like 6 mini projects perfectly without any testing in 2.5 hours. It's loving infuriating, sorry for ranting. Good Will Punting fucked around with this message at 02:32 on Aug 27, 2011 |
# ? Aug 27, 2011 02:20 |
|
TasteMyHouse posted:what's this story
|
# ? Aug 27, 2011 02:22 |
|
Good Will Punting posted:I have a practice question for a final exam tomorrow that I'm a little confused about. We're asked to write a class that does all the major operations on large integers. I'm not sure how to store said large integers. All the help on Google says that I should use string objects, but the professor makes it seem like he doesn't want us to use a string (or a vector). Should I store the integers in a regular array? This seems kinda like a pain in the rear end to manipulate. Are you talking Big Numbers such as 200 digits of pi or just large integers that would fit in an int or int64? One is trivial the other is not. If its big numbers then strings would probably be the fastest(coding wise) way to do it. I'm not sure what the GMP does for its functions.
|
# ? Aug 27, 2011 02:31 |
|
The example he gave is an integer of 25 digits.
|
# ? Aug 27, 2011 02:35 |
|
Good Will Punting posted:The example he gave is an integer of 25 digits. Just use strings
|
# ? Aug 27, 2011 02:54 |
|
Dicky B posted:http://forums.somethingawful.com/showthread.php?threadid=3221068 well this explains what he IMed me
|
# ? Aug 27, 2011 02:56 |
|
TasteMyHouse posted:Just use strings Comedy option: Program it on a vax emulator with 128 bit INTs.
|
# ? Aug 27, 2011 06:59 |
If you want proper big integer support with unlimited size numbers, strings or vectors is the only sensible solution. There are three general ways I can think of how to handle it: 1. Working directly on strings Simply looping over two strings doing arithmetics the way you were taught in grade school, one digit at a time. 2. List of regular integers You can treat each machine integer as a single digit in base 232 and work on those. It's probably way faster than the string approach and possibly even easier to program. Or maybe work with 16 or 8 bit units so you don't have to get into 64 bit integer land. 3. Binary coded decimal One 8 bit byte is treated as two 4 bit digits, where only values 0 to 9 are valid. Arithmetic is done on those. The x86 architecture actually has instructions to deal with BCD but I can't imagine your C++ compiler actually exposes it so it'll probably be really annoying to implement. Note that in option 1, you would be using a string, and in options 2 and 3 you would be using a vector (or other dynamic array structure) of integers or chars. I would suggest doing option 2.
|
|
# ? Aug 27, 2011 09:51 |
|
nielsm posted:
This will definitely be more performant but since it requires thinking in an unfamiliar arithmetical system it might be harder to think through the logic. Option one has the advantage of being dead simple -- just express in code basic elementary school arithmetic. Do 2 if you're looking to stretch your mind, do 1 if you just want to get the job done quickly and easily.
|
# ? Aug 27, 2011 15:55 |
|
Thanks for the help. I'm working through this, deciding to take the "get the job done quickly" approach because if something similar is asked on the exam, that's what I'll need to do. Pretty unfortunate.
|
# ? Aug 27, 2011 18:39 |
|
TasteMyHouse posted:This will definitely be more performant but since it requires thinking in an unfamiliar arithmetical system it might be harder to think through the logic. Option one has the advantage of being dead simple -- just express in code basic elementary school arithmetic. It's not so bad really. It's pretty much exactly the same except that everywhere you write '10' you write '65536' instead.
|
# ? Aug 27, 2011 22:06 |
Qwertycoatl posted:It's not so bad really. It's pretty much exactly the same except that everywhere you write '10' you write '65536' instead. That's exactly what makes it hard. Thinking in different bases than what you were brought up with is a major paradigm shift and some people just can't wire their brains right for it, and writing correct code to solve a problem is drat near impossible if you don't understand the problem fully. ("Paradigm shift". Did I use that word right? I think it's appropriate here...?)
|
|
# ? Aug 27, 2011 22:59 |
|
I understand recursion from math classes, but wow, the way it was explained in my class through Towers of Hanoi was piss poor. I've heard Stack Overflow suggested here, but is there anywhere less, specific I guess?, for me to continue my studies after this class is over? I don't really know where to go from here, I guess. The resources in the OP look a little advanced for where I'm at.
|
# ? Aug 27, 2011 23:47 |
|
Good Will Punting posted:I understand recursion from math classes, but wow, the way it was explained in my class through Towers of Hanoi was piss poor. I've heard Stack Overflow suggested here, but is there anywhere less, specific I guess?, for me to continue my studies after this class is over? I don't really know where to go from here, I guess. The resources in the OP look a little advanced for where I'm at. My best suggestion would be "Don't think you're somehow constrained to C or C++". Learn Scheme or Haskell or F# or something if you really want to force yourself to grok recursion.
|
# ? Aug 27, 2011 23:55 |
|
Good Will Punting posted:Thanks for the help. I'm working through this, deciding to take the "get the job done quickly" approach because if something similar is asked on the exam, that's what I'll need to do. Pretty unfortunate. Given that it's the final, this might not be something to look at immediately, but the Karatsuba algorithm is both a nice approach to computing the multiplication component and relatively easy to understand. There's a good chance you'll see it in a later class as well, so if you're feeling both driven and masochistic... Also a recursive algorithm, as it happens.
|
# ? Aug 28, 2011 00:34 |
|
Good Will Punting posted:I understand recursion from math classes, but wow, the way it was explained in my class through Towers of Hanoi was piss poor. I've heard Stack Overflow suggested here, but is there anywhere less, specific I guess?, for me to continue my studies after this class is over? I don't really know where to go from here, I guess. The resources in the OP look a little advanced for where I'm at. Though to be fair it is tricky to come up with an example of a problem to which recursion is suited that isn't also too complicated to serve as an explanation.
|
# ? Aug 28, 2011 01:40 |
|
roomforthetuna posted:Towers of Hanoi is intrinsically a terrible example for recursion because it's a job that's not actually best-suited to recursion. Not even moderately well suited to recursion What are you talking about, Towers of Hanoi is one of the most well-suited problems in existence when it comes to the applicability of recursive problem solving. If you want to figure out how to move the tower from peg A to peg B, you'll know that at some point you'll need to move the bottom disc from peg A to peg B (and by symmetry there's no sensible route to take that involves going via peg C) which means that you need to move all the other disks from peg A to peg C, first, and then move the bottom disc from A to B, and then, in the interest of completing the problem, move all the other disks from peg B to peg C. And you know that the subproblem can be treated independently because the bottom disc is bigger than the others and since it's not being moved it will always be beneath the other discs. I remember when I was a kid, we'd play computer games on these green-screen computers from a previous epoch and one of them was Towers of Hanoi. Guess what, I had no clue how to solve the puzzle, but obviously one way was to keep track of all possible positions and all possible moves and consider a way to get to the finish, but that's way too complicated, and eventually I realized that you had to move disc #1 the opposite peg you wanted to move disc #2 to, and the same peg you wanted to move disc #3 to, and if you wanted to make your first move the decision would flip back and forth depending on how high the tower was, but the question knowing a solution and knowing exactly why the solution works and the question of how to make an implementation of a solution is easy when the problem is regarded recursively. If 9 out of 10 programmers don't use recursion, it's because 90% of programmers are crap. They don't matter. shrughes fucked around with this message at 02:29 on Aug 28, 2011 |
# ? Aug 28, 2011 02:26 |
|
I certainly didn't mean they wouldn't use recursion because they don't understand it. But yeah, I suppose I'm wrong about it being horrible to solve ToH with recursion if you're solving the whole thing in one pass, which is what you'd be doing in a lesson context now I think about it that way. I was thinking in game terms where you'd want to display the moves between solve-steps, in which context you only want to figure out the next move, not solve the whole thing in one call - using recursion to figure out the next move is a horrific solution. You don't need a lookahead to know what the next move is. (Unlike Chess, where recursion is a pretty good way to figure out a good move.)
|
# ? Aug 28, 2011 02:48 |
|
Final exam was not bad! Recursion didn't end up on it (neither did vectors, sadly) but I still plan on understanding the Towers of Hanoi problem. Conceptually, I get it clear as day. But I've spent quite some time trying to understand what the gently caress is going on in the C++ example we were given, and the way my teacher presents it is confusing as all hell. Maybe it's a poorly designed example, maybe there's something I'm missing, but it's really bugging me and if anyone is interested in explaining it step by step, I'd gladly trade a Plat upgrade for a detailed explanation of what's going on. Closure from this course and onto a more practical way of learning C++, if you will.
|
# ? Aug 28, 2011 03:17 |
|
Good Will Punting posted:Final exam was not bad! If you understand it conceptually but not in C++ code, it could be because it's not that well suited to being translated into procedural code, since you can't actually see the disc relationships and positions. Try looking up a simple mathematical function that uses recursion, like factorial or the Fibonacci sequence.
|
# ? Aug 28, 2011 03:25 |
|
Good Will Punting posted:Final exam was not bad! Can you post the code you're referring to?
|
# ? Aug 28, 2011 05:50 |
|
roomforthetuna posted:When using Visual C++ and its IDE, is there a way to make a resource file properly act dependent on data files that it includes? It doesn't seem to be done automatically as it would be for #includes, and manual dependency setting only seems to be a thing at the project level, not the file level. Maybe there's some way to trick it into thinking I #included the data files? Edit: Aha, I can, brilliant. I stick at the top of the file code:
roomforthetuna fucked around with this message at 00:49 on Aug 30, 2011 |
# ? Aug 30, 2011 00:45 |
|
TasteMyHouse posted:Can you post the code you're referring to? Sorry, got lazy then lost power at some point. code:
|
# ? Aug 30, 2011 02:34 |
|
If all the code samples you get given look like that, perhaps you should inform whoever writes them that it's not 1980 any more and we have screens larger than 80x24 characters, so they're actually allowed to use whitespace and indentation. Here it is formatted a little cleaner, with better comments: code:
|
# ? Aug 30, 2011 02:47 |
|
Another newbie question:code:
I notice that the reference operator is used in the function prototype, so the function will be setup to pass information from getItem to the part instance(variable of InvItem). I'm just confused as to why it's necessary here and I'm having a difficult time wrapping my head around it. Not quite sure how to phrase my question beyond "Why or How does that work?" Any wisdom would be appreciated. Sephiroth_IRA fucked around with this message at 14:21 on Aug 30, 2011 |
# ? Aug 30, 2011 14:18 |
|
Orange_Lazarus posted:Another newbie question: quote:I'm just confused as to why it's necessary here and I'm having a difficult time wrapping my head around it. Not quite sure how to phrase my question beyond "Why or How does that work?" Do you understand the difference between pass-by-reference and pass-by-value? if getItem() didn't pas by reference, the function would not receive the exact original variable that was passed into it -- instead, it would receive a local copy of it. the function would then fill this copy with values it got through cin, then when the function returned this copy would be thrown away like all other local variables. Since the variable is marked as a reference, the function doesn't get a copy -- it gets the very same variable as was declared in the calling function, so when the function returns the variable has all the changes that were made to it inside the function. So, as to HOW this works -- internally this is usually just implemented with pointers by the compiler. as to WHY you'd do that, and not, say, this: code:
All this will change in C++ 11 with rvalue references, though TasteMyHouse fucked around with this message at 14:42 on Aug 30, 2011 |
# ? Aug 30, 2011 14:38 |
|
I never realize how hard it is to explain something until I actually try it.code:
If it wasn't passed by reference, p would be a copy of part, and part itself would remain unchanged. I don't know if you are savvy with a debugger... But if you break before the call to getItem(), and also inside of getItem(), you will see that the address of part is the same as the address of p.
|
# ? Aug 30, 2011 14:38 |
|
Fantastic! Both replies were helpful and thanks for the tip about the case sensitivity of headers as well! Speaking of problems I'd like to know what concepts you guys found most difficult to learn as you progressed. Did you find any supplemental reading helpful?
|
# ? Aug 30, 2011 17:27 |
|
read this whole drat thread Reading through this thread is a bit depressing though. AvengingDentist got me so hyped up for Concepts and I was so disappointed to hear (years after the fact) that they were cancelled. TasteMyHouse fucked around with this message at 17:40 on Aug 30, 2011 |
# ? Aug 30, 2011 17:36 |
|
This question is surely due to fundamentally flawed understanding of the C preprocessor, which I regard in a manner not dissimilar to that which our ancient ancestors regarded lightning. The code is essentially something like: code:
|
# ? Aug 30, 2011 20:12 |
|
It's not. It's just replacing any instance of the string "NUM_PARTY_TYPES" with the string "NUM_PARTIES - 1". That expression is not evaluated until runtime.
|
# ? Aug 30, 2011 20:20 |
|
Dicky B posted:It's not. It's just replacing any instance of the string "NUM_PARTY_TYPES" with the string "NUM_PARTIES - 1". That expression is not evaluated until runtime. Oh crap. Wow, I knew my C was rusty, but, poo poo. To completely forget how defines work? Goddamnit. Thanks.
|
# ? Aug 30, 2011 20:24 |
|
Slanderer posted:Oh crap. Wow, I knew my C was rusty, but, poo poo. To completely forget how defines work? Goddamnit. When you're #defining for constants with operators inside like that you should always enclose the values in braces. #define NUM_PARTY_TYPES (NUM_PARTIES - 1) (Though a const int is generally better than a #define anyway.)
|
# ? Aug 30, 2011 20:33 |
|
roomforthetuna posted:Relatedly, that particular #define is a horrific monster because, eg. (NUM_PARTY_TYPES*2) evaluates to 1 rather than the much-more-likely-intended 4. (Because it preprocesses out to "NUM_PARTIES - 1*2") Also, why is it NUM_PARTIES - 1? That would be 2, when in fact there are 3 party types.
|
# ? Aug 30, 2011 20:35 |
|
well, that was my bad, actually. I left out a line in the enum :code:
|
# ? Aug 30, 2011 20:41 |
|
|
# ? Jun 10, 2024 01:34 |
|
What kinds of things can I expectcode:
|
# ? Aug 30, 2011 21:03 |