I am having a bit of an issue with this problem. I am taking a Pascal programming class and this problem was in my logic book. I am required to have the user enter a series of (+) numbers and once he/she enters a (-) number, the program should find the sum of all the (+) numbers. I accomplished this, but now I am attempting part two of this problem, which requires me to utilize a nested loop to run the program x amount of times based on the user's input.
I do not know how to rerun the summation process based on a number the user enters. In other words, the program is required to 1) Ask the user how many times he/she would like to run the program
2) Begin the nested loop that prompts the user for a series of positive numbers 3) User enters numbers as loop asks for them 4) A negative number then signals the end of the series 5) After the repeat until loop, the program should then add all of the positive numbers together
steps 2-4 is one iteration of the program. I need this to run x amount of times, of course, based on user input.
The following code is what I have so far and honestly I am stumped:
program summation;
var num, sum, counter, userValue : integer;
begin
writeln('Run program how many times?');
readln(userValue);
for counter := 1 to userValue do
begin
sum := 0;
repeat
writeln('Enter a number: ');
readln(num);
if num >= 0 then
begin
sum := num + sum;
end;
until num < 0;
writeln('The sum is: ', sum);
readln();
end;
end.
Update [6/27] 3:40 Pacific Time Output: I attempted to upload an image of the output, but I require 10 rep points. Anyway, the program's output is as follows:
How many times would you like the program to run? 2 Enter a number: 1 Enter a number: 1 Enter a number: -1 <-- negative number signals one iteration of the nested loop Enter a number: 2 Enter a number: -3 <-- negative number signals one iteration of the nested loop The sum is: 6
The negative number signals the program to stop an iteration. However, I would like the program to repeat the summation of a sequence three times.
Update [6/27] 7:25PM Pacific Time
Currently my program executes correctly. By correctly I mean it (1) Asks the user how many times he/she would like to run it. (2) The nested loop begins and prompts user for a series of numbers. (3) Once a negative number is entered it signals the end of the series. (4) The program sums the positive numbers. (5) The program restarts by asking the user for another series of numbers. (6) Once again a negative number ends the series. (7) Error begins here: Once the program iterates (series of number prompts) according to the user defined number, it adds all of the sums from previous iterations to the final sum. This is not my goal. My goal is to have separate sums (one for each run) not all sums "summed" at the final iteration.
numPromptloop
function that are also global to the main program. It is therefore unclear to me whether, for example,numPromptloop
is supposed to just pass back an isolated sum, or keep accumulating into the main, globalsum
. Also, nowhere did you initializesum
to zero, so that needs to be done at an appropriate place before you start adding things to it. – lurkerfor
loop is missing thebegin
andend
for the loop block; (2) You need to initializesum
to 0 somewhere since it's not set to 0 before you sum up your numbers. I'm still unclear on one requirement: is it supposed to just show the sum of each series, or of ALL entries for all series, or both? – lurker