Write Pseudocode for the following a. Write pseudocode to determine if there are
ID: 3848964 • 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 how similar two vectors (arrays) of numbers are. The equation for cosine similarity is given by sim(A, B) = A middot B/||A|| middot ||B|| = Sigma^n_i=1 A_i B_i/2 Squareroot Sigma^n_i = 1 A^2_i^2 Squareroot Sigma^n_i=1 B^2_i where A_i and B_i are components of vector A and B respectively. Write pseudocode to 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
1a)
import java.util.*;
public class Odd_even {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]=new int[]{1,2,3,4,5};
String result=check_odd_even(arr);
System.out.println("list contains more "+result+" numbers");
}
public static String check_odd_even(int arr[]){
int oddcount=0;
int evencount=0;
String finalr="";
for(int i=0;i<arr.length;i++){
if(arr[i]%2==0){
evencount++;
}else
oddcount++;
}
//System.out.println(evencount+" "+oddcount);
if(evencount>oddcount)
finalr="even";
else if(evencount==oddcount)
finalr="equal";
else
finalr="odd";
return finalr;
}
}
output:
list contains more odd numbers
//list is passed as argument to method and it runs for loop to check even or odd
if it is even il increment even counter or else odd counter and if both are equal than return equal .if even counter is greater returns even else odd
2 program
import java.util.*;
public class Cosine_simillarty {
public static void main(String[] args) {
// TODO Auto-generated method stub
int vector1[]={2,3};
int vector2[]={3,1};
double similarty=cosime_sim(vector1,vector2);
System.out.println("similarity="+similarty);
}
private static double cosime_sim(int[] vector1, int[] vector2) {
// TODO Auto-generated method stub
int da=1;
int db=1;
int dp=0;
int ma=0;
int mb=0;
for(int i=0;i<vector1.length;i++){
da*=vector1[i];
}
for(int i=0;i<vector2.length;i++){
db*=vector2[i];
}
dp=da+db;
for(int j=0;j<vector1.length;j++){
ma+= Math.pow(vector1[j], 2);
}
double sqA=Math.sqrt(ma);
// System.out.println(sqA);
for(int j=0;j<vector2.length;j++){
mb+= Math.pow(vector2[j], 2);
}
double sqB=Math.sqrt(mb);
// System.out.println(sqB);
double sim=9/(sqA*sqB);
return sim;
}
}
output:
similarity=0.7893522173763263
//here first we take input array and pass to method
here dot product of both the vectors are find
and maginitude of both vectors
finally similarity of both vectors
formula 9/(srt(a)*sqrt(b));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.