1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
zloy xaker [14]
3 years ago
12

Develop a function in c++ that can be called to sequentially search an array of integers for a given element value and return th

e index of the element if it is found in the array.​
Computers and Technology
1 answer:
wariber [46]3 years ago
6 0

Answer:

#include <iostream>

#include <ctype.h>

#include <vector>

#include <algorithm>

using namespace std;

int findValue1(int* pArray, int nrElements, int value) {

   for (int i = 0; i < nrElements; i++) {

       if (pArray[i] == value) {

           return i;

       }

   }

   return -1;

}

void test1(int* pArray, int nrElements, int value) {

   int index = findValue1(pArray, nrElements, value);

   if (index >= 0) {

       cout << value << " is found at index " << index << endl;

   }

   else {

       cout << value << " is not in the array" << endl;

   }

}

int findValue2(std::vector<int> vec, int value) {

   std::vector<int>::iterator it = std::find(vec.begin(), vec.end(), value);

   if (it != vec.end()) {

       return it - vec.begin();

   }

   return -1;

}

void test2(std::vector<int> vec, int value) {

   int index = findValue2(vec, value);

   if (index >= 0) {

       cout << value << " is found at index " << index << endl;

   }

   else {

       cout << value << " is not in the array" << endl;

   }

}

int main() {

   int arr[] = { 4, 8, 15, 16, 23, 42 };

   int nrElements = sizeof(arr) / sizeof(int);

   test1(arr, nrElements, 0);

   test1(arr, nrElements, 15);

   test1(arr, nrElements, 42);

   test1(arr, nrElements, 7);

   std::vector<int> vec(std::begin(arr), std::end(arr));

   test2(vec, 0);

   test2(vec, 15);

   test2(vec, 42);

   test2(vec, 7);

}

Explanation:

Here are two solutions. One old-school array approach, and a standard library (STL) alternative.

You might be interested in
Enlist the various data analysis methods for study of Infrasonic waves, Seismic waves, Earthquake prone areas and how AI can be
snow_tiger [21]

Answer:

Throughout the clarification segment below, the essence including its query is mentioned.

Explanation:

  • The analytical techniques utilize magnetic fields that allow the waves to be analyzed.
  • They sometimes use echolocation strategies.
  • It is possible to be using artificial intelligence to research potential forecasts. Climate, improvements throughout the climate as well as other things.
  • Like toxic waves, this will be capable of predicting tides.
5 0
3 years ago
Please an urgent answer<br>will give brainliest<br>what is the definition of bucket address.​
zmey [24]
Is this technology? Let me know so I can help you!!
3 0
3 years ago
which of the following are good ways for virtual team members to get to know one another? (choose every correct answer.)
pav-90 [236]

what are the choicess

8 0
2 years ago
Write a function that takes as input a single integer parameter n and computes the nth Fibonacci Sequence number. The Fibonacci
RSB [31]

Answer:

Following are the code to this question:

def Fibonacci(n):#defining a method Fibonacci that accept a parameter

   a = 1#defining a variable a that holds a value 1

   b = 1#defining a variable b that holds a value 1

   if n <= 0:# Use if to check n value

       print("invalid number")#print message

   elif n == 2:#defining elif that check n equal to 2

        return 1#return 1

   else:#defining else block

       print(a)#print value of a

       print(b)#print value of b

       for i in range(2,n):#defining for loop to calculate series

           s = a + b# add value in s variable

           a = b # interchange value

           b = s# hold s value

           print(s)#print s value

Fibonacci(10)#calling method

Output:

1

1

2

3

5

8

13

21

34

55

Explanation:

In the above-given program code, a method "Fibonacci" is defined, which holds a value "n" in its parameter, and inside the method two-variable "a and b" is defined, that holds a value "1".

  • In the next step, if a block is defined that checks n value is less than equal to 0, it will print "invalid number" as a message.
  • In the elif, it checks n value is equal to 2 it will return a value that is 1.
  • In the else block, it will calculate the Fibonacci series and print its value.
4 0
3 years ago
Attackers need a certain amount of information before launching their attack. One common place to find information is to go thro
VARVARA [1.3K]

Answer:

The answer is Letter D. Dumpster diving.

Explanation:

This process of going through a target's trash is known in the community as dumpster diving. This attack is a technique used to retrieve information that could be used to carry out an attack on a computer network.

It is important to inform that the dumpster diving it is not limited to searching through  the trash for obvious treasures like acess codes or passwords written down on sticky notes.

8 0
3 years ago
Other questions:
  • Which type of system must you connect to and use to make changes to Active Directory?
    15·1 answer
  • How to use translate on a website?
    15·1 answer
  • Your browsing the Internet and realize your browser is not responding which of the following will allow you to immediately exit
    14·2 answers
  • Which types of operating systems execute program independent of the users and generate output within a specific amount of time?
    5·1 answer
  • Using PowerPoint or Impressed guarantees that your presentation will do which of the following?
    6·1 answer
  • Assume that passwords are selected from four-character combinations of 26 alphabeticcharacters. Assume that an adversary is able
    11·1 answer
  • When an expression containing a ____ is part of an if statement, the assignment is illegal.
    14·2 answers
  • If you entered data into row 4 and it should be in row 3, you should _____.
    8·2 answers
  • what will happen to the contents of the destination ell if you copy the contents of the source cell into the destination cell
    7·1 answer
  • Frequently used _____________ can be saved as _____________ for use in analysis, dashboards, reports, tickets, and alerts.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!