Using ADA language. We are supposed to make the Sum_From_1_To_N procedure into a
ID: 3803670 • Letter: U
Question
Using ADA language. We are supposed to make the Sum_From_1_To_N procedure into a function, which I did, but then I need to store the value returned by the function in a variable. What am I doing wrong? It keeps saying: Unmatched actual "sum" in call where sum => result.
WITH Ada.Text_Io, Ada.Integer_Text_Io;
PROCEDURE Summation (Stop_Value : Out Integer; -- Where to stop printing the table
Result : Out Integer; -- Parameter to return the sum
Sum : Out integer;
N : OUT Integer) IS
------------------------------------
-- This program uses a procedure to
-- make a table of the sums of all the
-- integers from 1 to N for various
-- values of N.
Function Sum_From_1_To_N (N :Integer) Return integer is Sum : Integer;
-- A procedure to sum the values from
-- 1 to N and return the sum to the
-- calling program.
BEGIN
Sum := 0;
-- Find the sum from 1 to N using a for loop.
Summation_Loop:
FOR I IN 1 .. N LOOP
Sum := Sum + I;
END LOOP Summation_Loop;
Return Sum;
END Sum_From_1_To_N;
BEGIN
-- Prompt user for ending value of table.
Ada.Text_IO.Put ("Please enter the size of the table: ");
Ada.Integer_Text_IO.Get (Stop_Value);
-- Print table heading
Ada.Text_IO.Put_Line (" Sum of Integers");
Ada.Text_IO.Put_Line (" N from 1 to N");
Ada.Text_IO.Put_Line ("-------------------------");
-- Iterate from 1 to Stop_Value, calculating the sum from 1 to
-- the value of the LCV.
Print_One_Row:
FOR I IN 1 .. Stop_Value LOOP
-- Print the value of I.
Ada.Integer_Text_IO.Put (
Item => I,
Width => 6);
Sum:= Sum_From_1_To_N(N => N);
Result := Sum_From_1_To_N(N => N);
--Calculate the sum from 1 to I.
Sum_From_1_To_N (
N => I,
Sum => Result);
-- Print the sum calculated in the previous step.
Ada.Integer_Text_IO.Put (
Item => Result,
Width => 18);
Ada.Text_IO.New_Line;
END LOOP Print_One_Row;
Explanation / Answer
WITH Ada.Text_Io, Ada.Integer_Text_Io;
use Ada;
PROCEDURE Summation (Stop_Value : Out Integer; -- Where to stop printing the table
Result : Out Integer; -- Parameter to return the sum
Sum : Out integer;
N : OUT Integer) is
------------------------------------
-- This program uses a procedure to
-- make a table of the sums of all the
-- integers from 1 to N for various
-- values of N.
Function Sum_From_1_To_N (N :Integer) Return Integer is
-- A procedure to sum the values from
-- 1 to N and return the sum to the
-- calling program.
BEGIN
Sum := 0;
-- Find the sum from 1 to N using a for loop.
Summation_Loop:
FOR I IN Integer range 1 .. N LOOP
Sum := Sum + I;
END LOOP Summation_Loop;
Return Sum;
END Sum_From_1_To_N;
N:positive
BEGIN
Input:loop
-- Prompt user for ending value of table.
Ada.Text_IO.Put ("Please enter the size of the table: ");
Ada.Integer_Text_IO.Get (N);
-- Print table heading
Ada.Text_IO.Put_Line (" Sum of Integers");
Ada.Text_IO.Put_Line (" N from 1 to N");
Ada.Text_IO.Put_Line ("-------------------------");
Integer_Text_IO.put(N,width=>3);
Integer_text_IO.put(sum_From_1_to_N ,width=>8);
Text_IO.New_Line (2);
end loop Input;
end summation;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.