Sum of Pascal Array Problem Homework

In summary, the conversation discusses the task of writing a program that calculates the sum of all the numbers in an array of unknown length. The input of the array ends when the letter 'D' is entered. The conversation includes suggestions for how to approach the problem and potential issues that may arise, such as determining the size of the array and handling the end of input. The use of an array and the need for converting input to integers is also mentioned.
  • #1
shinokk
26
0

Homework Statement


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

Homework Equations


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:
Physics news on Phys.org
  • #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);
 
  • #3
oli4 said:
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);

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
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
oli4 said:
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...

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
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...
 
  • #7
oli4 said:
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...

Thanks, it seems good but unfortunately it's not working. When I hit Ctrl-D nothing happens.
 
  • #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
 
  • #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.
 
  • #10
shinokk said:

Homework Statement


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

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
Mark44 said:
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.

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

skeptic2 said:
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 have to program everything myself.

skeptic2 said:
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.

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
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:
  • #13
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:
  • #14
shinokk said:
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.


"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
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...
 
  • #16
skeptic2 said:
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;


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.

oli4 said:
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...

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.

Mark44 said:
"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.

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
shinokk said:
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.
What error messages do you get?
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).
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
NascentOxygen said:
What error messages do you get?

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

NascentOxygen said:
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?

Yes, I have. It's working great, I just can't stop the input with Ctrl-D.
 
  • #19
shinokk said:
Yes, I have. It's working great, I just can't stop the input with Ctrl-D.
If this is DOS or Windows, use ctrl-Z instead (intepreted as end of file).
 
  • #20
shinokk said:
Ugh: 'string expression expected' '; expected'
And even when I try to correct the ';' error, I get it again and again on the same spot.
Can you post a listing of your program where that error is indicated?
 
  • #21
rcgldr said:
If this is DOS or Windows, use ctrl-Z instead (intepreted as end of file).

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

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

I'll post it a bit later, I'm not home at the moment.
 
  • #22
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
oli4 said:
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...


Well, this kind of works. The only problem now is that I get -240 as the sum of 1, 2, 3 and 4.
 
  • #24
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
oli4 said:
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...

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:
  • #26
Hi ?
What do you mean 'any number'
do you want to have a sequence lilke '3', '24', '12', ... for instance ?
This isn't going to work because it's an array of char, not strings, but it is a very silly example anyway.
The best would be to ask your teacher what exactly is the problem that we want to solve.
And why in the world would 'D' in the middle of a numbers sequence represent the end of the array, this is very fishy :)

Cheers...
 
  • #27
v:=values;

The array values has not been assigned any data, so there is nothing to assign to v. Should signal a fatal error.
You might be wanting to use a readln statement to allow you to type in the data at runtime?
 

Related to Sum of Pascal Array Problem Homework

What is the "Sum of Pascal Array Problem Homework"?

The "Sum of Pascal Array Problem Homework" is a mathematical problem that involves finding the sum of all elements in a Pascal array. A Pascal array is a triangular array of numbers where the first and last element in each row is always 1, and the remaining elements are the sum of the two elements directly above it.

What is the purpose of this homework?

The purpose of this homework is to help students understand the concept of Pascal arrays and practice their skills in calculating sums of array elements. It also helps students develop problem-solving and critical thinking skills.

How do I approach this problem?

To solve the "Sum of Pascal Array Problem Homework", you can use a loop to iterate through each row of the array and a variable to keep track of the sum. You can also use the mathematical formula for the sum of a Pascal array, which is 2^n, where n is the number of rows in the array.

What are some common challenges in solving this problem?

One common challenge in solving this problem is keeping track of the row and column indexes while iterating through the array. Another challenge is understanding the mathematical concept behind Pascal arrays and how to apply it in calculating the sum. Additionally, students may face difficulties in implementing the code correctly and debugging any errors.

How can I check if my solution is correct?

You can check your solution by manually calculating the sum of the elements in the array and comparing it to your program's output. You can also use test cases with different array sizes to verify that your code works for all scenarios. Additionally, you can seek feedback from your teacher or peers to ensure the accuracy of your solution.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
2K
Back
Top