6) The two code snippets below are supposed to check if a given array contains the integer 1 (a means a list of variables of the same type; don't worry about its syntax, we did not study arn and you can understand the code by reading the comments). Which of the two solutions has the correct logic? Justify your answer. Solution 1:

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
C++
6) The two code snippets below are supposed to check if a given array contains the integer 1 (an array
means a list of variables of the same type; don't worry about its syntax, we did not study arrays yet
and you can understand the code by reading the comments).
Which of the two solutions has the correct logic? Justify your answer.
Solution 1:
//array (i.e. list) of 10 int variables
int intList [U - (2,3, 20, 1,10,-1,5,8,-2,11):
6.
8
int n - 10; //n represents the size of the list
10
11
bool contains one - true:
12
13
14
15
16
17
18
19
20
for (int i-0: i<n;i++){
int x - intList[1]; //the ith element in the list
if (x!-1)
contains one - false;
break
if (contains_one)
cout<<"The list contains 1"<<endl:
21
22
23
else
24
cout<<"The list does not contain 1"<<endl;
Solution 2:
6 ||//array (1.e. list) of 10 int variables
int intList [] - (2,3,20,1,10,-1,5,8,-2,11};
int n - 10; //n represents the size of the list
10
11
bool contains one = false:
12
13
14
15
16
for (int i-0, i<n;i++) {
int x - intList [i] //the ith element in the list
if (x--1) {
contains_one - true;
break:
17
18
19
20
21
22
23
if(contains_one)
cout<<"The list contains 1"<<endl;
else
24
cout<<"The list does not contain 1"<<endl;
Transcribed Image Text:6) The two code snippets below are supposed to check if a given array contains the integer 1 (an array means a list of variables of the same type; don't worry about its syntax, we did not study arrays yet and you can understand the code by reading the comments). Which of the two solutions has the correct logic? Justify your answer. Solution 1: //array (i.e. list) of 10 int variables int intList [U - (2,3, 20, 1,10,-1,5,8,-2,11): 6. 8 int n - 10; //n represents the size of the list 10 11 bool contains one - true: 12 13 14 15 16 17 18 19 20 for (int i-0: i<n;i++){ int x - intList[1]; //the ith element in the list if (x!-1) contains one - false; break if (contains_one) cout<<"The list contains 1"<<endl: 21 22 23 else 24 cout<<"The list does not contain 1"<<endl; Solution 2: 6 ||//array (1.e. list) of 10 int variables int intList [] - (2,3,20,1,10,-1,5,8,-2,11}; int n - 10; //n represents the size of the list 10 11 bool contains one = false: 12 13 14 15 16 for (int i-0, i<n;i++) { int x - intList [i] //the ith element in the list if (x--1) { contains_one - true; break: 17 18 19 20 21 22 23 if(contains_one) cout<<"The list contains 1"<<endl; else 24 cout<<"The list does not contain 1"<<endl;
Expert Solution
steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Function Arguments
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.
Similar questions
  • SEE MORE QUESTIONS
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