Write a function that searches a numeric array for a specified value. The function should return the subscript of the element containing the value if it is found in the array. If the value is not found, the function should throw an exception.
Hi,
here is the solution,Language is not specified So C++ and Java examples are used
Here is the C++ program to search the array:
#include<iostream>
using namespace std;
int arraySeach(double* array,double element,int size){
for (int i = 0; i < size; i++){
if (element == array[i]){
return i;
}
}
throw "Array element not fount";
}
int main(){
double arr[] = { 2, 5, 8, 6, 12, 15 };
cout << "Plaese enter the element to search :
";
double ele;
cin >> ele;
try{
cout << "Element " <<
ele << " is at " << arraySeach(arr, ele, 6) << "
in the array";
}
catch (const char* ex){
cout << "An exception has
occured" << endl<<ex;
}
return 0;
}
Output:
test 1:
Plaese enter the element to search : 8
Element 8 is at 2 in the array
test 2 :
Plaese enter the element to search : 6
Element 6 is at 3 in the array
test 3:
Plaese enter the element to search : 11
An exception has occured
Array element not fount
here is the java program:
Running instructions:
1. In any IDE like Eclipse,NetBeans etc. create class file
called ArraySearch.java copy and paste the below code and press
run
2.In Linux open Terminal $gedit ArraySearch.java
copy and paste the below code save and then exit.
$javac ArraySearch.java
$java ArraySearch
class ArrayElementNotFountException extends Exception{
public ArrayElementNotFountException(String s){
super(s);
}
}
public class ArraySearch {
public int arraySearch(double[] array,double element)
throws ArrayElementNotFountException{
int index=-1;
for(int
i=0;i<array.length;i++){
if(array[i]==element){
index=i;
break;
}
}
if(index==-1){
throw new
ArrayElementNotFountException("element is not found in the
array");
}else{
return
index;
}
}
public static void main(String[] args) {
// TODO Auto-generated method
stub
double[]
arr={1,8,10,15,22,6};
java.util.Scanner s=new
java.util.Scanner(System.in);
double ele;
System.out.println("Please enter
the element to search in the array");
ele=s.nextDouble();
ArraySearch as=new
ArraySearch();
try {
System.out.print("Element is found at : ");
System.out.print(as.arraySearch(arr, ele));
} catch
(ArrayElementNotFountException e) {
// TODO
Auto-generated catch block
System.out.println(e.getMessage());
}
}
}
output:
run 1:
Please enter the element to search in the array
8
Element is found at : 1
run 2:
Please enter the element to search in the array
28
Element is found at : element is not found in the array
Write a function that searches a numeric array for a specified value. The function should return...
C++ Chapter 16 Problem: Implement #25 as a template function (Demonstrate using int, double, string, and x,y pair object) 24. Write a function that searches a numeric array for a specified value. The function should return the subscript of the element containing the value if it is found in the array. If the value is not found, the function should throw an exception. 25. Write a function that dynamically allocates a block of memory and returns a char pointer to...
Write a function searchList that recursively searches a linked list for a specified value. The function should return a pointer to the value if it’s found; otherwise, NULL should be returned. Use your function in a test program that creates a list of integers. The program should prompt the user for a value to locate in the list. It's a C program.
Write a program that deletes an element of a one-dimensional array of characters. The program should: Ask user to input the number of characters in the array the values of the array a character to be deleted Call a method to delete the character Print the resulting array or, if the character is not found, print “Value not found” The method called by the main program should: Pass the array and the character to be found as parameters If the...
Create a Raptor Flowchart and Pseudocode for the following: That searches an array of size 15 that holds a collection of numbers, for the value 7. First we take input from the user to fill each element in the array. Then we search the array for value 7. If value 7 is not found we output: "Value 7 is not contained in array[]", otherwise if the value 7 is found output: "Value 7 is contained in array[] at index" and...
Write a function to return a string containing the actual binary value of the mantissa of a float in a char array. You should call get_flt_bits_int to get the bits in an int and return the string. Example: for f = -15.375 n = 11000001011101100000000000000000 the mantissa bits are "11101100000000000000000" The function should accept a float and return a string.
write the C program that Searches a number in an array, if the number was found the program checks if the number is the largest number or not
Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...
Write a program that creates an array of 10 integers. The array should be populated with values: each element should be equal to its subscript/index. The program should then print each element. Make use of a loop in storing values to the array. Make use of another loop in printing the values of the array.
(C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....
Write a function named insertBeforeKey that takes the parameters as follows list as an char array, capacity for the capacity of the array, numItems has the number of items in the array, key is an element of the array before which newVal is to inserted. Assume key is always present in the array. and newVal has the new value to be inserted into the array. If the array is at capacity then the function should return -1, otherwise return 0...