Enter 1000 numbers: 3 4 2 6 2 8 7 2 ... Enter target: 2 Target occurs in the list at positions: 3, 5, 8, ... Total 23 times.or
Enter 1000 numbers: 3 4 2 6 2 8 7 2 ... Enter target: 5 Target occurs in the list at positions: Sorry, this number is not in the list.Extra points: How would you change the algorithm so that it only prints "Target occurs in the list at positions: " only in the case that target occurs in the list at least once.
Algorithm FindLargest: Variables: list a of size 100, i, largest, location print "Enter 100 values:" i = 1 while (i <= 100) get ai i = i+1 set largest = a1 set location = 1 set i = 2 while (i <= 100) if (largest < ai) then set largest = ai set location = i set i to i+1 print "The largest value is " largest "at location " locationIf the numbers in our list were not unique and therefore the largest could occur more than once, would the algorithm find the first occurence? The last occurence? Every occurence? Explain.
while (i <= 100) if (largest < ai) ...In each of the following cases, explain exactly what would happen if this instruction were changed to:
if (largest < ai) then ...In each of the following cases, explain exactly what would happen if this instruction were changed to:
BinarySearch Variables: list a of size 100, target, start, end, middle, found, i, n i = 1 n = 100 print "Enter " n " numbers: " while (i <= 100) get ai i = i + 1 print "Enter target:" get target start = 1 end = n found = 0 while (... ?? ...) //compute the middle between start and end middle = ceiling( (start + end)/2) if (target == amiddle) then print "Target found at position " middle found = 1 if (target < amiddle) then end = middle - 1 if (target > amiddle) then start = middle + 1 if (found == 0) then print "Target not in the list"
In the examples above, assume the algorithm is modified so that the size of the list (which in the algorithm above is 100), is replaced with the size of the actual list in that example (5 for (b), and 11 for (c)).
(a) Your assignment is to write the algorithm that counts the number of votes and decides the president. To do this, you will have to read a list of votes, one by one, from the voting machine. Each vote will represent the selection of one person voting in the booth. A vote can be one of two names, "Tom" and "Jerry" (no connection with reality, this is just an exercise); any other name is considered invalid, and discarded. Assume that when the vote day is over and the last person has voted, an appointed officer comes in and sends the vote "alea iacta est".
Thus your program should read in votes from the machine until reading the vote "alea iacta est", which marks the end. For the sake of this problem assume that Dumbell has forbidden you from counterfeiting votes. Write an algorithm that displays the following information, in this order:
Enter 20 letters: Hello world......... Thanks. The inverted text is: .........dlrow olleH Goodbye.
....T A G C C A G T A A C T A A G C T...Write an algorithm that reads two DNAs from the user (hmm...for the sake of this problem we'll assume a poor soul types in 3.5 billion letters, twice), and finds out whether they are the same or not. In this problem, assume that two DNAs are the same if they match leter by letter, either forwards or backwards. For example:
A C A A G T C and A C A A G T Cmatch. Also
A C A A G T C and C T G A A C Amatch.
Your algorithm should print one of the folowing:
DNA1 matches with DNA2 forward. DNA1 matches with DNA2 backwards. DNA1 does not match with DNA2.
In the example below let's assume the length of a DNA is 6 and the number of allowed mismatches is 3.
G T G G C A and A T A G C Gmatch forward, with 3 mismatches (first, third and last position).
G T G A C A and A C A G T CIf we try to align them forward, we find 6 mismatches, therefore they do not match forwards. However, when we try to match them backwards, we see that they in fact match with 1 mismatch.
Your algorithm should print one of the folowing:
DNA1 matches with DNA2 forward with xx mismatches. DNA1 matches with DNA2 backwards with xx mismatches. DNA1 does not match with DNA2.