answersLogoWhite

0


Best Answer

The following function performs a non-recursive linear search of the given vector, returning a constant iterator to the element containing the given value. If the value does not exist, the "one-past-the-end" iterator is returned instead.

std::vector<int>::const_iterator find (int val, const std::vector<int>& v){

for (std::vector<int>::const_iterator it=v.begin(); it!=v.end(); ++it)

if (*it==val) return it;

return v.end();

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Consider an integer array arr with maxnum elements stored in it. num is the number to be searched.


There are two ways of searching:

1. Linear Search: If your array is not sorted, then this is the way to go. Here you will linearly scan the array(check every element) and then display the position of the element if found.


int i, flag = 0;//flag is used to check if the number is found or not

for( i=0; i

if( arr[i] 0)

printf("The element doesn't occur in the array

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

The following function performs a non-recursive linear search of the given vector, returning a constant iterator to the element containing the given value. If the value does not exist, the "one-past-the-end" iterator is returned instead.


std::vector::const_iterator find (int val, const std::vector& v){

for (std::vector::const_iterator it=v.begin(); it!=v.end(); ++it)

if (*it==val) return it;

return v.end();

}



This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a program in c plus plus to search a number in an array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

You want to write a simple without using pointer or array c program which will print greatest number when you give 20 number?

i want to write a simple without using pointer or array c program which will print greatest number when i give 20 number .........How far have you gotten so far?


How do you write the program to search for a word in an array of 5 words?

Write a loop that compares the word with every word in the array. For example, in Java, I think it would be something like this: // Declare some variables here ... for (int i = 0; i


Write a java program that tests the presence of the number 90 in an array of 70 90 80?

// set up array int[] nums = new int[] {70, 90, 80}; // iterate and search for(int i = 0; i &lt; nums.length; ++i) { if(nums[i] == 90) { System.out.println("Found 90 at index: " + i); } }


How do you write a C Program to fill up an Integer Array?

Reference:cprogramming-bd.com/c_page1.aspx# array programming


Could you Write a program for 8086 microprocessor that displays on the monitor the average of 2 numbers from an array?

How to write a program for mouse in microprocessor?


How do you write a program to read set of numbers using by an array and display the ascending order of the given input numbers?

To write a C++ program to display the student details using class and array of object.


Would you Write c plus plus program using array for Fibonacci number?

You can write a C++ fib pro using arrays but the problem is the prog becomes very complicated since u need to pass the next adding value in an array.....


How do you write an assembly language program to find the sum of n numbers using array?

write an assembly language program to find sum of N numbers


How do you write a program in C to find and display the sum of each row and column of a 2 dimensional array of type float?

array type


Write a c program to find the maximum value of 25 element in an array?

int findMax(int *array) { int max = array[0]; for(int i = 1; i &lt; array.length(); i++) { if(array[i] &gt; max) max = array[i] } return max; }


Write a c program to find the maximum of two numbers and print out with its position?

maxValue = function (array) {mxm = array[0];for (i=0; i&lt;array.length; i++) {if (array[i]&gt;mxm) {mxm = array[i];}}return mxm;}; i don't know


Write a c program to reverse an array without using another array?

public static int[] reverseArray(int[] array) { int i = 0, j = array.length - 1; for (i = 0; i &lt; array.length / 2; i++, j--) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; }