0% found this document useful (0 votes)
5 views

CS1181 Intro-To-Computer-Science ISU 2024 MIDTERM psudocodeProgramProb

Uploaded by

cyberguyhurst
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CS1181 Intro-To-Computer-Science ISU 2024 MIDTERM psudocodeProgramProb

Uploaded by

cyberguyhurst
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Reverse array Code

// Function to reverse the array


function reverseArray(arr, sz):
// Initialize two pointers: one at the start and one at the end of the array
left = 0 // Start pointer
right = sz - 1 // End pointer

// Loop until the two pointers meet


while left < right:
// Swap the elements at left and right positions
temp = arr[left]
arr[left] = arr[right]
arr[right] = temp

// Move the left pointer forward


left = left + 1

// Move the right pointer backward


right = right - 1

// Main function to test the reverse function


function main():
// Initialize the array and its size
arr = {9, 4, 8, 0, 1, 0, 6}
sz = 7

// Call the reverse function to reverse the array


reverseArray(arr, sz)

// Output the reversed array


print arr // Expected output: {6, 0, 1, 0, 8, 4, 9}

Sort the array so the zeros are at the end.


// Function to left-align non-zero elements from arr_in to arr_out
function leftAlignNonZero(arr_in, arr_out, sz):
// Initialize a variable to keep track of the position in arr_out
pos = 0

// Loop through the input array


for i = 0 to sz - 1:
// If the current element in arr_in is not zero
if arr_in[i] != 0:
// Copy the non-zero element to the next available position in arr_out
arr_out[pos] = arr_in[i]
// Increment the position for the next non-zero element
pos = pos + 1

// Fill the remaining positions in arr_out with 0s


while pos < sz:
arr_out[pos] = 0
pos = pos + 1

// Main function to test the leftAlignNonZero function


function main():
// Input array
arr_in = {5, 3, 0, 0, 9, 1, 2, 0, 4}

// Size of the arrays


sz = 9

// Output array initialized with zeros


arr_out[sz] // Declared output array with size sz

// Call the function to left-align non-zero elements


leftAlignNonZero(arr_in, arr_out, sz)

// Output the result


print arr_out // Expected output: {5, 3, 9, 1, 2, 4, 0, 0, 0}
Compute Factoral of Positive integers:
// Function to compute the factorial of a number using recursion
function factorial(n):
// Base case: if n is 0 or 1, return 1
if n == 0 or n == 1:
return 1

// Recursive case: return n * factorial(n - 1)


return n * factorial(n - 1)

// Main function to test the factorial function


function main():
// Input: Read a positive integer from the user
integer n
print "Enter a positive integer:"
read n

// Call the recursive factorial function and store the result


result = factorial(n)

// Output the result


print "Factorial of ", n, " is: ", result

Compute Fibonacci series recursively.


// Function to compute the Fibonacci number at position n recursively
function fibonacci(n):
// Base case: return 0 if n is 0, and 1 if n is 1
if n == 0:
return 0
else if n == 1:
return 1

// Recursive case: return Fib(n-1) + Fib(n-2)


return fibonacci(n - 1) + fibonacci(n - 2)

// Main function to test the fibonacci function


function main():
// Input: Read an integer n from the user
integer n
print "Enter the position n for the Fibonacci sequence:"
read n
// Call the fibonacci function and store the result
result = fibonacci(n)

// Output the result


print "Fibonacci number at position ", n, " is: ", result

Given a positive integer, write a function to check if that number is prime


number.
// Function to check if a number n is prime
function isPrime(n):
// Special case: if n is less than or equal to 1, it's not prime
if n <= 1:
return false

// Loop from 2 to the square root of n (optimized check)


for i = 2 to sqrt(n):
// If n is divisible by any i, then it's not a prime number
if n % i == 0:
return false

// If no divisors were found, n is prime


return true

// Main function to test the isPrime function


function main():
// Input: Read an integer n from the user
integer n
print "Enter a positive integer:"
read n

// Call the isPrime function and store the result


result = isPrime(n)

// Output the result


if result == true:
print n, " is a prime number."
else:
print n, " is not a prime number."

Given a string, write a function to check whether the string is a palindrome.

//if prob is check to see if a word is a paladrome


for (int r = s.lenght - 1; i >=; i--)
output_str += s[r];

if (s == output_str)

Given a positive number, write a function to verify whether the number is a


perfect number.
// Function to check if a number n is a perfect number
function isPerfectNumber(n):
// If n is less than or equal to 1, it cannot be a perfect number
if n <= 1:
return false

// Initialize a variable to store the sum of divisors


sumDivisors = 0

// Loop through all possible divisors from 1 to n/2


for i = 1 to n / 2:
// If i is a divisor of n
if n % i == 0:
// Add i to the sum of divisors
sumDivisors = sumDivisors + i

// If the sum of divisors equals n, then n is a perfect number


if sumDivisors == n:
return true
else:
return false

// Main function to test the isPerfectNumber function


function main():
// Input: Read a positive integer n from the user
integer n
print "Enter a positive integer:"
read n

// Call the isPerfectNumber function and store the result


result = isPerfectNumber(n)

// Output the result


if result == true:
print n, " is a perfect number."
else:
print n, " is not a perfect number."

Given a string, return the reverse of the string.


// Function to reverse a string
function reverseString(str):
// Initialize an empty string to store the reversed string
reversedStr = ""

// Loop through the original string from the end to the beginning
for i = length of str - 1 to 0:
// Append each character to the reversed string
reversedStr = reversedStr + str[i]

// Return the reversed string


return reversedStr

// Main function to test the reverseString function


function main():
// Input: Read a string from the user
string str
print "Enter a string:"
read str
// Call the reverseString function and store the result
result = reverseString(str)

// Output the reversed string


print "Reversed string: ", result

You might also like