Question

Question 3. [15 marks in total Consider the following code fragment with missing statements at ????. Assume that A is a nonem

no other details are given, its asking for the invariant

programming language: Java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

A) At the start x = A[0] and it's count is 1. Invariant: Array has at least 1 element equal to A[0] in A[0... 0] is true because there exactly one element equal to A[0], itself.
B) ???? -->> x = A[i], count = 1; Let;s proof that invariant will stay true after iteration if we know that it is already true. we know that at the moment we have at least count elements equal to x. If "if condition" is held then count is incremented by one, but one element equal to x is added too so if we had numberOf(x) >= count than numberOf(x)+1 >= count+1 is true too. If else condition is held than we get count equal to one and x will be new element, it is similar to the base case explained in question A.
C) Majority item should be last element of the Array.
D) New Invariant will be: "array A has at least count items with value A[i-1] in A[0....i-1] and A[N-1] is majority item if such exists"
E) Let's say our array is {2 1 2 2 3}, here majority element is 2, but our while cycles last element will be 3, because before the last step x is equal to 2 and count is 2, at the last step x is not equal to 3, so else condition is held and x will became 3 and count will be 1. So final x will be 3 not 2.

Note: While condition is not correct, imagine array filled with entirely ones. {1, 1, 1, 1, 1, 1, 1, 1}. At the last step when i is equal to N-1, count will be 8 and after this step in while condition we have (i < N || count > N/2), i is equal to N so first part is false but count is equal to N and N > N/2, so we get (false || true) which is true and while cycle will continue without stop and we will get run time error.


Comment down for any queries

Please give a thumb up


Add a comment
Know the answer?
Add Answer to:
no other details are given, its asking for the invariant programming language: Java Question 3. [15...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...

    Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...

  • c++, we have to write functions for the code given below and other instructions for it...

    c++, we have to write functions for the code given below and other instructions for it to compile. I am having issues understanding how to confront the problem and how to write functions and read the program so it can eventually be solved so it can be compiled 7/ * INSTRUCTIONS: Write two functions in the space // * indicated below. // * // * #1 => Find index of maximum value: Write a function that will // * find...

  • Java Programming Question

    After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...

  • (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming...

    (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming for code for the new GameChanger class. // the code for Main class for step number 6 has already been given, please show modified progrraming for Main class and GameCHanger class, thank you. package .games; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); Scanner sc = new Scanner(System.in); // Get upper...

  • please help me on 12 106 Java Concepts Advanced Placement CS Study Guide 3 12. Consider...

    please help me on 12 106 Java Concepts Advanced Placement CS Study Guide 3 12. Consider the following code segment. int count = 0; for (int x = 0; x< 3; x++) for (int y = x; y < 3; y++) for (int z - y: z < 3; 2++) count++; What is the value of count after the code segment is executed? a 81 b. 27 JOHNNNN mshe thote c. 10x d. 9 e, 6 13. Each segment of...

  • Exercises (No programming is required. Your submission should be a word or pdf document.) Question 1:...

    Exercises (No programming is required. Your submission should be a word or pdf document.) Question 1: (3 Marks) Convert from binary the number (11081181), to decimal and show all steps. Question 2: (3 Marks) Convert from decimal (350)e to binary and show all steps Question 3: (3 Marks) Convert from decimal (3567)s to hexadecimal and show all steps. Question 4: (3 Marks) Convert from hexadecimal (45AC)s to octal and show all steps Question 5: (2 Marks) Given the following snippet...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • Question 1 [22 marks] (Chapt ers 2, 3, 4, 5, and 6) Let A e Rn...

    Question 1 [22 marks] (Chapt ers 2, 3, 4, 5, and 6) Let A e Rn be an (n x n) matrix and be R. Consider the problem 1 (P2) min2+ s.t. xe R" 1Ax-bil2 1 where & > O is fixed and Il IIl denot es the 2-norm. Call g.(x)=l|2 the objective function of problem (P2) 1Ax-bl2 i) [3 marks] Compute the gradient of g, and use it to show that the solution xi of this problem verifies (I+EATA)(x)...

  • Please finish all the questions,Thanks Following is Appendix assume the definition of Java class Heap given in the App...

    Please finish all the questions,Thanks Following is Appendix assume the definition of Java class Heap given in the Appendix For this question, a. (2 marks Consider the following heap 30 12 20 19 6 10 18 Given the array representation of a heap discussed in class, what is the array that corre sponds to this heap? b. (5 marks) Successively insert into the heap of part (a.) 22, 35 and 11, in that order. Use the standard heap insertion algorithm....

  • Use the C programming language to complete #include <stdio.h> #include <stdlib.h> #include <float.h> // Declare Global...

    Use the C programming language to complete #include <stdio.h> #include <stdlib.h> #include <float.h> // Declare Global variables here. void array_stats() { // Insert your solution here. } #include <stdlib.h> #include <time.h> int main() { // Simulate the test setup process. srand( time( NULL ) ); for ( int i = 0; i < 32; i++ ) { val[i] = rand(); } val_count = rand(); val_mean = rand(); val_min = rand(); val_max = rand(); // Call submitted code. array_stats(); // Display...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT