0
votes

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.

2
You haven't stated what your question or error is. You do need to think through your code carefully, as you are re-using names in your 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, global sum. Also, nowhere did you initialize sum to zero, so that needs to be done at an appropriate place before you start adding things to it.lurker
Hello, I have revised my code. I removed the modules for clarity. I also further explained my issue/goal. Thank you.pseud0
I think you're very close, and getting rid of the function actually made it simpler in this case. That says that functions/modules aren't always necessary if the problem is short. Here are a couple things to still consider with your updated code: (1) the for loop is missing the begin and end for the loop block; (2) You need to initialize sum 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
Fixed those issues. Read update @ 3:40pseud0
Got it ;) I put sum := 0 after the for loop. It makes sense now... somewhat lolpseud0

2 Answers

0
votes

In numPromptLoop change the name of the NUM parameter to SUM

0
votes

In summary (pun intended), your final corrected listing is:

program summation;

var num, sum, counter, userValue : integer;

begin
  { Prompt the user for how many times they wish to sum up a list of numbers }

  writeln('Run program how many times?');
  readln(userValue);

  { For each time the user wants to sum numbers, prompt them for the numbers }

  for counter := 1 to userValue do
  begin
    sum := 0;   { start with a sum of 0 for this list }

    { Repeatedly request a number from the user until they enter a negative to end }
    repeat
      { Prompt for and get the number }
      writeln('Enter a number: ');
      readln(num);

      if num >= 0 then
        sum := num + sum;  { add the number to the sum if it's not negative }
    until num < 0;  { finish with this sum if the number entered is negative }

    { Write out the last calculated sum }
    writeln('The sum is: ', sum);
    readln;  { Let the user press enter to go around and request another sum }
  end;
end.