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

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++
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,...
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...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The name of the file, a pointer to an array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to file, and then close the file. Write another function named fileToArray. This function should accept 3 arguments: the name of the file, a pointer, to an int array, and the size of...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as arguments, the name of a file, myFile, and the case, which will either be “upper” or “lower”. If case is equal to “upper” the function will open the file, convert all characters on each line to upper case, write each line to a new file, named “upperCase.txt”, and return the string “Converted file to upper case.” If case is equal to “lower” the function...
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input...
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input and returns the product of numbers from 1 to n.
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT