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
What are the major threats of using the internet? How do you use it? How do children use it? How canwe secure it? Provide four references with your answer. Two of the refernces can be from an article and the other two from websites.
Assume that a string of name & surname is saved in S. The alphabetical characters in S can be in lowercase and/or uppercase letters. Name and surname are assumed to be separated by a space character and the string ends with a full stop "." character. Write an assembly language program that will copy the name to NAME in lowercase and the surname to SNAME in uppercase letters. Assume that name and/or surname cannot exceed 20 characters. The program should be general and work with every possible string with name & surname. However, you can consider the data segment definition given below in your program. .DATA S DB 'Mahmoud Obaid." NAME DB 20 DUP(?) SNAME DB 20 DUP(?) Hint: Uppercase characters are ordered between 'A' (41H) and 'Z' (5AH) and lowercase characters are ordered between 'a' (61H) and 'z' (7AH) in the in the ASCII Code table. For lowercase letters, bit 5 (d5) of the ASCII code is 1 where for uppercase letters it is 0. For example, Letter 'h' Binary ASCII 01101000 68H 'H'…
What did you find most interesting or surprising about the scientist Lavoiser?

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
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