We read your photo as:

Write and test a procedure Swap, which takes two integers as parameters and returns the first value passed to the procedure as the second value returned by the procedure and vice versa.

Computer Science

ScanSolve AI Answer

Step-by-step solution

4 steps

Here are the solutions for the programming tasks:

Task 35: Swap
Write and test a procedure Swap, which takes two integers as parameters and returns the first value passed to the procedure as the second value returned by the procedure and vice versa.

  1. Define the Swap procedure.

    The procedure will take two integer parameters by reference, use a temporary variable to hold one value, and then perform the swap.

    PROCEDURE Swap(BY REFERENCE A: INTEGER, BY REFERENCE B: INTEGER)DECLARE Temp: INTEGERTemp←AA←BB←TempEND PROCEDUREExample Usage:DECLARE Num1: INTEGERDECLARE Num2: INTEGERNum1←10Num2←20OUTPUT "Before swap: Num1 = ", Num1, ", Num2 = ", Num2CALL Swap(Num1, Num2)OUTPUT "After swap: Num1 = ", Num1, ", Num2 = ", Num2// Expected output: Before swap: Num1 = 10, Num2 = 20// After swap: Num1 = 20, Num2 = 10\begin{align*} &\text{PROCEDURE Swap(BY REFERENCE A: INTEGER, BY REFERENCE B: INTEGER)} \\ &\quad \text{DECLARE Temp: INTEGER} \\ &\quad \text{Temp} \leftarrow \text{A} \\ &\quad \text{A} \leftarrow \text{B} \\ &\quad \text{B} \leftarrow \text{Temp} \\ &\text{END PROCEDURE} \\ \\ &\text{Example Usage:} \\ &\text{DECLARE Num1: INTEGER} \\ &\text{DECLARE Num2: INTEGER} \\ &\text{Num1} \leftarrow 10 \\ &\text{Num2} \leftarrow 20 \\ &\text{OUTPUT "Before swap: Num1 = ", Num1, ", Num2 = ", Num2} \\ &\text{CALL Swap(Num1, Num2)} \\ &\text{OUTPUT "After swap: Num1 = ", Num1, ", Num2 = ", Num2} \\ &\text{// Expected output: Before swap: Num1 = 10, Num2 = 20} \\ &\text{// After swap: Num1 = 20, Num2 = 10} \end{align*}

    The Swap procedure is defined to exchange the values of two integer variables passed by reference.

    Task 36: Sort
    Write and test a procedure Sort, which takes two integers as parameters and returns them in ascending order. For example, if No1 contained 5 and No2 contained 3, then the call Sort(No1, No2) will leave the value 3 in No1 and the value 5 in No2.

  2. Define the Sort procedure.

    The procedure will take two integer parameters by reference. It will compare the two numbers and, if they are not in ascending order, it will call the Swap procedure (from Task 35) to reorder them.

    PROCEDURE Sort(BY REFERENCE No1: INTEGER, BY REFERENCE No2: INTEGER)IF No1 > No2 THENCALL Swap(No1, No2)END IFEND PROCEDUREExample Usage:DECLARE X: INTEGERDECLARE Y: INTEGERX←5Y←3OUTPUT "Before sort: X = ", X, ", Y = ", YCALL Sort(X, Y)OUTPUT "After sort: X = ", X, ", Y = ", Y// Expected output: Before sort: X = 5, Y = 3// After sort: X = 3, Y = 5X←2Y←7OUTPUT "Before sort: X = ", X, ", Y = ", YCALL Sort(X, Y)OUTPUT "After sort: X = ", X, ", Y = ", Y// Expected output: Before sort: X = 2, Y = 7// After sort: X = 2, Y = 7\begin{align*} &\text{PROCEDURE Sort(BY REFERENCE No1: INTEGER, BY REFERENCE No2: INTEGER)} \\ &\quad \text{IF No1 > No2 THEN} \\ &\quad \quad \text{CALL Swap(No1, No2)} \\ &\quad \text{END IF} \\ &\text{END PROCEDURE} \\ \\ &\text{Example Usage:} \\ &\text{DECLARE X: INTEGER} \\ &\text{DECLARE Y: INTEGER} \\ &\text{X} \leftarrow 5 \\ &\text{Y} \leftarrow 3 \\ &\text{OUTPUT "Before sort: X = ", X, ", Y = ", Y} \\ &\text{CALL Sort(X, Y)} \\ &\text{OUTPUT "After sort: X = ", X, ", Y = ", Y} \\ &\text{// Expected output: Before sort: X = 5, Y = 3} \\ &\text{// After sort: X = 3, Y = 5} \\ \\ &\text{X} \leftarrow 2 \\ &\text{Y} \leftarrow 7 \\ &\text{OUTPUT "Before sort: X = ", X, ", Y = ", Y} \\ &\text{CALL Sort(X, Y)} \\ &\text{OUTPUT "After sort: X = ", X, ", Y = ", Y} \\ &\text{// Expected output: Before sort: X = 2, Y = 7} \\ &\text{// After sort: X = 2, Y = 7} \end{align*}

    The Sort procedure ensures that the two input numbers are arranged in ascending order.

    Task 37: Diamond of Stars
    Using procedures, write a program that asks the user to enter an odd number, validates the number, and then displays a diamond of stars based on that number. For example, the value 7 would produce:

       *
      ***
     *****
    *******
     *****
      ***
       *
    
  3. Define the PrintDiamond procedure.

    This procedure will take the odd number N as input and print the diamond pattern.

    PROCEDURE PrintDiamond(N: INTEGER)DECLARE midRow: INTEGERDECLARE row: INTEGERDECLARE numStars: INTEGERDECLARE numSpaces: INTEGERmidRow←(N+1)/2// Upper half of the diamond (including the widest row)FOR row FROM 1 TO midRownumSpaces←midRow−rownumStars←2×row−1FOR i FROM 1 TO numSpacesOUTPUT " "NEXT iFOR i FROM 1 TO numStarsOUTPUT "*"NEXT iOUTPUT NEWLINENEXT row// Lower half of the diamondFOR row FROM midRow - 1 DOWN TO 1numSpaces←midRow−rownumStars←2×row−1FOR i FROM 1 TO numSpacesOUTPUT " "NEXT iFOR i FROM 1 TO numStarsOUTPUT "*"NEXT iOUTPUT NEWLINENEXT rowEND PROCEDURE\begin{align*} &\text{PROCEDURE PrintDiamond(N: INTEGER)} \\ &\quad \text{DECLARE midRow: INTEGER} \\ &\quad \text{DECLARE row: INTEGER} \\ &\quad \text{DECLARE numStars: INTEGER} \\ &\quad \text{DECLARE numSpaces: INTEGER} \\ \\ &\quad \text{midRow} \leftarrow (\text{N} + 1) / 2 \\ \\ &\quad \text{// Upper half of the diamond (including the widest row)} \\ &\quad \text{FOR row FROM 1 TO midRow} \\ &\quad \quad \text{numSpaces} \leftarrow \text{midRow} - \text{row} \\ &\quad \quad \text{numStars} \leftarrow 2 \times \text{row} - 1 \\ &\quad \quad \text{FOR i FROM 1 TO numSpaces} \\ &\quad \quad \quad \text{OUTPUT " "} \\ &\quad \quad \text{NEXT i} \\ &\quad \quad \text{FOR i FROM 1 TO numStars} \\ &\quad \quad \quad \text{OUTPUT "*"} \\ &\quad \quad \text{NEXT i} \\ &\quad \quad \text{OUTPUT NEWLINE} \\ &\quad \text{NEXT row} \\ \\ &\quad \text{// Lower half of the diamond} \\ &\quad \text{FOR row FROM midRow - 1 DOWN TO 1} \\ &\quad \quad \text{numSpaces} \leftarrow \text{midRow} - \text{row} \\ &\quad \quad \text{numStars} \leftarrow 2 \times \text{row} - 1 \\ &\quad \quad \text{FOR i FROM 1 TO numSpaces} \\ &\quad \quad \quad \text{OUTPUT " "} \\ &\quad \quad \text{NEXT i} \\ &\quad \quad \text{FOR i FROM 1 TO numStars} \\ &\quad \quad \quad \text{OUTPUT "*"} \\ &\quad \quad \text{NEXT i} \\ &\quad \quad \text{OUTPUT NEWLINE} \\ &\quad \text{NEXT row} \\ &\text{END PROCEDURE} \end{align*}
  4. Write the main program to get input, validate, and call the procedure.

    PROGRAM DiamondPatternDECLARE UserNum: INTEGERDECLARE isValid: BOOLEANisValid←FALSEWHILE NOT isValidOUTPUT "Enter an odd number (e.g., 7): "INPUT UserNumIF UserNum > 0 AND UserNum MOD 2 = 1 THENisValid←TRUEELSEOUTPUT "Invalid input. Please enter a positive odd number."END IFEND WHILECALL PrintDiamond(UserNum)END PROGRAM\begin{align*} &\text{PROGRAM DiamondPattern} \\ &\quad \text{DECLARE UserNum: INTEGER} \\ &\quad \text{DECLARE isValid: BOOLEAN} \\ \\ &\quad \text{isValid} \leftarrow \text{FALSE} \\ &\quad \text{WHILE NOT isValid} \\ &\quad \quad \text{OUTPUT "Enter an odd number (e.g., 7): "} \\ &\quad \quad \text{INPUT UserNum} \\ &\quad \quad \text{IF UserNum > 0 AND UserNum MOD 2 = 1 THEN} \\ &\quad \quad \quad \text{isValid} \leftarrow \text{TRUE} \\ &\quad \quad \text{ELSE} \\ &\quad \quad \quad \text{OUTPUT "Invalid input. Please enter a positive odd number."} \\ &\quad \quad \text{END IF} \\ &\quad \text{END WHILE} \\ \\ &\quad \text{CALL PrintDiamond(UserNum)} \\ &\text{END PROGRAM} \end{align*}

    The program prompts the user for an odd number, validates the input, and then uses the PrintDiamond procedure to display the star pattern.

Was this helpful?

Still stuck on this one?

Ask a follow-up, and the answer starts from this question.

Got a different question?Ask your own question