# Homework Help: Pascal array problem

1. May 31, 2012

### shinokk

1. The problem statement, all variables and given/known data
I need to write a program which calculates the sum of all the numbers of an array of unknown length. The input of a ends when a's value is the letter 'D'.

2. Relevant equations

3. The attempt at a solution
program homework;
uses wincrt;
var
a:array[1..100] of integer;
S, i:integer;
begin
S:=0;
i:=1;
repeat read(a); S:= S+ a; i:= i+1 until a='D';
write(S);
end.

Last edited: May 31, 2012
2. May 31, 2012

### oli4

Hi shinokk
No, this is certainly wrong, but I don't know which part is more wrong according to the context.
1) you don't know the size of the 'array', yet you upper bound it with 100 as a max length (a:array[1..100]) without ever checking if you are yet hitting this limit
2) this array apparently is not even given to you as a procedure parameter, you build it on repeated 'read'
So I don't know if you actually need an array or not, it depens on what is the actual text of the exercise.
Supposing you are continuously reading the input, then you don't even need an array.
I don't remember pascal, so this is pseudocode
var x, S;
S:=0;
x:=0;
do
S:=S+x;
while x!='D'
write(S);

3. May 31, 2012

### shinokk

Well, first of all, thanks for the reply.
It actually asks for an array in the text of the exercise, I tried doing it without it but the problem I'm getting is that x can't ever be 'D' since it has to have some numerical value before that (it has to be integer). Here's my other try:

program homework;
uses wincrt;
var S, x:integer;
begin
S:=0;
repeat read (x); S:= S+x until x='D';
write (S);
end.

When I compile this I get the error 'Type mismatch' meaning that an integer (number x) can't have the value of 'D'. I tried doing it with a constant stop = 'D' but I get the same error. Also, we don't actually go beyond 30 when we're dealing with arrays, so my 100 was an educated guess. We're just getting to know Pascal.
Basically my only problem is the type of x.

4. May 31, 2012

### oli4

Hi again, sorry, Pascal is looong gone for me now, but I have the intuition that this 'D' to end the conversation can't be a coincidence.
Are you sure this is not ^D ? (Ctrl-D) ?
in this case, you would be doing something like
while not eof do
(the same as before)
end
and you enter integer values until you want to stop by hitting ctrl-D ?

Cheers...

5. Jun 1, 2012

### shinokk

I'm not sure I'm following you. I tried something like that but I still get an error. Could you try to write the code for it?

6. Jun 1, 2012

### oli4

Hi shinokk
I don't have a pascal compiler to test the code so this is pseudo code
var x, total: integer;
total:=0;
while not eof do
begin
total:=total+a;
end
writeln(total);

this program (if it compiled at all, you'll have to adapt) will read for integers from the standard input until end of file (which is ^D)
that is, you would use it by entering the numbers one after an other, and hit ^D to stop, and then the program would rght the sum.
this could look like this:

1
2
3
1
2
^D <-- you terminate here by hitting Ctrl-D with your keyboard
9 <--- this is the result

Hope this helps
Cheers...

7. Jun 1, 2012

### shinokk

Thanks, it seems good but unfortunately it's not working. When I hit Ctrl-D nothing happens.

8. Jun 1, 2012

### oli4

Well as I said I can't test it because I don't have a pascal compiler at hand, but maybe this is just a silly condition that would be fixed by using readln instead of read ?

Cheers

9. Jun 1, 2012

### skeptic2

Where is the array? Is it in a file given to you by your instructor? If so, do you have to read the file into an array? In what format is the instructor's file, for instance text or CSV?

I think you need to format your array as an array of strings (Pascal is highly typed.) Then as you read each element, check to see if the string equals the letter D and if not, convert it to an integer and add it to your sum.

10. Jun 1, 2012

### Staff: Mentor

What exactly is the statement of the problem? What you have here is not a clear description of what you have to work with and what you need to do.

11. Jun 2, 2012

### shinokk

The exact statement of the problem, roughly translated, would be something like this:
Write a program in Pascal which calculates the sum of all the values of the array A. The length of the array is unknown and the input ends when A= D. E.g.
1
2
3
D
S= 6

I have to program everything myself.

I'm not sure how to do that. After a few minutes of looking up 'strings in Pascal' on Google, I'm pretty sure we weren't taught that yet. Could you write a code?

12. Jun 2, 2012

### skeptic2

When you define an array as an array of integer in Pascal, it can only contain integers. It will give you an error if you try to store the letter D. That is why I suggested defining the array as an array of strings and storing the numbers as text. That way the array would accept the letter D. To type the array as an array of string, instead of a: array[1..100] of integer; you would put a: array[1..100] of string; Note: if you do this, all entries must have apostrophes around them such as '123'.

There are many variations in Pascal and it is impossible to know which commands your version uses. In TurboPascal Val is used to convert a string to either an integer or a real. The usage is Val(s: string; var i: integer; var code: integer) So your statement might be Val(a[1], i, code). If a contains a valid integer in string format, it will be converted to an integer an assigned to i. Code will tell you if the conversion was successful. If successful, code will be zero. If not it will contain some other value.

In some versions of Pascal you can assign a type of variant to an array. This will allow it to contain values of different types and may allow you to have both integers and characters in the array. The statement in the type section would be a: array[1..100] of variant;

Last edited: Jun 2, 2012
13. Jun 2, 2012

### Staff: Mentor

For single-digit unsigned numbers, a: array[1..100] of char;

For multi-digit numbers, in pure Pascal I think you must define string as an array of char because string is not a pre-defined type.

Perhaps OP should have it confirmed that the array is to contain multi-digit integers, as if it's single unsigned digits the coding will be simpler.

Last edited: Jun 2, 2012
14. Jun 2, 2012

### Staff: Mentor

"Exact statement" and "roughly translated" don't go together. This description gets us no closer to understanding what you need to do. I think that the best course of action for you is to talk to your instructor to get clarification on what you need to do. As already mentioned, an array of integers can contain numbers, not letters such as 'D'. Since you haven't learned about strings yet, it doesn't seem feasible to store strings of numeric digits in the array and convert them to integers to do the sum calculation.

15. Jun 2, 2012

### oli4

Hi, I installed a pascal compiler, so this code works:
var x, total: integer;

begin
total:=0;
while not eof do
begin
total:=total+x;
end;
writeln(total);
end.
You compile it, run it, and to use it, you just enter integers one after the other
there is no need for an array, and to stop entering values, just hit ctrl-D (I have to hit it twice btw) and then you get the sum.
If you absolutely must process an array of unknown length, well, it sounds fishy that D would mark the end, are you sure you copied this correctly ? maybe it is 0 ?

Cheers...

16. Jun 3, 2012

### shinokk

I'm using Turbo Pascal for Windows. I understand what you're trying to say, but I can't get it to work in TPW.

Thanks for going to so much trouble just to help me. I'm using Turbo Pascal for Windows and this code doesn't work for me (nothing happens when I hit Ctrl-D). And yes, I am sure that D marks the end. I know how to write a code for an array that ends with 0, but this one is a different story altogether.

I reread the text of the statement and I've concluded that ''roughly translated'' can be erased. The statement of the problem is as I had written before. I talked to my instructor and she altered my incorrect solution a bit and said it should work, but when I compiled the program, I got an error and neither of us knew what was wrong. She said she would solve it at home and tell me the solution, but she never did. I'll ask her again for the solution but I doubt she has one.

17. Jun 3, 2012

### Staff: Mentor

What error messages do you get?
Have you typed in a handful of integers for it to add? Can you amend the code so that after each data value it prints out the running tally to confirm that the code is executing as intended?

18. Jun 3, 2012

### shinokk

Ugh: 'string expression expected' '; expected'
And even when I try to correct the ';' error, I get it again and again on the same spot.

Yes, I have. It's working great, I just can't stop the input with Ctrl-D.

19. Jun 3, 2012

### rcgldr

If this is DOS or Windows, use ctrl-Z instead (intepreted as end of file).

20. Jun 3, 2012

### Staff: Mentor

Can you post a listing of your program where that error is indicated?

21. Jun 3, 2012

### shinokk

It's Windows and crtl-Z would completely eliminate the letter 'D' from the program. Anyway, it's not working either.

I'll post it a bit later, I'm not home at the moment.

22. Jun 3, 2012

### oli4

Well I'm sorry that didn't help, don't worry it wasn't much trouble anyway, but I'm on a linux machine, so no TP for me, but I think it should work the same.
I hit ^D twice also, not just once and I find it weird that it does not work for you.
But since you have restated the problem since, apparently it doesn't have to do with stdin anyway, you are asked to loop over a given array without a given legnth and the ending condition would be on a value.
I am just suspicous on the value being 'D' because it doesn't mean anything particularly interesting (while ^D was a natural ending for input)
Apparently it isn't too clear for your teacher either, so this problem could very well never end in diverging interpretations.
Here is an interpretation:
-> you read chars, and you translate them as (single digit) numbers until one of the chars is just 'D' (why not ?)

So this code would work as an example, but it's rather silly...

const n=5;
var values: array[1..n] of char = ('1', '3', '7', '8', 'D') ;
var i, total: integer;
var v:char;
begin
total:=0;
for i:=1 to n do
begin
v:=values;
if(v='D') then break;
total:=total+integer(v)-integer('0');
end;
writeln(total);
end.

note that I put a const length anyway so you might not like it, but the whole thing is silly anyway, you can loop with a while instead and don't mention the length anywhere (except when actually giving the array itself)...

Cheers...

23. Jun 5, 2012

### shinokk

Well, this kind of works. The only problem now is that I get -240 as the sum of 1, 2, 3 and 4.

24. Jun 5, 2012

### oli4

in the line
total:=total+integer(v)-integer('0');
do you understand what is being done ?
Did you copy it by hand and put a letter 'O' or 'o' or something like that instead of '0' (the digit just before '1' ? that would explain it I guess...

Cheers...

25. Jun 5, 2012

### shinokk

hahahahaha
It's a zero. I made some changes so it looks like this:

var values: array[1..5] of char ;
var i, total: integer;
var v:char;
begin
total:=0;
for i:=1 to 5 do
begin
v:=values;
if(v='D') then break;
total:=total+integer(v)-integer('0');
end;
writeln(total);
end.

When I copy your code, I get the correct, solution (1+3+7+8=19), but I wanted to change it so it could work with any numbers and it's not working.

Last edited: Jun 5, 2012