# A simple program for pros..IM STUCK!

2»

• : : : The program still does not know what to do with a minus sign (or a
: : : plus sign for that matter) and will not work with negative numbers.
: : : I've tried to make it work with negative numbers but the code
: : : quickly gets tricky. I think the algorithm can be made to work with
: : : negative numbers but it needs considerable modification.
: : :
: : : Good luck.
: : :
: : : Actor
: : :
: : :
: : [color=Green]Thanks buddy the program worked but got some test cases
: : right and most wrong this is what it said:
: :
: : "Integers don't have to be positive
: :
: : Also you seem to have a limit of 255 - but as I said above the
: : strings might be up to 100,000 characters long. (I know this won't
: : work for Pascal strings; you have to read() into a char array.)"
: :
: : and by the way im really thankfull to you even if i dont get this
: : right thanks alot for helping me!
: : [/color]
: [color=Green]
: so any idea what i should do now?[/color]
:
:
Are you familiar with procedures and functions?

• : : : : The program still does not know what to do with a minus sign (or a
: : : : plus sign for that matter) and will not work with negative numbers.
: : : : I've tried to make it work with negative numbers but the code
: : : : quickly gets tricky. I think the algorithm can be made to work with
: : : : negative numbers but it needs considerable modification.
: : : :
: : : : Good luck.
: : : :
: : : : Actor
: : : :

: :
: :
: Are you familiar with procedures and functions?
:
:

Yes i am not alot but abit yes and as far as the test cases goes it doesnt show us it only shows us the output of the wrong test cases
• Let's address several issues separately.

[b]Negative integers[/b] The algorithm in the following program inputs the numbers as strings then copies the digits into arrays of integers, one integer for each digit. The first character in the string is can be anything, but if it is a minus then the sign of the number is negative, otherwise it's positive. Once the sign is determined the first character is deleted if it is not a digit. The remaining characters all have to be digits, there is no error checking. The two arrays are added together to form the Sum.

[b]Normalizing[/b] Each element of the Sum array will have a value in the range -18 .. +18. It will probably be a mix of positive and negative values. We have to determine the sign of the Sum and make sure each element of Sum matches it. We cannot do this by merely changing the sign. We have to make sure the absolute value falls in the range 0 .. 9 by paying close attention to the carry.

I think the normalizing routine here is very ugly since it considers four separate cases (1)positive and the integer is negative, (2)positive and the integer > 9, (3)negative and the integer is positive, (4)negative and the integer < -9. There may be a more elegant way to handle this but I can't think of anything the I consider safe.

[b]Input[/b] You seem to be concerned that there is a limit of 255 digits. If the data is read in as a string there is no way around this. You could read in more digits one at a time using ReadKey but it would be more complex. Since the hint mentions strings the system I use here is probably OK.

255 characters = 156 googols.
[code]
Program BigSum ;

CONST
MINUS = '-' ;
ZERO = '0' ;

Type
BigInt = Array [1 .. 255] of Integer ;

Procedure ReadInput (Var Int : BigInt) ;

Var
Buf : String ;
i : Byte ;
Negative : Boolean ;

begin
Negative := (Buf[1] = MINUS) ;
if NOT (Buf[1] IN ['0' .. '9']) then
Delete (Buf, 1, 1) ;
while Length(Buf) < 255 do
Buf := ZERO + Buf ;
for i := 255 downto 1 do
if Negative then
Int[i] := Ord(ZERO) - Ord(Buf[i])
else
Int[i] := Ord(Buf[i]) - Ord(ZERO) ;
end ;

Procedure GetSum (A,B : BigInt ; Var Sum : BigInt);

Var
i : Byte ;

begin
for i := 255 downto 1 do
Sum[i] := A[i] + B[i] ;
end ;

Function GetSign (X : BigInt) : Boolean ;

Var
i : Byte ;

begin
{
Sign of first non-zero element is sign of X
}
for i := 1 to 255 do
if X[i] <> 0 then begin
GetSign := (X[i] >= 0) ;
Exit
end ;
{
if we reach this point then X is all zeros
}
GetSign := TRUE
end ;

Procedure Normalize (Var Sum : BigInt ; Sign : Boolean) ;

Var
Carry : Integer ;
i : Byte ;

begin
Carry := 0 ;
if Sign then begin { Sum is positive }
for i := 255 downto 1 do begin
Sum[i] := Sum[i] + Carry ;
Carry := 0 ;
while Sum[i] < 0 do begin { case 1 }
Sum[i] := Sum[i] + 10 ;
Carry := Carry - 1
end ;
while Sum[i] > 9 do begin { case 2 }
Sum[i] := Sum[i] - 10 ;
Carry := Carry + 1
end
end
end
else begin { Sum is negative }
for i := 255 downto 1 do begin
Sum[i] := Sum[i] + Carry ;
Carry := 0 ;
while Sum[i] > 0 do begin { case 3 }
Sum[i] := Sum[i] - 10 ;
Carry := Carry + 1
end ;
while Sum[i] < -9 do begin { case 4 }
Sum[i] := Sum[i] + 10 ;
Carry := Carry - 1
end
end
end
end ;

Procedure WriteResult (Positive : Boolean ; X : BigInt);

Var
i,j : Byte ;
PrintIt : Boolean ;

begin
if NOT Positive then
Write (MINUS) ;
PrintIt := FALSE ;
for i := 1 to 255 do begin
if X[i] <> 0 then
PrintIt := TRUE ;
if PrintIt then
Write (Chr(Abs(X[i]) + Ord(ZERO)))
end ;
WriteLn
end ;

Var
A, B, Sum : BigInt ;
Positive : Boolean ; { TRUE if positive }

begin
Write ('Enter first number ') ;
Write ('Enter second number ') ;
GetSum (A, B, Sum) ;
Positive := GetSign (Sum) ;
Normalize (Sum, Positive) ;
Write (' Their sum is ') ;
WriteResult (Positive, Sum)
end.
[/code]

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!