Question

C ++Write a function, anyThree (int a[], int n), that returns true if the value 3 appears in the array exactly 3 times, and no 3's are next to each other. The parameter n will be greater than or equal to 0.

Write a function, anyThree (int all, int n), that returns true if the value 3 appears in the array exactly 3 times, and no 3

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Function:

bool anyThree (int a[], int n) //function
{
int threes=0; //to count number of 3s in array.
for(int i=0; i<n; i++)
{
if(a[i]==3) //if number is 3 then incrementing the count of 3s
threes++;

if(a[i]==3 && a[i-1]==3 && i!=0) //if two 3s are next to each other then return false.
return false;
}
if(threes==3) //if number of 3's in array are 3 then return true.
return true;
return false; //if number of 3's in array are not equal to 3 then return false.
}

bool anyThree (int a[], int n) //function { int threes=0; //to count number of 33 in array. for(int i=0; i<n; i++) { if(a[i]=

Program:

#include<bits/stdc++.h>
using namespace std;
bool anyThree (int a[], int n) //function
{
int threes=0; //to count number of 3s in array.
for(int i=0; i<n; i++)
{
if(a[i]==3) //if number is 3 then incrementing the count of 3s
threes++;

if(a[i]==3 && a[i-1]==3 && i!=0) //if two 3s are next to each other then return false.
return false;
}
if(threes==3) //if number of 3's in array are 3 then return true.
return true;
return false; //if number of 3's in array are not equal to 3 then return false.
}
int main()
{ //declaring and calling the function and printing the result.
int a[] = {3, 1, 3, 1, 3};
cout << anyThree(a, 5) << endl;

int b[] = {3, 1, 3, 3};
cout << anyThree(b, 4) << endl;

int c[] = {3, 4, 3, 3, 4};
cout << anyThree(c, 5) << endl;

int d[] = {3};
cout << anyThree(d, 1) << endl;
return 0;
}

1 2 3 #include<bits/stdc++.h> using namespace std; bool anyThree (int a[], int n) //function 4 int threes=0; //to count numbe

output:

1 © Process returned o (Oxo) execution time : 0.017 s Press any key to continue.

Note: my friend if you have any questions or queries comment below. i am happy to answer your questions. I will sort out your queries. Thank you my friend.

Add a comment
Know the answer?
Add Answer to:
C ++Write a function, anyThree (int a[], int n), that returns true if the value 3...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ language Write a function, int flip(string a[], int n); It reverses the order of the...

    C++ language Write a function, int flip(string a[], int n); It reverses the order of the elements of the array and returns n. The variable n will be greater than or equal to zero. Example: string array[6] = { "a", "b", "", "c", "d", "e" }; int q = flip(array, 4); // returns 4 // array now contains: "C" "" "" "a" "d" "e" O Full Screen 1 code.cpp + New #include <string> 2 using namespace std; 3 4- int...

  • C++ ONLY Write a function, int flip(string a[], int n); It reverses the order of the...

    C++ ONLY Write a function, int flip(string a[], int n); It reverses the order of the elements of the array and returns n. The variable n will be greater than or equal to zero. Example: string array[6] = { "a", "b", "", "c", "d", "e" }; int q = flip(array, 4); // returns 4 // array now contains: "c" "" "b" "a" "d" "e" Write a function, int flip(string a[], int n); It reverses the order of the elements of...

  • In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an...

    In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an array (allocated off of the heap in the function) containing only the elements of array a that pass the “test” t. The function iterates through all the elements of a, and constructs a new array containing all the elements that pass the test. When the parameter corresponding to “size” is passed in (by reference), it contains the size of the array a. In the...

  • using c++ Write a function that returns true if a given integer array is sorted(in ascending...

    using c++ Write a function that returns true if a given integer array is sorted(in ascending order) and false if it is not. The function should perform no more than "size" comparisons. bool isSorted(int A[], int size);

  • java programming. One. Write a method public boolean hasOnlyoddDigits(int n) that returns true if its parameter...

    java programming. One. Write a method public boolean hasOnlyoddDigits(int n) that returns true if its parameter n contains only odd digits (1, 3, 5, 7, or 9), and returns false otherwise. Zero is counted as an even digit whenever it appears inside the number. Remember that for a positive integer n, the expression (n % 10) gives its last digit, and the expression (n / 10) gives its other digits. Correct answer true false false false 357199 7540573 97531000

  • 13. Write a recursive function with the declaration: int count Equal (int* numbers, int n, int...

    13. Write a recursive function with the declaration: int count Equal (int* numbers, int n, int x) that has as parameters an array numbers with n > 0 elements, and an integer x, and returns how many times I appears in the array. 14. Write a recursive function with the declaration: double dist(double* u, double *v, int n) that gets two double precision arrays with n > 1 elements and returns the value: Vu[0] - v[0])2 + (u[1] – v[1])2...

  • In class Recursion problems bool anyNegative(int al , int size) This function returns true if there...

    In class Recursion problems bool anyNegative(int al , int size) This function returns true if there are any negative numbers in the array, grity false otherwise. int firstNegative(int al I, int size) This function returns the position of the first negative number in the array & EAO If there are no negative numbers it returns int countNegative(int al I, int size) This function returns the number of negative numbers in the array a. 12 bst Stringsint indexO bout Strings nt...

  • C programming Write a function that gets a 2-d array of given dimensions of ints. It...

    C programming Write a function that gets a 2-d array of given dimensions of ints. It returns true if the array contains two rows with exactly the same values in the same order, and returns false otherwise, bool contains_equal_rows(int height, int width, const int ar[height] [width]) Examples: On input {{1,2,3,4}, {2,3,4,1}, {1,2,3,4}} it returns true. On input {{1,2,3,4}, {2,3,4,5}, {3,4,5,6}} it returns false. On input {{1,1,1,1}, {2,2,2,2}, {1,1,1,6}} it returns false.

  • (a) Write a recursive function int find(const int A[], int n, int x); which returns the...

    (a) Write a recursive function int find(const int A[], int n, int x); which returns the first index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found. (b) Write a recursive function int rfind(const int A[], int n, int x); which returns the last index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found....

  • Given an array of strings, return true if each string's size is equal or greater than...

    Given an array of strings, return true if each string's size is equal or greater than the one before, otherwise return false. The array will be length 2 or more. Example 1: If the names array contains… “Edwin” “Satish” “Solomon” “Massoud” …then the function call stringsIncreasing (names, 4) returns true. Example 2: If the names array contains… “Janet” “Linda” “Jackie” “Marta” …then the function call stringsIncreasing (names, 4) returns false since string Jackie comes before Marta and has more letters...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT