Moondrops
Moondrops

Reputation: 11

Find and display the max and min values in the array

What is wrong with this program?

def find_min(array):
    for counter in range(0,20):
        if nums[counter] < nums[counter]:
            minnum= nums[counter]


def find_max(array):
    for counter in range(0,20):
        if nums[counter] > nums[counter]:
            maxnum= nums[counter]



#main program
nums=[""]*20

nums=[38,56,78,104,34,56,109,374,91,101,56,78,89,982,5,39,43,30,2,85]

minnumber=find_min(nums)

maxnumber=find_max(nums)

print("The smallest number in the array is " , minnumber)

print("The biggest number in the array is " , maxnumber)

Upvotes: 0

Views: 138

Answers (2)

Saimon
Saimon

Reputation: 407

First of all, your functions find_min() and find_max() are not returning anything. So,minnumber=find_min(nums) and maxnumber=find_max(nums) are assigned the value None.

Secondly, you are passing the global list variable nums to both the functions where it is assigned to the parameter array. So, there is no point in using the nums to refer to the list within the functions. Instead of writing if nums[counter] < nums[counter]:, you should write if array[counter] < array[counter]:.

Thirdly, you are hard-coding the length of the array in the range() function of the for loop. This should be avoided.

Finally, you need to read a lot on Python basics.

Since you are a beginner, I am providing you one approach to find the minimum value in a list. There are several other ways by which you can achieve the same result. Try on your own.

Code

def find_min(array):
  if len(array) == 0:
    return None
  else:
    minimum = array[0]
    for i in array:
      if i < minimum:
        minimum = i
  return minimum

print(find_min([1, 2, 0, 33, -1])) # -1

Upvotes: 0

Sheri
Sheri

Reputation: 1413

There is some problem in your logic and you have not write any return statement in both of your functions, You can try this solution as per your attemts:

def find_min(array):
    minNum = array[0]
    for counter in range(1,len(array)):
        if array[counter] < minNum:
            minNum = array[counter]
    return minNum


def find_max(array):
    maxNum = array[0]
    for counter in range(1,len(array)):
        if array[counter] > maxNum:
            maxNum = array[counter]
    return maxNum

#main program
nums=[""]*20

nums=[38,56,78,104,34,56,109,374,91,101,56,78,89,982,5,39,43,30,2,85]
minnumber=find_min(nums)
maxnumber=find_max(nums)
print("The smallest number in the array is " , minnumber)
print("The biggest number in the array is " , maxnumber)

Ouput:

The smallest number in the array is  2                                                                                
The biggest number in the array is  982

Other solution: If you want to use builtin min() or max() function it would be easy for you to find minimum and maximum number from a list:

def find_min(array):
    return min(array)

def find_max(array):
    return max(array)

Both ways gives the same output.

Upvotes: 1

Related Questions