![]() |
|
#1
|
|||
|
|||
Help With StacksHello, I am creating a program to add and multiply large numbers using stacks. I have been working on the program all day and I believe I am on the verge of completing it, but right now the addition and multiplication functions are not doing their job correctly. I'm not entirely sure why, and I was hoping someone here could assist me in finding out the source of this problem. Thank you for your time and any help is appreciated, no matter how small.
CPP / C++ / C Code:
Last edited by admin : 09-Oct-2005 at 20:03.
Reason: Please insert your C code between [c] & [/c] tags
|
|||
|
#2
|
|||
|
|||
Re: Help With StacksQuote:
My first question is rhetorical: Why would you use stacks to hold digits for multi-digit arithmetic? Here's the way I think you are doing addition: You get digits for one operand (the addend?) and push them onto a stack (most significant digit first). You get digits for the other operand (the augend?) and push them onto another stack (most significant digit first). That's pretty good: the least significant digits are sitting on top of the stack, easy to get to, and that's where the arithmetic will begin. You get the top digit (least significant of the remaining digits) for each number, calculate sum and carry for the decimal addition. So far, so good. But now, you push the sum digit onto the result stack. When you get through, the result has least significant on bottom, most significant on top (assuming the logic and arithmetic are correct). Maybe that's convenient for printing out the value, but in order to use that result as the operand for another addition, it has to be reversed so that the most significant digit is on bottom, right? See my problem? You are representing numbers on a stack, but sometimes they have least significant digits on top and sometimes they have most significant digits on top. I think the data base should match the application, and I can't think of any reason to make the representation context-sensitive (the program ---i.e. the programmer--- has to know whether a given number is ready for operating or ready for printing). Oh, well, without actually looking really, really close at the logic and arithmetic, here are a couple of observations: Line 59 (or thereabouts) CPP / C++ / C Code:
Filename is an array of char, and when you use an array name by itself (no brackets) it is treated as a pointer, so the correct syntax would be CPP / C++ / C Code:
You do this a couple of other places, too (for num1 and num2). Now, there are lots of reasons not to use scanf, and there are also safer ways to use scanf (most experienced programmers recommend fgets() for what you are doing here), but at any rate, whatever you decide to use for input, I really, really, strongly recommend that you put in statements to print out what the user entered at each step. This is also (and in this case, especially) true for getting the input data from the files. If you get the "wrong" output answer, how do you know whether the problem is with input routines or arithmetic routines? line 207 (or thereabouts): CPP / C++ / C Code:
line 333 or thereabouts CPP / C++ / C Code:
Easy to do; I've done it more times than you have seen summers (probably more times than you have seen sunsets). Your function pop() doesn't do anything except decrease the stack by one. If you want to pop a value, maybe it should return the value. (The second argument isn't used in the function, but places where you call the function seem to expect that the top of the stack be returned in the second argument. If you want to do it that way, you can pass a pointer, but why not just have the function return the value?) Maybe you could write a simple main() program that does a few pushes and pops to test the fundamentals before you get into the arithmetic. I think your multiplication() function is supposed to multiply x by y, but the parameter x is not used. You use y but not x Why???? You are using global variables for a lot of your stacks. Why? It's really bad programming practice in the opinion of many of my more knowledegable colleagues. You could at least be consistent. Either make everything global (a bad idea, in my opinion) or use function arguments to pass information around the program. One final comment about style (about comments, actually): I know that everyone says to use lots of comments, but really! compare your code CPP / C++ / C Code:
with this: CPP / C++ / C Code:
Do you really need a comment "pop value from second value stack" for the instruction "pop(y, num2)" (even if your pop() worked)? And look at the statement "product = (num1 * num2) + carry;" That's beautiful! What could be more clear! I really like the use of meaningful variable names. But --- what useful information is added by the comment "multiply the numbers and add the carry to get the product"? Just more clutter, in my opinion. By putting comments every single line (or almost every line) you obscure the really relevant comments and make the program, overall, less pleasant to read (and eyeball fatigue is one main reason people overlook things like "if (carry = 1)"). Regards, Dave |
|
#3
|
|||
|
|||
Re: Help With StacksThanks for your assistance, I tried changing a lot of what you said although the problems don't seem to be clearing up. There's probably some big issue at the root of all this.
Here is the new code. CPP / C++ / C Code:
Last edited by admin : 09-Oct-2005 at 20:08.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#4
|
|||
|
|||
Re: Help With StacksChanged the code again. Problem still exists but it's starting to clear away.
CPP / C++ / C Code:
Last edited by admin : 09-Oct-2005 at 20:08.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#5
|
|||
|
|||
Re: Help With StacksQuote:
I gave some general observations and a couple of specific hints about glaring errors. I hope you didn't think that my post had all the information you need to get satisfaction. By the way, this is wrong: CPP / C++ / C Code:
The variable y is a pointer, so the print statement should be CPP / C++ / C Code:
This brings me to the point: It's not enough for you to say, "the problems don't seem to be clearing up." In order for you to have some hope of getting meaningful help, I recommend that you run a simple test case. Maybe you could give the program a couple of two-digit numbers to add so that it will be easy to test the results, and in worst case, you could print out every single step of the operations. Make a new file that has the main program, the addition routine and whatever input and output stuff that you think you need. You could even hard-code values for the input variables so as not to get hung up in file input routines. Then: 1. Tell us what the input was. (Also, for your information and ours, have the program print out the input values that is using.) 2. Tell us what the output was. (Copy/paste from your screen to the post whatever the program printed; you might explain what the printout actually represents.) 3. Tell us what you expected the output to be. How did you arrive at the values of your expected outputs? Did you step through the program manually with the given input values or what? 4. Tell us what you don't understand about the difference between expected and actual program output. Additional debugging hints: Put in some print statements in the internal routines for pushing, popping, adding digits, showing sum and carry at each step, or whatever would give you (and us) a clue as to what is going on. In other words: simplify the program and give a simple test case. Regards, Dave "Simplify, simplify!" ---Henry David Thoreau |
|
#6
|
|||
|
|||
Re: Help With StacksI revised the code again to this.
CPP / C++ / C Code:
I'm sorry I didn't mention this earlier but I have been using a test case to run the program, which is how I know it's not running correctly even though the compiler says there are no errors. Given the data file 3 26 32 3523011938638144 619232 1 100 Which is supposed to result in 58 832 3523011939257376 2181561728786775185408 101 100 The program puts out 5 8 35230119 3925 10 1 With the current code. Obviously this isn't correct, but what I've just discovered is quite curious. The first two result numbers, 5 and 8 make 58, which is what the addition function is supposed to display. So it goes with the others. It seems that instead of displaying the result of addition and multiplication, it's displaying the result of addition broken into two values. Quite odd. Last edited by admin : 09-Oct-2005 at 20:07.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#7
|
|||
|
|||
Re: Help With StacksChanged the code again. Now the addition works correctly. The multiplication on the other hand keeps resulting in an empty stack. Working on it.
CPP / C++ / C Code:
Last edited by admin : 09-Oct-2005 at 20:06.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#8
|
|||
|
|||
Re: Help With StacksSo, I rewrote the program a bit so now nothing is global. The addition still works fine, but the multiplication isn't. I don't know what's wrong with it. I've got it so that now it still spits out "Stack is empty" for the first two responses and a large number for the third, which doesn't really help my problem any. Anyone have any suggestions?
CPP / C++ / C Code:
Last edited by admin : 09-Oct-2005 at 20:05.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#9
|
|||
|
|||
Re: Help With StacksApologies to the admin. I did not realize there were specific tags for C code. Anyway, as near as I can tell, somewhere in the multiplication function some pop operation is popping an empty stack, but I can't figure out where or how. If someone could please assist me in rectifying this situation I would be most grateful.
|
|
#10
|
|||
|
|||
Re: Help With StacksQuote:
There are thee possibilities that come to mind (I have not gone through your code to try to find which of them apply; that's your job): 1. Your algorithm for multiplication is wrong. 2. Your code doesn't actually implement the algorithm that you had in mind. 3. All of the above. Since your program is running, but giving the wrong answer (no answer in this case), make the program tell you what it's working with. You might have to put many, many printf statements to get to the bottom of things. Sometimes it's easier to run through the code with pencil-and-paper for a simple example (pretend you are the computer); sometimes it's more time-effective to let the computer do the work. So in your multiplication routine, you could have something like the following: CPP / C++ / C Code:
Regards, Dave |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| queues, stacks or lists? | zidanefreak01 | C++ Forum | 22 | 15-Aug-2005 21:13 |
| stacks or queue | zidanefreak01 | MS Visual C++ / MFC Forum | 1 | 08-Aug-2005 13:40 |
| Issue with stacks | CronoX | C++ Forum | 10 | 10-Mar-2004 15:30 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The