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.
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:
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";
}
}
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?
Chapter 7 Solutions
Problem Solving with C++ (10th Edition)
Additional Engineering Textbook Solutions
Modern Database Management (12th Edition)
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Database Concepts (7th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Artificial Intelligence: A Modern Approach
Starting Out with C++: Early Objects (9th Edition)
- Write a function named swapFrontback that takes as input an array ofintegers and an integer that specifies how many entries are in the array. Thefunction should swap the first element in the array with the last elements inthe array. The function should check if the array is empty to prevent errors.The header file for the swapFrontback() is:swapFrontback.h:double swapFrontback (int [] arr,int size);Write a driver (main.c) to test your function with arrays of different lengthand with varrying front and back numbers. Print the array elements beforeand after the swap in the main.c.Sample Run: Original Array: 1 2 3 4 5 6 FrontBack Array: 6 2 3 4 5 1arrow_forwardWrite a function called modifyDistinct that takes an array of positive integers in random order and modifies all distinct integers in the array. The function should take three arguments: (1) an array of integers; (2) an integer that tells the size of the array. (3) an integer to use for replacing distinct values. The function should not return a value but should change the values of distinct integers to be the value passed in the third argument. Here is an example. Suppose the array passed to the function is as shown below, and the integer passed as array size is 11 and the integer used for replacing distinct values is -1. 1 2 3 4 5 6 7 8 9 10 Array Before: 58 | 26 | 91 | 26 | 70 | 70 | 91 | 58 | 21 | 58 | 66 Then the function should alter the array so that it looks like this: 0 1 2 3 4 5 6 7 8 9 10 Array After: 58 | 26 | 91 | 26 | 70 | 70| 91| 58 | -1 | 58 | -1 The -1 in the cells indicate that the cell contained a distinct integer that is replaced with -1 (the value of the third…arrow_forwardWrite a program that creates a two-dimensional array initialized with test data. Use any data type you wish. The program should have the following functions:a. getTotal . This function should accept a two-dimensional array as its argument and return the total of all the values in the array.b. getAverage . This function should accept a two-dimensional array as its argument and return the average of all the values in the array.c. getRowTotal . This function should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The function should return the total of the values in the specified row.d. getColumnTotal . This function should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a column in the array. The function should return the total of the values in the specified column.e. getHighestInRow .…arrow_forward
- Write a function numberOfOccurences() which takes an integer reference array of size 10 and a search value as parameters and returns the number of occurrences of the value inside the array. Test your function. Initialize an array that contains the following numbers: 4, 7, 2, 8, 8, 1, 4, 8, 9, 1. Then test it with search values 1, 8 and 6. Your program output should look like the following: [4 7 2 8 8 1 4 8 9 1 ]1 occurs 2 times in the array.8 occurs 3 times in the array.6 occurs 0 times in the array. C++ Nothing too advanced pleasearrow_forwardWrite a function called GradientCount that accepts as parameter a two-dimensional array called Matrix of size 6 by 6. The function returns how many numbers in the Matrix are gradient numbers. A number in the Matrix is considered a gradient number if it matches the following two rules: 1. The numbers to the left and above the number are smaller. 2. The numbers to the right and below the number are larger.arrow_forwardCreate a function named "onlyOdd".The function should accept a parameter named "numberArray".Use the higher order function filter() to create a new array that only containsthe odd numbers. Return the new array.// Use this array to test your function:const testingArray = [1, 2, 4, 17, 19, 20, 21];arrow_forward
- Nothing too advanced pleaseC++arrow_forwardWrite a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an array. The program then passes the array to your array expander function, and displays the values of the new expanded array, one value per line. You may assume that the file data has at least N values. There are no prompts for the integer and no labels for the expanded reversed array that is printed out. If the integer read in from standard input exceeds 50 or is less than 0 the program terminates silently.arrow_forwardWrite a program that stores first n prime numbers in a two-dimensional integer array. You should ask user to input row and column values of two-dimensional array. You should also write a function isPrimeNumber to check whether a number is a prime number or not. First n prime numbers should be found by multiplying row and column values entered by a user. Finally display the two dimensional array in a pretty form in reverse order of prime numbers. Note: You should NOT use built-in functions (except the most common ones) for finding prime numbers or displaying results.arrow_forward
- Write a program that uses a two-dimensional array to store the highest andlowest temperatures for each month of the year. The program shouldoutput the average high, average low, and the highest and lowest temperaturesfor the year. Your program must consist of the following functions:a. Function getData: This function reads and stores data in the twodimensionalarray.b. Function averageHigh: This function calculates and returns theaverage high temperature for the year.c. Function averageLow: This function calculates and returns the averagelow temperature for the year.d. Function indexHighTemp: This function returns the index of thehighest high temperature in the array.e. Function indexLowTemp: This function returns the index of thelowest low temperature in the array.arrow_forwardWrite a function that accepts an int array and the array’s size as arguments. The function should create a new array that is two elements larger than the argument array. The first two elements of the new array should be set to 0. Element 0 of the argument array should be copied to element 2 of the array, element 1 of the argument array should be copied to element 3 of the new array, and so forth. The function should return a pointer to the new array. Demonstrate the function in a complete program.arrow_forwardWrite a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program must consist of the following functions: Part a: Function getData: This function reads and stores data in the two- dimensional array. Part b: Function averageHigh: This function calculates and returns the aver- age high temperature for the year. Part c: Function averageLow: This function calculates and returns the average low temperature for the year. Part d: Function indexHighTemp: This function returns the index of the highest high temperature in the array. Part e: Function indexLowTemp: This function returns the index of the lowest low temperature in the array. (These functions must all have the appropriate parameters.) #include <iostream> using namespace std; const int NO_OF_MONTHS = 12; void getData(int twoDim[][2], int…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education