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)
- 1.Enter a positive integer value n (6 <= n < 30) and take n integers from the user as input and store them in array a. Then move the elements of this array one step to the left and make the first element of the array the last element of the array. Shift this array with a function. Display integer value n, the array elements in original order and the array elements in modified order (after shifting). For example, if the original order of the array a is like below : a: 33 45 67 89 12 7 After shifting one step to the left, you should get the following order of the array а: a: 45 67 89 12 7 33arrow_forwardWrite 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 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_forward
- 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_forwardIn statistics, the mode of a set of values is the value that occurs most often or with the greatestfrequency. Write a function that accepts as arguments the following:A. An array of integersB. An integer that indicates the number of elements in the arrayThe function should determine the mode of the array. That is, it should determine which valuein the array occurs most often. The mode is the value the function should return. If the arrayhas no mode (none of the values occur more than once), the function should return −1.Demonstrate your pointer process by using pointer notation instead of array notation in thisfunction.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 that determines standartdeviation of float array. The function takes dataset as afloat array parameter!! And it also return result as afloat.(Clue: calculate average first) Definations: in the image σ = standard deviationxi = each value of dataset? (with a bar over it) = the arithmetic mean of the data(This symbol will be indicated as average of dataset)N = the total number of data points(if you need square root of some number you can use sqrtfunction in math.h library. Usage example: a=sqrt(b);which means “a” is equal to square root of “b”)arrow_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
- Write a class with name Arrays . This class has an array which should be initialized by user.Write a method Sum that should sum even numbers in array and return sum. write a function with name numFind in this class with working logic as to find the mid number of an array. After finding this number calculate its factorial.Write function that should display sum and factorial.Don’t use divide operatorarrow_forwardWrite a function that accepts an int array and the array’s size as arguments. Thefunction should create a new array that is one element larger than the argument array.The first element of the new array should be set to 0. Element 0 of the argument arrayshould be copied to element 1 of the new array, element 1 of the argument array shouldbe copied to element 2 of the new array, and so forth. The function should return apointer to the new array.arrow_forwardWrite a function that takes 3 arguments, all integers. // It returns an array starting with the first number and increasing by the interval until it reaches // the second number, including the second number if it is part of the pattern. // For this exercise you can assume that the first integer is strictly less than the second integer. const countWithIntervals = (start, end, interval) => { } // Examples countWithIntervals(1, 10, 2) // returns [1, 3, 5, 7, 9] countWithIntervals(5, 20, 3) // returns [5, 8, 11, 14, 17, 20] // Write a function that takes 2 arguments, both strings. // It returns as a number how many times the second string occurs within the first string. const subStringCount = (string, subString) => { } // Examples subStringCount("mississippi", "i") // returns 4 subStringCount("lions and tigers and bears", "and") // returns 2arrow_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