In: Computer Science
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL!
Program 4: Design (pseudocode) and implement (source code) a program (name it LargestOccurenceCount) that read from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the loop). The program should ignore any negative input and should continue to read user inputs until 0 is entered. The program should display the largest value and number of times it appeared as shown below in this sample runs. Document your code and properly label the input prompts and the outputs as shown below.
Sample run 1:
Enter positive integers (0 to quit): 3 4 5 -9 4 2 5 1 -5 2 5 0
Largest value: 5
Occurrences: 3 times
Sample run 2:
Enter positive integers (0 to quit): 3 7 5 -4 4 2 -5 5 1 7 0
Largest value: 7
Occurrences: 2 times
Sample run 3:
Enter positive integers (0 to quit): 2 9 8 -4 8 9 -5 8 9 1 7 7 9 0
Largest value: 9
Occurrences: 4 times
Implemented the code as per the requirement. As python is tab
specific, you may not get the formatted text while copying the
code,
so I'm attaching the screenshots of the code for reference. Please
make sure when you are executing the below code you have same
format, especially tabs.
Please comment if any modification required.
Code:
====
nums_array = input("Enter positive integers (0 to quit):
").split()
maxim = int(nums_array[0])
i=0
for i in range(1, len(nums_array)):
num = int(nums_array[i])
if(num==0):
break
else:
if(maxim<num):
maxim = num
freq = 0
for j in range(i):
num = int(nums_array[j])
if(num==maxim):
freq = freq+1
print("Largest value: ", maxim)
print("Occurrences: ",freq, "times")
pseudocode:
===========
take input for integers as a string and split them to store as an array
initialize a variable maxim to store first value of the integers as mximum
initialize a variable i to count how many integers we have got before user entered zero
for loop from zero till length of the integer array :
initialize num variable to store each number in string format as an integer
if num is equal to zero
stop the loop
else
if maxim is less than num
maxim is to be replaced with num
initialize variable freq to zero
for loop from zero to i
initialize num variable to store each number in string format as an integer
if num is equal to maxim
increment freq by 1
print maxim
print frequecy