Question
Kindly note that I have asked this question before but the answer was not very logical. Therefore, I had to post it again here: The
Kindly note that I have asked this question before but the answer was not very logical. Therefore, I had to post it again here: The solution must only be provided in pseudocode.
Write a program whose input is an array A of positive numbers and a positive number x. The program should print the smallest element of A that is greater than x. If such an element does not exist, the program should print 1. For example, if A = [1, 3, 5, 3, 6, 5] and x = 3 then the program should print 5. You may not use recursion for this particular task.
I have tried to solve this as follows:
FindNum(A[ ], x)
{
result = A[0]; // kindly note i can not initialise this with a random number therefore I have done so by A[0]
for (i = 0; i < len(A[ ]); i++) {
if ( A[i] > x )
result - A[i]; //this finds a number larger than x but does not satisfy the requirement of finding the smallest number larger than x
}
print (resutl);
}
I believe there should be another variable containing the current value of result and comparing it with the new value and keeping the smaller of two? but I am not sure how to implement it.
Kindly advice.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started