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++, Student Value 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 that takes as input an array of (type float) 5 numbers and prints the positionand the value of the smallest number.For example, for an array A = [-2.3, 0.8, 1.7, 1.1, -5.2], the function should print: The minimumvalue of the array is -5.2 and is stored at position 5.Write a program that asks the user to enter 5 numbers to populate the array, and then calls thefunction described above.arrow_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 is ordered that takes an integer array and its size as parameters and returns true if the array elements are ordered in nondecreasing order and returns false if not. For example, integers in {10,0,1,1,5,1009} are ordered in nondecreasing order. On the other hand, integers in {1, 2, 3, 8, 4, 19} are not since 8 > 4arrow_forward
- Write a modular program that analyzes a year’s worth of rainfall data. In addition to main, the program should have a getData function that accepts the total rainfall for each of 12 months from the user and stores it in an array holding double numbers. It should also have four value-returning functions that compute and return to main the totalRainfall, averageRainfall, driestMonth, and wettestMonth. These last two functions return the number of the month with the lowest and highest rainfall amounts, not the amount of rain that fell those months. Notice that this month number can be used to obtain the amount of rain that fell those months. This information should be used either by main or by a displayReport function called by main to print a summary rainfall report similar to the following: 2019 Rain Report for Springdale County Total rainfall: 23.19 inches Average monthly rainfall: 1.93 inches The least rain fell in January with 0.24 inches. The most rain fell in April with 4.29…arrow_forwardWrite a Program for insertion and deletion from an array without replacement of element from the arrays. Also, write a program to search the array for the presence of a given element from an array. Write the sub-functions in the provided test case in text form and upload the same. #include<stdio.h> #include<stdlib.h> int cmpfunc (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int insert_function(int *data, int n, int p, int x) { /* input data: array, n: size of array, p: position of insertion, x: element to insert*/ /*output:-1 indication error or 0 if successful /* write the function for inserting element into a position*/ } int delete_function(int *data, int n, int p, int x) { /* input data: array, n: size of array, p: position of deletion, x: element to deletion*/ /*output:-1 indication error or 0 if successful /* write the function for delete element into a position*/ } int linear_search_function(int *data, int n, int x) { /*write the function to…arrow_forwardWrite a function named(difference), that accepts an integer one-dimensional array and its size, returns the difference between the largest element and the smallest element. e.g. the array is [1,7,13,22,15,6,4,3,2,9,21] the output is:21arrow_forward
- Write a function named "reverse" that takes as its arguments the following: (1) an array of floating point values; (2) an integer that tells how many floating point values are in the array. The function must reverse the order of the values in the array. Thus, for example, if the array that's passed to the function looks like this: 0 1 2 3 4 5.8 2.69.0 3.417.1 then when the function returns, the array will have been modified so that it looks like this: 0 1 2 3 4 7.13.49.0 2.6 5.8 The function should not return any value.arrow_forwardWrite a function intersect() that takes five arguments: an array of integers, number of elements in this array, the second array of integers, number of elements in the second array, the third array as parameters; finds the numbers in the first array that also exist in the second array and places these numbers into the third array; returns the number of matching values stored in array-3. Prototype: int intersect(int ar1[], int n1, int ar2[], int n2, int ar3[])arrow_forwardWrite a function reportDuplicates() that takes a two di mensional integer array as a parameter and identifies the duplicate valuesin the array. The function then reports these to user. Sample input and corresponding output:How many rows? 5How many columns? 2Let’s populate the array:1 86 97 312 522 4Thank you, there are no duplicate elements!How many rows? 3How many columns? 4Let’s populate the array:3 7 5 76 9 7 38 5 12 6Thank you, 3 appears 2 times, 7 appears 3 times, 5 appears 2 times,and 6 appears 2 times.arrow_forward
- Create a function in Javascript that takes two numbers as arguments (num, length) and returns an array of multiples of num until the array length reaches lengtharrow_forwardWrite a function named largestNumber that accepts an array of positive integers as a parameter and returns a string representing together those integers. (We use a string since the result might be larger than the domain of type int or long.) For example, if the array passed is {11,3}, you shold return "311". If the array passed is {2,20,7,8,25}, your should return "8725220". You may assume that the array contains at least one element and that all values in the array are greater than 0.arrow_forwardWrite a function that will combine two arrays of different lengths into another array. Express the function's running time in terms of actual running time and Big-Oh notation. For reference, this is the function declaration: void combine(int* comb_arr, int* arr1, int n, int* arr2, int m) where comb_arr- the array used to combine arr1 and arr2 arr1 - the first array n - the length of the first array arr2 - the second array m - the length of the second array INITAL CODE TO BE COMPLETED #include <iostream>using namespace std;void combine(int*, int*, int, int*, int); int main(void) { // Hey there, start typing your C++ code here... int size1, size2; cin >> size1; int arr1[size1]; for (int i = 0; i < size1; i++) { cin >> arr1[i]; } cin >> size2; int arr2[size2]; for (int i = 0; i < size2; i++) { cin >> arr2[i]; } int comb_size = ___; int comb_arr[comb_size]; combine(comb_arr, arr1, size1, arr2, size2);…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