Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
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
Just wanted to know, if you had a scene graph, how do you get multiple components from a specific scene node within a scene graph? Like if I wanted to get a component from wheel from the scene graph, does that require traversing still?   Like if a physics component requires a transform component and these two component are part of the same scene node. How does the physics component knows how to get the scene object's transform it is attached to, this being in a scene graph?
How to develop a C program that receives the message sent by the provided program and displays the name and email included in the message on the screen?Here is the code of the program that sends the message for reference: typedef struct {    long tipo;    struct {        char nome[50];        char email[40];    } dados;} MsgStruct; int main() {    int msg_id, status;    msg_id = msgget(1000, 0600 | IPC_CREAT);    exit_on_error(msg_id, "Creation/Connection");    MsgStruct msg;    msg.tipo = 5;    strcpy(msg.dados.nome, "Pedro Silva");    strcpy(msg.dados.email, "pedro@sapo.pt");    status = msgsnd(msg_id, &msg, sizeof(msg.dados), 0);    exit_on_error(status, "Send");    printf("Message sent!\n");}
9. Let L₁=L(ab*aa), L₂=L(a*bba*). Find a regular expression for (L₁ UL2)*L2. 10. Show that the language is not regular. L= {a":n≥1} 11. Show a derivation tree for the string aabbbb with the grammar S→ABλ, A→aB, B→Sb. Give a verbal description of the language generated by this grammar.

Chapter 7 Solutions

Problem Solving with C++ (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...
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