Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 7, Problem 1P

Write a function named firstLast2 that takes as input an array of integers and an integer that specifies how many entries are in the array. The function should return true if the array starts or ends with the digit 2. Otherwise it should return false. Test your function with arrays of different length and with the digit 2 at the beginning of the array, end of the array, middle of the array, and missing from the array.

Expert Solution & Answer
Check Mark
Program Plan Intro

Definition of function “firstLast2()”

Program Plan for “firstLast2()” function:

  • The function “firstLast2()” should declared before it defined in a program.
  • Define the function with its argument.
    • One argument is to get array values and another one is to define the size of the array.
    • Using “if…else” condition, check the array beginning and end value “2” or not.
      • If the condition is true, return “true” to calling function.
      • Otherwise return “false” to calling function.

Program Plan for testing code:

  • Include the appropriate headers into program.
  • Define the “firstLast2()” function.
  • Define the “main()” method.
    • Initialize the arrays with a value “2” at starting, ending, and middle position of arrays.
    • Call the “firstLast2()” function with resultant value using “if…else” condition.
    • Print the appropriate statement on screen.
Program Description Answer

Program Description:

The following C++ program to define the “firstLast2()” function with a testing method.

Explanation of Solution

Function definition:

//Function definition with bool type

bool firstLast2(int arr[],int size)

{

//Condition

if(arr[0]==2||arr[size-1]==2)

{

/*Return true to calling Function*/

return true;

}

//Else statement

else

{

/*Return false to calling Function*/

return false;

}

}

Testing code:

//Include the appropriate headers

#include <iostream>

using namespace std;

//Function definition with bool type

bool firstLast2(int arr[],int size)

{

//Condition

if(arr[0]==2||arr[size-1]==2)

{

/*Return true to calling Function*/

return true;

}

//Else statement

else

{

/*Return false to calling Function*/

return false;

}

}

//Main method

int main()

{

/*Initialization of arrays with different length*/

int a1[5]={2,5,6,5,1};

int a2[4]={5,6,1,2};

int a3[7]={6,4,5,2,5,1,1};

int a4[3]={6,5,1};

  /*Condition to check first array*/

if(firstLast2(a1, 5))

{

/*Print statement for true block*/

cout<<"The array contains a value 2  either at beginning and end of the array\n";

}

  //Else statement

else{

/*Print statement for false block*/

cout<<"The array does not contains a value 2  either at beginning and end of the array\n";

}

/*Condition to check second array*/

if(firstLast2(a2, 4))

{

/*Print statement for true block*/

cout<<"The array contains a value 2  either at beginning and end of the array\n";

}

//Else statement

else{

/*Print statement for false block*/

cout<<"The array does not contains a value 2  either at beginning and end of the array\n";

}

/*Condition to check third array*/

if(firstLast2(a3, 7))

{

/*Print statement for true block*/

cout<<"The array contains a value 2  either at beginning and end of the array\n";

}

//Else statement

else{

/*Print statement for false block*/

cout<<"The array does not contains a value 2  either at beginning and end of the array\n";

}

/*Condition to check fourth array*/

if(firstLast2(a4, 3))

{

/*Print statement for true block*/

cout<<"The array contains a value 2  either at beginning and end of the array\n";

}

//Else statement

else{

/*Print statement for false block*/

cout<<"The array does not contains a value 2  either at beginning and end of the array\n";

}

}

Sample Output

Output:

The array contains a value 2 either at beginning and end of the array

The array contains a value 2 either at beginning and end of the array

The array does not contains a value 2 either at beginning and end of the array

The array does not contains a value 2 either at beginning and end of the array

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
I need help creating the network diagram and then revising it for the modified activity times.
Activity No. Activity Time (weeks) Immediate Predecessors 1 Requirements collection 3 2 Requirements structuring 4 1 3 Process analysis 3 2 4 Data analysis 3 2 5 Logical design 50 3,4 6 Physical design 5 5 7 Implementation 6 6 c. Using the information from part b, prepare a network diagram. Identify the critical path.
Given the following Extended-BNF grammar of the basic mathematical expressions:  Show the derivation steps for the expression: ( 2 + 3 ) * 6 – 20 / ( 3 + 1 ) Draw the parsing tree of this expression. SEE IMAGE

Chapter 7 Solutions

Problem Solving with C++ (9th Edition)

Ch. 7.2 - Consider the following function definition: void...Ch. 7.2 - Prob. 12STECh. 7.2 - Write a function definition for a function called...Ch. 7.2 - Consider the following function definition: void...Ch. 7.2 - Insert const before any of the following array...Ch. 7.2 - Write a function named outOfOrder that takes as...Ch. 7.3 - Write a program that will read up to ten...Ch. 7.3 - Write a program that will read up to ten letters...Ch. 7.3 - Following is the declaration for an alternative...Ch. 7.4 - Prob. 20STECh. 7.4 - Write code that will fill the array a (declared...Ch. 7.4 - Prob. 22STECh. 7 - Write a function named firstLast2 that takes as...Ch. 7 - Write a function named countNum2s that takes as...Ch. 7 - Write a function named swapFrontBack that takes as...Ch. 7 - The following code creates a small phone book. An...Ch. 7 - There are three versions of this project. Version...Ch. 7 - Hexadecimal numerals are integers written in base...Ch. 7 - Solution to Programming Project 7.3 Write a...Ch. 7 - Prob. 4PPCh. 7 - Write a program that reads in a list of integers...Ch. 7 - Prob. 6PPCh. 7 - An array can be used to store large integers one...Ch. 7 - Write a program that will read a line of text and...Ch. 7 - Write a program to score five-card poker hands...Ch. 7 - Write a program that will allow two users to play...Ch. 7 - Write a program to assign passengers seats in an...Ch. 7 - Prob. 12PPCh. 7 - The mathematician John Horton Conway invented the...Ch. 7 - Redo (or do for the first time) Programming...Ch. 7 - Redo (or do for the first time) Programming...Ch. 7 - A common memory matching game played by young...Ch. 7 - Your swim school has two swimming instructors,...Ch. 7 - Your swim school has two swimming instructors,...Ch. 7 - Prob. 19PPCh. 7 - The Social Security Administration maintains an...
Knowledge Booster
Background pattern image
Computer Science
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
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License