Question

In: Computer Science

Write a function named hasNValues which takes an array and an integer n as arguments. It...

Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values.

If you are writing in Java or C#, the function signature is
int hasNValues(int[ ] a, int n)

If you are writing in C or C++, the function signature is
int hasNValues(int a[ ], int n, int len) where len is the length of a

Note that an array argument is passed by reference so that any change you make to the array in your function will be visible when the function returns. Therefore, you must not modify the array in your function! In other words, your function should have no side effects.

Solutions

Expert Solution

Code

Java


import java.util.Arrays;


public class Main {

public static void main(String[] args)
{
int a[]={1, 2, 2, 1};
int b[]={1, 1, 1, 8, 1, 1, 1, 3, 3};
int c[]={1, 1, 1, 8, 1, 1, 1, 3, 3};
System.out.println(Arrays.toString(a)+" has 2 distinc values: "+hasNValues(a,2));
System.out.println(Arrays.toString(b)+" has 3 distinc values: "+hasNValues(b,3));
System.out.println(Arrays.toString(c)+" has 2 distinc values: "+hasNValues(c,2));
}
public static int hasNValues(int[] a, int n)
{
int hasNValues = 1;
int[] uniqueValues = new int[n];
int uniqueindex = 0;
for (int i = 0; i < a.length && hasNValues == 1; i++)
{
if (isExistInArray(uniqueValues, a[i]) == -1)
{
if (uniqueindex < n)
{
uniqueValues[uniqueindex] = a[i];
uniqueindex++;
}
else
{
hasNValues = 0;
}
}
}

if (uniqueindex != n) hasNValues = 0;

return hasNValues;
}
//helper function to find if numer io in array or not
public static int isExistInArray(int a[],int num)
{
for(int i=0;i<a.length;i++)
if(a[i]==num)
return 1;
return -1;
}
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

2. Define a function max_n(arr, n) that takes in an array and an integer as arguments....
2. Define a function max_n(arr, n) that takes in an array and an integer as arguments. Your function will then return the n largest values from that array as an array containing n elements. It is safe to assume that arr will have at least n elements. The resulting array should have the largest number on the end and the smallest number at the beginning. For Example: max_n(np.array([1,2,3,4,5]), 3) returns np.array([3,4,5]) max_n(np.array([10,9,8,7,6,5]), 4) returns np.array([7,8,9,10]) max_n(np.array([1,1,1]), 2) returns np.array([1,1])
Write a boolean function named isMember that takes two arguments: an array of type char and...
Write a boolean function named isMember that takes two arguments: an array of type char and a value. It should return true if the value is found in the array, or false if the value is not found in the array. PLEASE WRITE FULL PROGRAM IN C++ AND USE RECURSION AND DO NOT USE LOOPS
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
Function named FunCount takes three arguments- C, an integer representing the count of elements in input...
Function named FunCount takes three arguments- C, an integer representing the count of elements in input list IP- input list of positive integers. Item- an integer value. Function FunCount returns an integer representing the count of all the elements of List that are equal to the given integer value Key. Example: Don’t take these values in program, take all inputs from user C = 9, IP= [1,1,4,2,2,3,4,1,2], Item = 2 function will return 3 IN C PROGRAMMING
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
a splitting function, split_by Write a splitting function named split_by that takes three arguments an equality...
a splitting function, split_by Write a splitting function named split_by that takes three arguments an equality checking function that takes two values and returns a value of type bool, a list of values that are to be separated, and a list of separators values. This function will split the second list into a list of lists. If the checking function indicates that an element of the first list (the second argument) is an element of the second list (the third...
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the...
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the two arrays contain the same values (but not necessarily in the same order), otherwise it returns 0. Your solution must not sort either array or a copy of either array! Also you must not modify either array, i.e., the values in the arrays upon return from the function must be the same as when the function was called. Note that the arrays do not...
(In python) Write a function calc_pizza_charge that takes four integer arguments, one for the size (1...
(In python) Write a function calc_pizza_charge that takes four integer arguments, one for the size (1 small, 2 medium, 3 large), one for the number of meat toppings, one for the number of other toppings and another for the quantity of pizzas ordered. It should calculate and return the total due for that pizza order based on the following information: Small pizza base price: $6.50 Medium pizza base price: $9.50 Large pizza base price: $11.50 The base pizza price includes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT