Data Structure and Algorithm Analysis using C++ Write Pseudocode for the followi
ID: 3587001 • Letter: D
Question
Data Structure and Algorithm Analysis
using C++
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.)
PLEASE REFER BELOW CODE.HERE I TOOK ARRAY DIRECTLY GIVEN IN PROBLEM STATEMENT.IF YOU WANT USER INPUT ARRAY JUST COMMENT IN COMMENT SECTION. I WIIL CHANGE CODE.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int arr[5] = {1,2,3,4,5}; //creating an array;
int count_odd=0,count_even; //variables to count odd and even
for(int i = 0; i < 5; i++) //calculating odd numbers in list
{
if(arr[i] % 2)
count_odd++;
}
count_even = 5 - count_odd; //even numbers in list
if(count_even > count_odd) //if even count > odd count then print even else odd
cout<<"even"<<endl;
else
cout<<"odd"<<endl;
return 0;
}
PLEASE REFER BELOW OUTPUT
odd
Process returned 0 (0x0) execution time : 0.018 s
Press any key to continue.
b.)
PLEASE REFER BELOW CODE. HERE I AM TAKING PREDEFINED ARRAY TO CALCULATE sim(A,B).
IF YOU WANT TO USER INPUT ARRAY JUST PING IN COMMENT SECTION
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int A[5] = {1,2,3,4,5}; //defining array A and B
int B[5] = {1,2,4,6,5};
float sim_A_B; //variable to store sim(A,B)
int sum_prod=0,sum_sq_A=0,sum_sq_B=0; //variables to store AiBi, Ai^2 Bi^2
//taking each element from array A and B and calculating sum of AiBi, Ai^2, Bi^2
for(int i = 0; i < 5; i++)
{
sum_prod += (A[i] * B[i]);
sum_sq_A += (A[i] * A[i]);
sum_sq_B += (B[i] * B[i]);
}
//using formula given in problem sqrt() is standard function in C++
sim_A_B = sum_prod / (sqrt(sum_sq_A) * sqrt(sum_sq_B));
//printing value of sim(A,B)
cout<<"sim(A,B) = "<<sim_A_B<<endl;
return 0;
}
PLEASE REFER BELOW OUTPUT
sim(A,B) = 0.982779
Process returned 0 (0x0) execution time : 0.030 s
Press any key to continue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.