- If found, set the Boolean variable to true - If not found, continue search through all the elements - If not found at search end, then assign Boolean variable to false - Print the search result based on the Boolean variable Example 1 • Write a program to create an int array with the following elements: 1 Eanple 1 /Search an array inclde estrean ineide tring using n ace std tnt natn) {2,4,6,8,10,12,14,16,18,20}. Then use cin to input a number to find. Then check if the input number is in the array. Int at11-2,4,6,8, 10, 12, 14,16, 18,20), int ber to find bool fountfalse 12 cout "Inputa nunber te searchi 4. far(int i-e; s0, d) element aals une-trues aeign true to feund re 17 20 ir (raund) fond is true et faund in the array" Jelset i fand se fale coutee ee ot found ie the array;

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
I need help to write this in c++ .please and thank you
10:26
all 33%
Read Only - You can't save changes to t...
- If found, set the Boolean variable to true
- If not found, continue search through all the elements
- If not found at search end, then assign Boolean variable to false
- Print the search result based on the Boolean variable
Еxample 1
Write a program to create an int
array with the following
1/ H Exanple 1
2 Search as array
jnclude lostrean
include catring
5 using namespace st;
elements:
7 int nain)
{2,4,6,8,10,12,14,16,18,20}.
Then use cin to input a number to
find. Then check if the input
number is in the array.
Int at101-(2,4,6,8,10, 12, 14,16, 18,20);
int x; /rumber to find
bool found-false;
cout e "Input a nunber te searchi
10
11
12
13
14-
15
16
for(int i-0; sle; de)
Ilelenent equals x
if(ali]x){
found-trues lassign true to found
break
17
18
19
20
if (found)
21
22
23. Jelsel
/ fond is true
cout ec found in the array";
i found ie falte
coutce xc" not found in the array";
Example 2
• Re-write the example 1 to use an int variable instead of a
Boolean variable found. Specifically, if the search element is
found then int found=1, else if the element is not found then int
found=0
Finding maximum of an array
Here are the steps to find the maximum element within an array
1. Set the first element to a variable called max
2. Compare the next element to max. if this element is greater than max,
then assign this element to max
3. Repeat the step 2 for all the remaining elements
4. Print the variable max.
Transcribed Image Text:10:26 all 33% Read Only - You can't save changes to t... - If found, set the Boolean variable to true - If not found, continue search through all the elements - If not found at search end, then assign Boolean variable to false - Print the search result based on the Boolean variable Еxample 1 Write a program to create an int array with the following 1/ H Exanple 1 2 Search as array jnclude lostrean include catring 5 using namespace st; elements: 7 int nain) {2,4,6,8,10,12,14,16,18,20}. Then use cin to input a number to find. Then check if the input number is in the array. Int at101-(2,4,6,8,10, 12, 14,16, 18,20); int x; /rumber to find bool found-false; cout e "Input a nunber te searchi 10 11 12 13 14- 15 16 for(int i-0; sle; de) Ilelenent equals x if(ali]x){ found-trues lassign true to found break 17 18 19 20 if (found) 21 22 23. Jelsel / fond is true cout ec found in the array"; i found ie falte coutce xc" not found in the array"; Example 2 • Re-write the example 1 to use an int variable instead of a Boolean variable found. Specifically, if the search element is found then int found=1, else if the element is not found then int found=0 Finding maximum of an array Here are the steps to find the maximum element within an array 1. Set the first element to a variable called max 2. Compare the next element to max. if this element is greater than max, then assign this element to max 3. Repeat the step 2 for all the remaining elements 4. Print the variable max.
10:26 A
3 all 33%
Read Only - You can't save changes to t..
// M8- Example 1
// Search an array
#include <iostream>
1
#include <string>
using namespace std;
6.
4.
int main()
8- {
7
int a[10]-{2,4,6,8,10,12,14,16, 18,20};
int x;
bool found-false;
cout <« "Input a number to search: ";
cin >> x;
for(int i-0; i<10; i++){
if(a[i]==x){
9.
10
//number to find
11
12
13
14 -
//element equals x
//assign true to found
15 -
found-true;
break;
16
17
18
}
19
if (found){
// found is true
cout<< x<<" found in the array";
}else{
/i found is false
cout<< x<<" not found in the array";
}
20 -
21
22
23 -
24
25
26
Transcribed Image Text:10:26 A 3 all 33% Read Only - You can't save changes to t.. // M8- Example 1 // Search an array #include <iostream> 1 #include <string> using namespace std; 6. 4. int main() 8- { 7 int a[10]-{2,4,6,8,10,12,14,16, 18,20}; int x; bool found-false; cout <« "Input a number to search: "; cin >> x; for(int i-0; i<10; i++){ if(a[i]==x){ 9. 10 //number to find 11 12 13 14 - //element equals x //assign true to found 15 - found-true; break; 16 17 18 } 19 if (found){ // found is true cout<< x<<" found in the array"; }else{ /i found is false cout<< x<<" not found in the array"; } 20 - 21 22 23 - 24 25 26
Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education