How to convert c++ Pseodocode into a program for searching the value of an array.

What is the program of this pseodocode?

1
2
3
4
5
6
7
8
9
10
11
Set found to false.
Set position to -1.
Set index to 0.
While found is false and index < number of elements
     If list[index] is equal to search value
          found = true.
          position = index.
     End If
     Add 1 to index.
End While.
Return position
Last edited on
Hi @judo11,
what (code) do you have so far?
#include <iostream>
using namespace std;
int main()
{
const int arraySize = 5;
int target;
// Array size and values already known.
int array[arraySize] = {1, 2, 3, 4, 5};
int first, mid, last;
cout << "Enter a target to be found: ";
cin >> target;

first = 0;
last = 2;
Last edited on
I think @eyenrique meant what you have so far from the pseudocode
I think my code from that pseudocode is incorrect I want correct it please give the code for that. It's very difficult for me to solve that problem
Last edited on
Well this is a "first attempt" you
could complete the rest.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//search_program.cpp
/*
Set found to false.
Set position to -1.
Set index to 0.
While found is false and index < number of elements
     If list[index] is equal to search value
          found = true.
          position = index.
     End If
     Add 1 to index.
End While.
Return position
*/

#include <iostream>

using namespace std;

int find(int search_value,int list[],int const NUMBER_OF_ELEMENTS);

int main(){

	int const NUMBER_OF_ELEMENTS=5;
	int list[NUMBER_OF_ELEMENTS]={1,2,3,4,5};

	int key=3;

	cout<<key<<" is in position: "<<find(key,list,NUMBER_OF_ELEMENTS)<<endl;

return 0; //indicates success
}//@end of main

int find(int search_value,int list[],int const NUMBER_OF_ELEMENTS){
	bool found=false; //Set found to false.
	int position=-1; //Set position to -1
	int index=0; //Set index to 0
	
	while(found==false&&index<NUMBER_OF_ELEMENTS) //While found is false and index < number of elements
	{
		//If list[index] is equal to search value
               // found = true.
              //position = index.
             // End If
            //Add 1 to index.
	}//end while
//????
}//end function find 
Topic archived. No new replies allowed.