Question

Write a function that searches a numeric array for a specified value. The function should return...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
Write a function that searches a numeric array for a specified value. The function should return...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT