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
Write a function called FindMyChar that receives three parameters: . the name of the array • the array size • a character ch The function must return the index number in which the character ch is in your array. If the character ch is not found of the array, insert ch in the last index of the array and return that index number. This index number will be printed from your main( ). For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). BIUS Paragraph Arial 14px A v ... Click Save and Submit to save and submit. Click Save All Answers to save all answers. Save All Answers Save and Submit 梦 $ 耳 a 911A S/19/20 hp 12 -> ort sc home delete nd %24 4. & 7. 6 8 num %3D backspace lock R Y. home 4. enter pause 1 shift end alt ctrl
Write a function called initializeArray that receives an array, its size, and two integers andinitializes the array with random values between the first integer and the second integer. Write anotherfunction called printArray that receives an array and its size and prints the array elements in a column. Writea test program that declares and initializes an array of size 20 with random values between 0 and 10 inclusive,and then prints the array using the above functions
Write a program that creates a 2D array of integers of size nXn initialized with test data.The program should have the following functions:● getTotal. This function should accept a 2D array as an argument and returnthe total of all the values in the array.● getAverage. This function should accept a 2D array as an argument and returnthe average of all the values in the array.● getRowTotal. This function should accept a 2D array as the first argument andan integer as its second argument. The second argument should be the subscriptof a row in the array. The function should return the total of the values in thespecified row.● getColumnTotal. This function should accept a 2D array as the first argumentand an integer as its second argument. The second argument should be thesubscript of a column in the array. The function should return the total of thevalues in the specified column.● getHighestInRow. This function should accept a 2D array as the firstargument and an integer as its second…

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

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