Use R:There is a numeric vector of integers; every element appears twice except for one Implement the function findUnique() to find the element that appears only once in the numeric vector using loop. Implement the function findUnique2() to find the element that appears only once in the numeric vector without using loop. datavec <- c(3, 5, 2, 7, 7, 13, 13, 2, 8, 1, 3, 1, 5) sample output is 8
A <- function(s){
for(x in 1:length(s)){
found <- FALSE
for(j in 1:length(s)){
if(x!=j){
if(s[x]==s[j]){
found <- TRUE
break
}
}
}
if(!found){
return(s[x])
}
}
}
CODE:
findUnique <- function(s){
#take each element and compare with other and check found or
not
for(x in 1:length(s)){
#intially found is false
found <- FALSE
for(j in 1:length(s)){
if(x!=j){
if(s[x]==s[j]){
#when element occuers found will became true
found <- TRUE
break
}
}
}
#if not found that means no second occurence.
if(!found){
return(s[x])
#return the value
}
}
}
#finding element using recursion
findUnique2<- function(s,pos){
#find all occurences of element
ind=which(s[pos] == s)
#if element occur 1 time then return it
if(length(ind)==1){
return(s[pos])
}
#if element occur 2 time then shift to next element
pos=pos+1
findUnique2(s,pos)
}
#take a vector
datavec <- c(3, 5, 2, 7, 7, 13, 13, 2, 8, 1, 3, 1, 5)
#pass vector to findUnique() to find the element using loops.
print(findUnique(datavec))
#pass vector to findUnique2() to find the element without using
loops.
print(findUnique2(datavec,1))
OUTPUT:
8
8
Use R:There is a numeric vector of integers; every element appears twice except for one Implement...
please use c++ language 1. Request three different integers from the console. a) Write a swap function that swaps two integer values. b) Write a sort function which accepts three integers as input then sorts them from largest to smallest using the swap function. c) Output the integers before and after sorting. Example 1 Output (input in bold italics) Enter three different integers: 3 2 4 3 2 4 4 3 2 Example 2 Output (input in bold italics) Enter...
(Find the smallest element) use pointers to write a function that finds the smallest element in an array of integers. Use {1,2,4,5,10,100,2,-22} to test the function. using the following header. int minindex(double* list, int size) example output: array size: 8 number 1: 1 number 2: 2 number 3: 4 number 4: 5 number 5: 10 number 6: 100 number 7: 2 number 8: -22 smallest element is: -22 C++ only.
in c++
Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...
Below is a pseudocode for a function that finds the integer 5 in the 3 element integer array. The number 5 is in the array, but it is not known where it is. The Find5 function returns it’s seat number. Find5 (A[1..3]) found = false while( not found ) i = random(1, 3) if A[i] == 5 then found = true return i The function random (i, j) returns the integer of the closed range [i..j] with equal probability for...
Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an array of zeros from the main function. It will set the array to the Fibonacci sequence. This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13…). Add another function named findNum that will get the newly created array by fibo from the main function....
Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers) in an array. Note that the second largest element should be distinctly smaller than the largest element. You should also adhere to the following restrictions. (1) You should traverse through the array ONLY ONCE. (2) The array could have both positive and negative elements. So, you should NOT initialize any temporary variables to very small negative values as part of your algorithm. (3) You...
(+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100) to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0 (how many) Display the number of integers < 0 (how many) Display the...
Define a std::vector<int> variable v and add 15 random integers into that container. Use the <random> header file and the “uniform_int_distribution” to generate values between 1 and 100, inclusive. <random> is available only in C++11 and newer. If you prefer, you can use floating-point values instead of ints, in that case, look up uniform_real_distribution -- if you decide to do that, use float or double instead of int in all subsequent parts of the lab. Write a print function that...
integers between 5 and 400 Use MATLAB command windows to create a vector with all a) Ans) "deconv" Write a function integerdivision to divide two polynomials function using the command b) d) Write m-file to find the roots of the following linear equation using MATLAB x5 +2x -5x3 +7x2+12x +200 Ans) Questions 2 cover CLO2:/10 a) Write the output of the following MATLAB code >> A = [2,4,10,13:16,3,7,18; 8,4,9,25:3,12,15,171 >> length (A) >> size (A)
Create a new function file and write a function s= SUM_EVEn_ ubitname(V) that takes a vector V of arbitrary length and determines the sum of the even element and assign it to s. your function calculations must do the following: * Use a loop that iterates through every element of V and adds s only even element and skips all other elements. * Contain the appropriate H1 and help text lines. * Note: A number is even, if round(number/2) is...