Define a function named countOccurs that takes a list and an atom as parameters.
ID: 3813210 • Letter: D
Question
Define a function named countOccurs that takes a list and an atom as parameters. The function should return the number of times the atom occurs in the list. For example: count occurs[(6 4 2 1 0), 3] = 0 count occurs[(3 3 2 3 14), 3] = 3 count occurs[(), 3] = 0 Your solution must provide both the "design notation" used in class and an implementation of that design in Scheme. Test your code using the interpreter on stdlinux to check your work. Your solution MUST only use the elements we have discussed in class, not other Scheme constructs available on the web (you have enough to do this). Your solution MUST also use the notation and conventions we have discussed in class.Explanation / Answer
import java.util.*;
public class count_example {
static int count=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("enter elements size");
int size=sc.nextInt();
int arr[]=new int[size];
System.out.println("enter elements:");
for(int i=0;i<arr.length;i++)
arr[i]=sc.nextInt();
System.out.println("enter element to count occurance:");
int c=sc.nextInt();
count=countOccurs(arr,c);
System.out.println("total ocurance is:"+count);
}
private static int countOccurs(int[] arr, int c) {
for(int i=0;i<arr.length;i++){
if(arr[i]==c)
count++;
}
return count;
}
}
case 1:
enter elements size
5
enter elements:
6
4
2
1
0
enter element to count occurance:
3
total ocurance is:0
case 2:
enter elements size
6
enter elements:
3
3
2
3
1
4
enter element to count occurance:
3
total ocurance is:3
case 3
enter elements size
0
enter elements:
enter element to count occurance:
3
total ocurance is:0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.