Problem Solving with C++ Plus MyLab Programming with Pearson eText -- Access Card Package (10th Edition)
Problem Solving with C++ Plus MyLab Programming with Pearson eText -- Access Card Package (10th Edition)
10th Edition
ISBN: 9780134710747
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
Write a function called remove_punct() that accepts an array of characters and the number of items in the array as parameters, removes the punctuation (',', '!', '.') characters from the array, and returns the number of punctuation characters removed. For example, if the array contains ['C', 'p', 't', 'S', ',', '1', '2', '1', '.', 'i', 's', 'f', 'u', 'n', '!'], then the function should remove the punctuation characters. The function must remove the characters by shifting all characters to the right of each punctuation character, left by one spot in the array. This will overwrite the punctuation characters, resulting in: ['C', 'p', 't', 'S', '1', '2', '1', 'i', 's', 'f', 'u', 'n']. In this case, the function returns 3. Note: if the array does not contain any punctuation characters, then the array is unchanged and the function returns 0.
Write a function that takes an array of ints, and the size of the array – another int.  It also returns a double.  Call this one ‘average.’  Return a double that is the average of the values in the array.  Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92}
Write a function that returns an integer that appears most often with respect to an array of integers   In the array. for example. [1 2 3 2 3 4 2 5] For an array your function must return 2.

Chapter 7 Solutions

Problem Solving with C++ Plus MyLab Programming with Pearson eText -- Access Card Package (10th 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...

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License