Write a function that takes a sorted array of integers and counts how many diffe
ID: 3651405 • Letter: W
Question
Write a function that takes a sorted array of integers and counts how many different figures arefor more than once. Example: For input f0, 1, 1, 1; 2; 4; 4; 5; 6; 6g answer is 3, because the three numbers (1.4, 6) appear twice or more.
Write a pre condition loop in your application. Remember to write using / for / after exposure. Note that this is only a request to write a pre condition loop, not to prove that it is correct. The pre condition must nevertheless be correct, ie it should be possible to prove it correct.
Explanation / Answer
Program CountSortedArray.java
===================================================
class CountSortedArray
{
public static void main(String args[])
{
int ar[]={1,1,2,3,4,4,4,5,6,7,7,8,8,8,8,9,10,10,11};
int c=countar(ar);
System.out.println("The count is: "+c);
}
public static int countar(int a[])
{
int i,c=0,j,s=0,b=0;
for(i=0;i<a.length;i++)
{
for(j=i+1;j<a.length;j++)
{
if(a[i]==a[j]){s++;b=j;}
}
if(s>=1){c++;i=b;s=0;}
}
return c;
}
}
===================================================
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.