## Pascal

Moderators: None (Apply to moderate this forum)
Number of posts: 14016

This Forum Only

A simple program for pros..IM STUCK! Posted by JP161470 on 10 Nov 2008 at 6:12 AM
A plus B... again
Given two integers A and B, find their exact sum.

This time there are no limitations on A and B (of course, they will fit in memory).

Primes
Given an integer N <= 100, output the first N primes, one per line.

Sample Input:
4

Sample Output:
2
3
5
7

THX really stuck on this questions but i no they are easy but im a beginner so cant seem to get it right.
Re: A simple program for pros..IM STUCK! Posted by Actor on 10 Nov 2008 at 10:43 PM
A plus B... again
I’m not really clear about what you’re asking here. Surely you’re aware that
```     Sum := A + B ;
```

will add A and B, and put the result in Sum.

And there will be limits on A and B. The biggest integer type is LongInt which takes up four bytes. The greatest value that can be stored in a LongInt is +2,147,483,647.

Primes
This one’s easier. A number is prime if it cannot be evenly divided by any prime number that is less than it is. You need an array to keep track of all the prime numbers your program has found thus far, up to 100. Establish an index, i, and initialize it to 2. Then go into a loop. In psuedocode:
```     j := 2 ;
i := 1 ;
while i <= N do
if (j is prime) then
A[i] := j
i    := i + 1
end
j := j + 1
end
```

Then print out the array
```         for j := 1 to N do
writeln (A[j])
```

The big challenge here is to determine if “j is prime.” This is best handled by a function
```          function isprime(j : LontInt) : Boolean ;
```

which returns TRUE is the number is prime. Giving IsPrime access to the Array it can determine this by
```               j mod A[i] = 0
```

for all the A[i] found so far. IsPrime is FALSE if this statement is ever TRUE.

Re: A simple program for pros..IM STUCK! Posted by JP161470 on 11 Nov 2008 at 4:25 PM
: A plus B... again
: I’m not really clear about what you’re asking here. Surely you’re
: aware that
:
```:
:      Sum := A + B ;
: ```
:
: will add A and B, and put the result in Sum.
:
: And there will be limits on A and B. The biggest integer type is
: LongInt which takes up four bytes. The greatest value that
: can be stored in a LongInt is +2,147,483,647.
:
: Primes
: This one’s easier. A number is prime if it cannot be evenly divided
: by any prime number that is less than it is. You need an array to
: keep track of all the prime numbers your program has found thus far,
: up to 100. Establish an index, i, and initialize it to 2. Then go
: into a loop. In psuedocode:
:
```:
:      j := 2 ;
:      i := 1 ;
:      while i <= N do
:           if (j is prime) then
:                A[i] := j
:                i    := i + 1
:           end
:           j := j + 1
:      end
: ```
:
: Then print out the array
:
```:
:          for j := 1 to N do
:                writeln (A[j])
: ```
:
: The big challenge here is to determine if “j is prime.” This is
: best handled by a function
:
```:
:           function isprime(j : LontInt) : Boolean ;
: ```
:
: which returns TRUE is the number is prime. Giving IsPrime access to
: the Array it can determine this by
:
```:
:                j mod A[i] = 0
: ```
:
: for all the A[i] found so far. IsPrime is FALSE if this statement
: is ever TRUE.
:
:
so what would the final "prime" question look like...the whole code? sry really new to this so got all confused what you said would be easier to see if the whole code was there at once. And for the "a plus b 2" thats all they have give but this doesnt work:

```readln(a,b);
writeln(a+b);```

and they also gave a hint which i forgot to mention before is that it said it uses string and array.
Re: A simple program for pros..IM STUCK! Posted by Actor on 11 Nov 2008 at 10:46 PM
:
: so what would the final "prime" question look like...the whole code?
: sry really new to this so got all confused what you said would be
: easier to see if the whole code was there at once. And for the "a
: plus b 2" thats all they have give but this doesnt work:
:
:
```: readln(a,b);
: writeln(a+b);```
:

It should work. It does work on my computer. The problem may be with the way you're entering the data. If you enter a and b on the same line you need to separate them with a space. It's kind of intuitive to separate them with a comma but if you do the program will see the comma as your input for b, and since a comma is not an integer you get a run time error.

Another way to enter the data is to enter the first number and hit ENTER, then enter the second number and hit ENTER again.

Re: A simple program for pros..IM STUCK! Posted by Actor on 11 Nov 2008 at 11:38 PM
: : A plus B... again
: : I’m not really clear about what you’re asking here. Surely you’re
: : aware that
: :
```: :
: :      Sum := A + B ;
: : ```
: :
: : will add A and B, and put the result in Sum.
: :
: : And there will be limits on A and B. The biggest integer type is
: : LongInt which takes up four bytes. The greatest value that
: : can be stored in a LongInt is +2,147,483,647.
: :
: : Primes
: : This one’s easier. A number is prime if it cannot be evenly divided
: : by any prime number that is less than it is. You need an array to
: : keep track of all the prime numbers your program has found thus far,
: : up to 100. Establish an index, i, and initialize it to 2. Then go
: : into a loop. In psuedocode:
: :
```: :
: :      j := 2 ;
: :      i := 1 ;
: :      while i <= N do
: :           if (j is prime) then
: :                A[i] := j
: :                i    := i + 1
: :           end
: :           j := j + 1
: :      end
: : ```
: :
: : Then print out the array
: :
```: :
: :          for j := 1 to N do
: :                writeln (A[j])
: : ```
: :
: : The big challenge here is to determine if “j is prime.” This is
: : best handled by a function
: :
```: :
: :           function isprime(j : LontInt) : Boolean ;
: : ```
: :
: : which returns TRUE is the number is prime. Giving IsPrime access to
: : the Array it can determine this by
: :
```: :
: :                j mod A[i] = 0
: : ```
: :
: : for all the A[i] found so far. IsPrime is FALSE if this statement
: : is ever TRUE.
: :
: :
: so what would the final "prime" question look like...the whole code?
: sry really new to this so got all confused what you said would be
: easier to see if the whole code was there at once. And for the "a
: plus b 2" thats all they have give but this doesnt work:
:
:
```: readln(a,b);
: writeln(a+b);```
:
:
: and they also gave a hint which i forgot to mention before is that
: it said it uses string and array.
:

Here's a program that prints out all primes up to 1000 using an algorithm called the Seive of Eratosthenes. I found it at

http://www.scriptol.org/sieve.php#pascal

I've reformatted it and made some minor changes.

```Program Eratosthenes ;
{
a program to print out all primes up to 1000
using the Sieve of Eratosthenes.
}
CONST
N = 1000 ;

Var
P     :  Array [1 .. N] of Boolean ;
i,j,m :  Word ;

begin
for i := 1 TO N do
P[i] := TRUE ;

m := trunc(sqrt(N)) ;
for i := 2 to m do
if P[i] then
for j := 2 to N DIV i do
P[i * j] := FALSE ;

for i := 1 to N do
if P[i] then
write(i:4) ;
end.
```

With a couple of modifications this can be the program you need. First, is 1 prime? According to this program it is, but from your example printout it is not. Whatever! You can include a statement to set P[1] := FALSE.

Second, you need to use a counter so that the program prints out only the number of primes you want, not all of them. And of course you need to include a prompt and a readln to get the number primes you want to print.

Re: A simple program for pros..IM STUCK! Posted by JP161470 on 12 Nov 2008 at 6:00 AM
KK i got the prime one thx but in a plus b this code doesnt work:

var
a,b:integer;
begin
writeln(a+b);
end.

THE HINT WAS THAT IT uses string and arrays somehow.
Re: A simple program for pros..IM STUCK! Posted by Actor on 12 Nov 2008 at 2:18 PM
: KK i got the prime one thx but in a plus b this code doesnt work:
:
: var
: a,b:integer;
: begin
: writeln(a+b);
: end.

:
: THE HINT WAS THAT IT uses string and arrays somehow.
:
The code you have posted should work.

Exactly how does in not work? Does it compile? Do you get a run-time error?

I don't see how strings and arrays enter into it. What is the exact wording or the problem?

Re: A simple program for pros..IM STUCK! Posted by JP161470 on 12 Nov 2008 at 3:09 PM
: : KK i got the prime one thx but in a plus b this code doesnt work:
: :
: : var
: : a,b:integer;
: : begin
: : writeln(a+b);
: : end.

: :
: : THE HINT WAS THAT IT uses string and arrays somehow.
: :
: The code you have posted should work.
:
: Exactly how does in not work? Does it compile? Do you get a
: run-time error?
:
: I don't see how strings and arrays enter into it. What is the exact
: wording or the problem?
:
-----------------------------------------------------------------------

the question i sent was copied and pasted so its the exactly worded and we did two questions. the first one it was the code i put before but this second time it says there is no limitation on A and B.
Re: A simple program for pros..IM STUCK! Posted by Actor on 12 Nov 2008 at 4:16 PM
: : : KK i got the prime one thx but in a plus b this code doesnt work:
: : :
: : : var
: : : a,b:integer;
: : : begin
: : : writeln(a+b);
: : : end.

: : :
: : : THE HINT WAS THAT IT uses string and arrays somehow.
: : :
: : The code you have posted should work.
: :
: : Exactly how does in not work? Does it compile? Do you get a
: : run-time error?
: :
: : I don't see how strings and arrays enter into it. What is the exact
: : wording or the problem?
: :
: ---------------------------------------------------------------------
: --
:
: the question i sent was copied and pasted so its the exactly worded
: and we did two questions. the first one it was the code i put before
: but this second time it says there is no limitation on A and B.
:
Ok. It says "this time." What was it the previous time?

Re: A simple program for pros..IM STUCK! Posted by JP161470 on 12 Nov 2008 at 6:02 PM
: Ok. It says "this time." What was it the previous time?
:
:=========================================================================

Ok the first time it was "a plus b"

and the answer to that was:

```var
a,b:integer;
begin
writeln(a+b);
end.```

and then the second one we got was "a plus b..again" and it had the same instruction but it said that the variables "a" and "b" had no limitations on how many numbers.
Re: A simple program for pros..IM STUCK! Posted by Actor on 12 Nov 2008 at 11:05 PM
: : Ok. It says "this time." What was it the previous time?
: :
: :====================================================================
: =====
:
: Ok the first time it was "a plus b"
:
: and the answer to that was:
:
:
```: var
:  a,b:integer;
: begin
:  writeln(a+b);
: end.```
:
:
: and then the second one we got was "a plus b..again" and it had the
: same instruction but it said that the variables "a" and "b" had no
: limitations on how many numbers.
:

Ok. Now it makes sense. In the original a and b were integers and the maximum value of an integer is 32767. What your second assignment wants is to surmount that limitation.

One approach is to store a and b as strings. The sum is stored in a third string. In Turbo Pascal (my compiler) a string has a maximum length of 255 characters.

```Program BigSum ;

CONST
BLANK = Chr(32) ;
ZERO  = '0' ;

Var
a, b, Sum : String ;
x, y, z   : Byte ;

begin
{
data entry
}
Write ('Enter the first integer ') ;
Write ('Enter the second integer ') ;
{
least significant digit is on the right so we right justify both
}
while Length(a) < 255 do
a := BLANK + a ;
while Length(b) < 255 do
b := BLANK + b ;
{
}
for i := 255 downto 1 do begin
x := Ord(a[i]) - Ord(ZERO) ;
y := Ord(b[i]) - Ord(ZERO) ;
z := x + y ;
Sum[i] := Chr(Ord(ZERO) + z)
end ;
{
left  justify
}
while Sum[1] = BLANK do
Delete(sum,1,1) ;
{
print the result
}
WriteLn(sum)
end.
```

I have not compiled and tested this but I hope it gives you an idea of the approach. As is, there are a couple of bugs in it. First, it will only work with positive integers. Second, when the sum of two digits is more than 9 it will not handle the carry. Handling the carry is fairly simple.

```Program BigSum ;

CONST
BLANK = Chr(32) ;
ZERO  = '0' ;

Var
a, b, Sum : String ;
x, y, z   : Byte ;
Carry     : Boolean ;

begin
Carry := FALSE ;
{
data entry
}
Write ('Enter the first integer ') ;
Write ('Enter the second integer ') ;
{
least significant digit is on the right so we right justify both
}
while Length(a) < 255 do
a := BLANK + a ;
while Length(b) < 255 do
b := BLANK + b ;
{
}
for i := 255 downto 1 do begin
x := Ord(a[i]) - Ord(ZERO) ;
y := Ord(b[i]) - Ord(ZERO) ;
z := x + y ;

if Carry then
z := z + 1 ;

if z < 10 then
Carry := FALSE
else begin
Carry := TRUE ;
z := z - 10
end ;

Sum[i] := Chr(Ord(ZERO) + z)
end ;
{
left  justify
}
while Sum[1] = BLANK do
Delete(sum,1,1) ;
{
print the result
}
WriteLn(sum)
end.
```

There is no error checking here. The program will only work correctly if the input is valid, i.e., no non-numeric characters appear in the input.

There is still a limit of course, but a series of 255 nines represents a very big number, more than the total number of electrons in the known universe or the number of nanoseconds since the big bang.

Re: A simple program for pros..IM STUCK! Posted by JP161470 on 13 Nov 2008 at 6:11 AM
Did this work in yours? I opened it said unknown identifier in:
```for i := 255 downto 1 do begin
```

so then i add the varible "i" in the var section:
```x, y, z , 1  : Byte ;
```

then it worked so then i tried entering to numbers 2 and 3 buh it didnt give the answer. So whats wrong now?
Re: A simple program for pros..IM STUCK! Posted by Actor on 13 Nov 2008 at 3:16 PM
I did not try to make it work. I was just trying to suggest a strategy. Yes, you do need to declare i. Another bug is that the strings need to be front padded with ZEROs, not BLANKs. A very subtle bug is that as the program builds the string Sum the program has no awareness of what is going on and does not really know the length of Sum. I've cured this by initializing Sum to a null string and adding one character at a time via the concatenation operator +, which keeps the program updated as to the length of the string. The program now works on my machine.
```Program BigSum ;

CONST
ZERO  = '0' ;

Var
a, b, Sum : String ;
i,
x, y, z   : Byte ;
Carry     : Boolean ;

begin
Carry := FALSE ;
{
data entry
}
Write ('Enter the first integer  ') ;
Write ('Enter the second integer ') ;
{
least significant digit is on the right so we right justify both
}
while Length(a) < 255 do
a := ZERO + a ;
while Length(b) < 255 do
b := ZERO + b ;
{
}
Sum := '' ;
for i := 255 downto 1 do begin
x := Ord(a[i]) - Ord(ZERO) ;
y := Ord(b[i]) - Ord(ZERO) ;
z := x + y ;

if Carry then
z := z + 1 ;

if z < 10 then
Carry := FALSE
else begin
Carry := TRUE ;
z := z - 10
end ;

Sum := Chr(Ord(ZERO) + z) + Sum
end ;
{
left  justify
}
while Sum[1] = ZERO do
Delete(sum,1,1) ;
{
print the result
}
WriteLn('The sum is               ',sum)
end.
```

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

Re: A simple program for pros..IM STUCK! Posted by JP161470 on 13 Nov 2008 at 6:19 PM
: 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
:
:
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! :)
Re: A simple program for pros..IM STUCK! Posted by JP161470 on 15 Nov 2008 at 10:18 AM
: : 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
: :
: :
: 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! :)
:

so any idea what i should do now?

Re: A simple program for pros..IM STUCK! Posted by Actor on 15 Nov 2008 at 6:51 PM
: : 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! :)
: :

:
: so any idea what i should do now?

:
:
What were the test cases?

You said that there was a hint that the solution uses string and arrays. If it uses strings then a limit of 255 characters is probably acceptable. To go beyond that I'd use a linked list instead of strings (or arrays). Are you comfortable with pointers and linked lists?

I'll see what I can come up with as far as handling negative integers.

Take care.

Actor
Re: A simple program for pros..IM STUCK! Posted by Actor on 15 Nov 2008 at 6:52 PM
: : : 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
: : :
: : :
: : 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! :)
: :

:
: so any idea what i should do now?

:
:
Are you familiar with procedures and functions?

Re: A simple program for pros..IM STUCK! Posted by JP161470 on 16 Nov 2008 at 5:07 PM
: : : : 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
Re: A simple program for pros..IM STUCK! Posted by Actor on 16 Nov 2008 at 10:35 PM

Negative integers 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.

Normalizing 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.

Input 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.
```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.
```

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic