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
Katarina is deciding whether to buy a desktop or a laptop computer. What will most likely help Katarina make her decision?
Gnesinka [82]
Laptops are portable decides , while desktops remain in one plcae
4 0
3 years ago
Read 2 more answers
The page-replacement policy means that pages are not placed to make more space. A. True B. False
BabaBlast [244]

Answer:

B. False

Explanation:

A page-replacement policy can be defined as a set of algorithm that instructs the operating systems on what memory page is to be swapped, paged out or written to disk in order to allocate more memory as they're required by various active processes during virtual memory management.

Some of the algorithms or techniques used by the operating system for page-replacement policy are;

1. Last In First Out (LIFO).

2. First In First Out (FIFO).

3. Least Recently Used (LRU).

4. Least Frequently Used (LFU).

5. Optimal (OPT or MIN).

Hence, the page-replacement policy means that pages are placed to make more space and to minimize the total number of page that would be missing.

7 0
3 years ago
Search for and list an internet websites that defines plagiarism and discusses how to avoid it
Korvikt [17]
Here are the sites that defines plagiarism and how to avoid it:
1. http://www.plagiarism.org/article/what-is-plagiarism
2. https://en.wikipedia.org/wiki/Plagiarism
3. https://writingcenter.unc.edu/tips-and-tools/plagiarism/
4. http://gethelp.library.upenn.edu/guides/engineering/ee/plagiarize.html
5. http://www.aresearchguide.com/6plagiar.html
8 0
3 years ago
Write the definition of a function named quadratic that receives three double parameters a, b, c. If the value of a is 0 then th
podryga [215]

Answer:

Following are the program in the C++ Programming Language.

#include <iostream> //header file

#include <cmath> //header file

using namespace std; //namespace

//define function

void quadratic (double a, double b, double c)

{

 double x,y; //set double type variables

 //check a==0

 if (a==0)

 { //then print message

   cout << "no solution for a=0";

 }

 //check if solution is less than 0

 else if ((b*b - 4*a*c) < 0)

 { //print message

   cout << "no real solutions";

 }

 //otherwise

 else

 {//solve the following quadratic equation

   x = (-b + sqrt( b*b - 4*a*c)) /(2*a);//in term of x

   y = (-b - sqrt(b*b - 4*a*c)) / (2*a);//in term of y

   //check x is greater than y

   if (x > y){

     cout<<x; //print the value of x

   }

   //otherwise

   else{

     cout<<y;//print the value of y

   }

 }

}

//define main method

int main() {

 //set double type variables and assign their value

 double x=10, y=50, z=-205;

 //call the function

 quadratic(x,y,z);

}

<u>Output</u>:

2.67204

Explanation:

Here, we define a function "quadratic" and pass three double data type variables i.e., "a", "b" and, "c" inside the function.

  • Set two double type variable i.e., "x", "y".
  • Set the if conditional statement and check the conditions is the variable a is equal to the 0 then, print the message or check another condition is the solution of the equation "b*b - 4*a*c" is less than 0, then print the message.
  • Otherwise, solve the quadratic equation in terms of the variable x or the variable y.
  • Again check the condition inside the else part is the variable x is greater than the variable y then, print the value of x otherwise print the value of y.

Finally, we define the main function inside it we set three double type variables i.e., "x", "y" and, "z" and initialize value 10, 50 and, -250 then, call the function and pass the variables x y z in its parameter.

6 0
4 years ago
How to understand why driving is considered a privilege?
Mnenie [13.5K]
Driving is considered a privilege because you have to be a certain age to start driving and be able to drive by yourself legally. Driving can be dangerous if you are not a safe driver and if you do illegal things while behind the wheel. Driving is also a privilege because you have to be responsible when driving, because the people that are around you or with you, their lives are in your hands.

Your answer is: You need to carry the responsibility of being safe and not harming yourself or others. 

Have an amazing day!
3 0
4 years ago
Other questions:
  • What type of data visual would you use to illustrate trends over time? Gantt chart Bar graph Line chart Scatter diagrams
    5·1 answer
  • Which of the given original work is protected by the copyright law
    14·2 answers
  • Which of the following statements is the least abstraction of the world wide web?
    6·2 answers
  • 1. Explain what is meant by the following data types:
    11·1 answer
  • Which statement describes borders and shading?
    11·2 answers
  • Assume that two parallel arrays have been declared and initialized: healthOption an array of type char that contains letter code
    13·1 answer
  • By default, the hyperlink will display the text in the hyperlink itself, such as the web URL. How can this behavior be modified?
    12·1 answer
  • A security policy is a?
    11·2 answers
  • The Fed: decreases the money supply when the economy contracts. performs banking services for commercial banks in districts wher
    7·1 answer
  • Computer science is a blank process
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!