Sunday, November 18, 2007

Giving Up

Wow, I've really hit a wall on this one. Assignment's due tomorrow at 5 pm so unless I can get an answer from the professor or TA, I'm shit outta luck.

The assignment is called "Long Integer Addition" and the idea is to take two numbers (up to but not exceeding 30 digits) and add them together. Things to keep in mind:

-The "long integers" are not numeric data types (they are too long to be represented on 32 (or 64) bit machines). They are strings. What the fuck.
-Ada is very strongly typed, meaning casting (conversion) can't be done from integer to string. VB6's CInt and CStr will not work here.
-How do you add two values from a string literal and come up with their sum? You don't.

Well it would be nice if I could get far enough to do that, but I can't even get the basic input to go through. It'll take the first variable just fine, but freaks out when it comes to deux. Code:

Ada.Text_IO.Put(Item => "Enter a nonnegative long integer(30 digits maximum) >");
Ada.Text_IO.New_Line;
Ada.Text_IO.Get_Line(Item => X, Last => StrLenX);
Ada.Integer_Text_IO.Put(Item => StrLenX);
Ada.Text_IO.New_Line(2);
FOR Idx IN REVERSE 1 .. StrLenX LOOP
ArrayX(Idx) := X(Idx);
END LOOP;

That seems to work just fine. X is a 30-character length string and StrLenX is the length of the string. The For..Next loop at the bottom assigns the contents of the string to an array holding characters '0' through '9', the idea being that since each character will be stored as such in the string X, nothing outside that range will ever need to be represented.

A conditional would catch when the sum was over 9 and would carry the one, but I never got far enough to be concerned with that.

That first block works fine, but an identical block below it to correspond to Y variables (all declared as used obviously, the compiler would never let me get away with something stupid like that). It raises a data error when it goes to get string Y, which means that somewhere the data types are being misrepresented or mismatched through some bizarre and presently incomprehensible means.

That's all OK though, I don't know how to do this without casting the characters to integers and back.

1 comment:

Mitch Klein said...

Damn, ADA sucks. It seems like your concept is sound. Not sure why it's not working.