Savanna Kraft

Savanna Kraft

  • 1.9k
  • 2
  • 85

Convert psuedocode program to C#

Nov 17 2019 10:00 AM
Hi I am completely new to programming and need help translating these two pseudocodes to C#:
CLASS Program
BEGIN
//Method that prints out a 1D array with no spaces or lines between each value.
METHOD PrintArray (PARAMETERS: anyArray)
BEGIN
//A for loop that traverses the array.
FOR x ← 0 to anyArray’s length
PRINT anyArray’s value at x
ENDFOR
END PrintArray
METHOD Main
BEGIN
CREATE dnaArray
//Initialize the values of the dnaArray, to a random choice of {A, C, G, T}.
FOR i ← 0 to 254
choice ← RAND(4)
IF choice == 0
dnaArray value at i ← A
ELSEIF choice == 1
dnaArray value at i ← C
ELSEIF choice == 2
dnaArray value at i ← G
ELSEIF choice == 3
dnaArray value at i ← T
ENDFOR
//Print the array with DNA values.
PrintArray(dnaArray)
//Go through the dnaArray and replace the T with a U so that we have a RNA sequence and not DNA anymore.
FOR i ← 0 to 254
IF dnaArray value at i == T
dnaArray value at i ← U
ENDFOR
//Print the array with RNA values.
PrintArray(dnaArray)
END MAIN
END Program
and the 2nd psuedocode is:
CLASS Program
BEGIN
//Method that prints out the values of rabbit array, so that each value is separated by a pipe | and some spaces.
METHOD PrintRabbitArray(PARAMETERS: rabbitArray, rabbitFound)
BEGIN
FOR x ← 0 to rabbitArray’s length
//Print all the values in the array including R.
IF rabbitFound
PRINT rabbitArray value at position x
//Print all values in the array except for R.
ELSE
IF spots == R
PRINT “ | “
ELSE
PRINT spots + “ | “
ENDIF
ENDIF
ENDFOR
END PrintArray
Continued on the next page…
//Method that takes the guess and array and returns true or false if the rabbit was correctly found/guessed.
METHOD FindRabbit(PARAMETERS: rabbitArray, guess)
BEGIN
//Checks if the rabbit was not at the guessed spot and places an x there.
IF rabbitArray at position guess NOT == R
RabbitArray at position i ← X
RETURN FALSE
//Otherwise return true
ELSE
RETURN TRUE
ENDIF
ENDFOR
END FindRabbit
METHOD Main
BEGIN
//Create the rabbit array, which holds the position of the rabbit.
CREATE rabbitArray
//Make a random number between 0 and 10.
rabbitSpot ← RAND(10)
//Use that random number to place the rabbit in that random spot.
rabbitArray at rabbitSpot ← R
//Create a Boolean that will know if the rabbit was found.
rabbitFound ← FALSE
PrintRabbitArray(rabbitArray, rabbitFound)
Continued on the next page…
//Outer loop to keep playing the game until the user enters anything but “Yes”.
WHILE (TRUE)
//Inner loop that keeps running until the user guesses the rabbit’s position.
WHILE (rabbitFound != TRUE)
guess ← READ //Get the guess input.
//Use methods to find the rabbit and print the array in the correct manner depending on if the rabbit was found.
rabbitFound ← FindRabbit(rabbitArray, guess)
PrintRabbitArray(rabbitArray, rabbitFound)
END WHILE
tryAgain ← READ //Get the tryAgain input.
//If the input was “Yes” then the program resets the rabbit array so that it is blank (no Xs or R) and places a R back in the array in a random spot and changes the rabbit found value back to false.
IF tryAgain.Equals(“Yes”)
rabbitFound ← FALSE
rabbitArray.Empty()
rabbitSpot ← RAND(10)
rabbitArray at rabbitSpot ← R
ELSE
BREAK
END WHILE
END MAIN
END Program
if anyone can help me, I would be so thankful!

Answers (1)