Write Pseudocode for the following a. Write pseudocode to determine if there are
ID: 3586907 • Letter: W
Question
Write Pseudocode for the following a. Write pseudocode to determine if there are more odd or even numbers in a list of integers It should take a list of integer numbers as input and return either "odd" or "even". For example, if given the list [1,2,3,4,5] as input your pseudocode should return "odd" b. Cosine similarity (cos sim) is a commonly used similarity metric that measures hovw similar two vectors (arrays) of numbers are. The equation for cosine similarity is given by A B sim(A, B) where A and B, are components of vector A and B respectively. Write pseudocode o calculate cosine similarity of two vectors A and B. it should take two arrays of numbers, A and B, as arguments and return a single value, their cosine similarity.Explanation / Answer
a.
Pseudocode in Java
public static String determineOddOrEven(int arr[]){
int countOfOdd=0,coountOfEven=0;
for(int i=0;i<arr.length;i++){
if(arr[i] % 2 == 0){
coountOfEven++;
}else{
countOfOdd++;
}
}
if(coountOfEven > countOfOdd){
return "even";
}else{
return "odd";
}
//in this if both are equal we are returning "Odd" as output
}
b.
Pseudocode in Java for cosineSimilarity
public static double cosineSimilarity(int arr1[],int arr2[]){
int result1=0,result2=0,sum1=0,sum2=0;
for(int i=0;i<arr1.length;i++){
result1+=arr1[i]*arr2[i];
sum1+=arr1[i]*arr1[i];
sum2+=arr2[i]*arr2[i];
}
result2=(int)(Math.sqrt(sum1)*Math.sqrt(sum2));
// System.out.println(result1);
// System.out.println(result2);
return result1*1.0/result2 ; //to convert the result into double value
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.