1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Pascal array problem

  1. May 31, 2012 #1
    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. jcsd
  3. May 31, 2012 #2
    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;
    read(x);
    while x!='D'
    write(S);
     
  4. May 31, 2012 #3
    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.
     
  5. May 31, 2012 #4
    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...
     
  6. Jun 1, 2012 #5
    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?
     
  7. Jun 1, 2012 #6
    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
    read(a);
    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...
     
  8. Jun 1, 2012 #7
    Thanks, it seems good but unfortunately it's not working. When I hit Ctrl-D nothing happens.
     
  9. Jun 1, 2012 #8
    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
     
  10. Jun 1, 2012 #9
    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.
     
  11. Jun 1, 2012 #10

    Mark44

    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.
     
  12. Jun 2, 2012 #11
    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?
     
  13. Jun 2, 2012 #12
    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
  14. Jun 2, 2012 #13

    NascentOxygen

    User Avatar

    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
  15. Jun 2, 2012 #14

    Mark44

    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.
     
  16. Jun 2, 2012 #15
    Hi, I installed a pascal compiler, so this code works:
    var x, total: integer;

    begin
    total:=0;
    while not eof do
    begin
    read(x);
    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...
     
  17. Jun 3, 2012 #16


    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.
     
  18. Jun 3, 2012 #17

    NascentOxygen

    User Avatar

    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?
     
  19. Jun 3, 2012 #18
    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.
     
  20. Jun 3, 2012 #19

    rcgldr

    User Avatar
    Homework Helper

    If this is DOS or Windows, use ctrl-Z instead (intepreted as end of file).
     
  21. Jun 3, 2012 #20

    NascentOxygen

    User Avatar

    Staff: Mentor

    Can you post a listing of your program where that error is indicated?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Pascal array problem
  1. Pascal problem (Replies: 1)

  2. Pascal programming (Replies: 1)

Loading...