Question

Write an algorithm to search for and count the number of times a value is found...

Write an algorithm to search for and count the number of times a value is found in a list L of n numbers. Each item in the list can be accessed by L[i] where i is the position of the item in the list. In other words, L[O] is the first item, L[1] is the second item, and so on. The last item can be accessed by! L[n-1]. The user enters the value to search for and count, then your algorithm should print the number of occurrences of the value in the list. If it is not there, it will print "The value was not found".

Homework Answers

Answer #1

Aflgorithm:

-->l=list of n numbers;
-->x=input(); /*number to search*/
-->count=0;
-->for i=0 to n:
   if l[i]==x:
       count=count+1
-->if count==0:
   display "the value was not found"
else:
    disply count

--------------------------------------------------------------------

python code:

l=[1,2,3,2,4,3,5,2,3,4]
x=int(input("enter the number to search the no.of occurence: "))
c=0
for i in range(len(l)):
   if(l[i]==x):
       c=c+1
if(c==0):
   print("the value is not found")
else:
   print("No.of occurence is ",c)

------------------------------------------------

input:3

Output:3

Hope you understand.If you have any doubts comment me.Upvote me.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The binary search algorithm given in this chapter is nonrecursive. Write and implement a recursive version...
The binary search algorithm given in this chapter is nonrecursive. Write and implement a recursive version of the binary search algorithm. The program should prompt Y / y to continue searching and N / n to discontinue. If an item is found in the list display the following message: x found at position y else: x is not in the list Use the following list of integers when testing your program: 2, 6, 8, 13, 25, 33, 39, 42, 53,...
In this programming exercise you will create an algorithm for solving the following version of the...
In this programming exercise you will create an algorithm for solving the following version of the m Smallest Numbers problem.   Instead of just returning the m smallest values as in homework 1, you want to return a list of the positions where the m smallest values are located without changing the original array. Your algorithm should meet the following specifications: mSmallest( L[1..n], m ) Pre: L is a list of distinct integer values. n is the number of elements in...
3) Write an algorithm to take a list of numbers from user and determine the numbers...
3) Write an algorithm to take a list of numbers from user and determine the numbers divisible by the number 7. You need to first prompt the user that how many numbers s/he wants to input and then take the numbers. After taking the numbers, you check which numbers are divisible by 7 from the list. You need to print the number along with the position of the number. Finally, you need to print how many numbers from the list...
1.Draw a flowchart to read 3 numbers: M, K and N and print the largest number...
1.Draw a flowchart to read 3 numbers: M, K and N and print the largest number of them. 2. Devolve an algorithm that allows the user to enter the count of number in a list followed by these numbers. The algorithm should find and output the minimum and maximum numbers in the list. 3. Develop an algorithm that inputs a series of number and output their average.
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
What is the complexity of this algorithm? Assign each student in the class a number from...
What is the complexity of this algorithm? Assign each student in the class a number from 1 to n, where n is the number of students. Then ask each of the odd-numbered students whether he or she is left-handed. a. O(1) b. O(n) c. O(n ^ 2) d. O(log n) What is the complexity of this algorithm? In a very difficult CS class, half the n students who originally signed up drop the course after the first quiz. After each...
A speedy Decrease-and-Conquer search. Use your newly acquired knowledge of “Decrease-and-Conquer” algorithm design strategy to design...
A speedy Decrease-and-Conquer search. Use your newly acquired knowledge of “Decrease-and-Conquer” algorithm design strategy to design a O( n ) algorithm to search for a given number in an n × n matrix in which every row and every column in this matrix is sorted in increasing order Write your algorithm as an elegant java method named findElement(int [][] arr, int element) that returns the index of that element in the array as a small int array made of 2...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the code to be written with these) I need Class river, Class CTRiver and Class Driver with comments so I can learn and better understand the code I also need a UML Diagram HELP Please! Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong()...
This is my code, python. I have to search through the roster list to find a...
This is my code, python. I have to search through the roster list to find a player using their number. it says list index out of range. it also says there is error in my main. def file_to_dictionary(rosterFile): myDictionary={}       with open(rosterFile,'r') as f: data=f.read().split('\n')       for line in data:    (num,first,last,position)=line.split() myDict=[first, last, position] myDictionary[num]=myDict print (myDictionary) return myDictionary file_to_dictionary((f"../data/playerRoster.txt"))    def find_by_number(number): player=None    second=[] foundplayer= False myDictionary=file_to_dictionary((f"../data/playerRoster.txt")) for p in myDictionary: fullplayer=p.split() second.append([fullplayer[0], (fullplayer[1]+" "+...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT