Question

In: Computer Science

(C++) Behavior: Returns true is there exists a sequence of 3 *consecutive* values in the array...

(C++)
Behavior: Returns true is there exists a sequence of 3 *consecutive* values in the array such that the sum of the first two elements is equal to the third element in that sequence, false otherwise. The third element has to be within a distance of dist from the second element. False otherwise.

Example: For the array {3,4,1,3,17,3,96,21,5,20}, if dist is 7 the function returns true because 4+1=5 and the element 5 is within 7 spots from element 1.

bool exists_trio_within_distance(int*, int, int);
int main()
{
   const int asize=10;
   int a[asize]={3,4,1,3,17,3,20,21,5,20};

   const int bsize=6;
   int b[bsize]={3,4,1,3,3,7};
   //////////////////////////////////////////

//test exists_trio function
  

   //should print "A trio exists."


   if (exists_trio(a,asize))
       cout << "A trio exists.\n";
   else
       cout << "A trio does not exist.\n";

   //should print "A trio does not exist."
   if (exists_trio(b,bsize))
       cout << "A trio exists.\n";
   else
       cout << "A trio does not exist.\n";

   cout << "=========================\n";

   //////////////////////////////////////////////

//test exists_trio_within_distance function

   //if you only want to test exists_trio, comment
   //out the below code

   //change the array a to help test Function 2
   a[6]=209; //change a[6] from 20 to 209
   int dist=7;
   //should print "A trio exists within distance 7."
   if (exists_trio_within_distance(a,asize,dist))
       cout << "A trio exists within distance " << dist << "." << endl;
   else
       cout << "A trio does not exist within distance " << dist << "." << endl;

   dist=2;
   //should print "A trio does not exist within distance 2."
   if (exists_trio_within_distance(b,bsize,dist))
       cout << "A trio exists within distance " << dist << "." << endl;
   else
       cout << "A trio does not exist within distance " << dist << "." << endl;
}

Solutions

Expert Solution

Here is the C++ code to the given question.

As asked in the question, the two functions exists_trio() and exists_trio_within_distance() are completed.

Output of the program is added at the end.

Code:



#include <iostream>

using namespace std;


//function to check if trio exists for three consecutive numbers
bool exists_trio(int* array,int asize){
    
    for(int i=2;i<asize;i++){     /*runs the loop from third element to last element of array*/
        
        if(array[i-1]+array[i-2]==array[i]){    /*checks if the sum of two numbers before the present number is equal to present number*/
            return true;     /*returns true if such trio is found*/
        }
    }
    return false;     /*returns false if no such trio is found*/
}


//function to check if trio exists within given distance
bool exists_trio_within_distance(int* array, int asize, int dist){
    
    for(int i=2;i<asize;i++){    /*runs the loop from third element to last element of array*/
        
        for(int j=i;j<i+dist;j++){    /*runs inner loop from ith element to i+dist element*/
            
            if(array[i-1]+array[i-2]==array[j]){    /*checks if sum of two numbers before the starting number of this loop is equal to the present number*/
                return true;   /*returns true if any such trio is found*/
            }
        }
    }
    
    return false;  /*returns false if no such trio is found*/
    
}


int main()
{
   const int asize=10;
   int a[asize]={3,4,1,3,17,3,20,21,5,20};

   const int bsize=6;
   int b[bsize]={3,4,1,3,3,7};
   //////////////////////////////////////////

//test exists_trio function
  

   //should print "A trio exists."


   if (exists_trio(a,asize))
       cout << "A trio exists.\n";
   else
       cout << "A trio does not exist.\n";

   //should print "A trio does not exist."
   if (exists_trio(b,bsize))
       cout << "A trio exists.\n";
   else
       cout << "A trio does not exist.\n";

   cout << "=========================\n";

   //////////////////////////////////////////////

//test exists_trio_within_distance function

   //if you only want to test exists_trio, comment
   //out the below code

   //change the array a to help test Function 2
   a[6]=209; //change a[6] from 20 to 209
   int dist=7;
   //should print "A trio exists within distance 7."
   if (exists_trio_within_distance(a,asize,dist))
       cout << "A trio exists within distance " << dist << "." << endl;
   else
       cout << "A trio does not exist within distance " << dist << "." << endl;

   dist=2;
   //should print "A trio does not exist within distance 2."
   if (exists_trio_within_distance(b,bsize,dist))
       cout << "A trio exists within distance " << dist << "." << endl;
   else
       cout << "A trio does not exist within distance " << dist << "." << endl;
}

Output:


Related Solutions

Assume that there are a sequence of consecutive integers 1, 2, 3, 4, 5, ... 15....
Assume that there are a sequence of consecutive integers 1, 2, 3, 4, 5, ... 15. Tom and Jim respectively select a number from the sequence randomly (no repetition). Given that Tom’s number is divisible by 5, what’s the probability that Tom’s number is greater than Jim’s number ?
Write a c program arrays2.c that checks if an integer array contains three identical consecutive elements....
Write a c program arrays2.c that checks if an integer array contains three identical consecutive elements. Assume the number of identical consecutive elements are no more than three if they exist. Sample input/output #1: Enter the length of the array: 11 Enter the elements of the array: -12 0 4 2 2 2 36 7 7 7 43 Output: The array contains 2 of three identical consecutive elements: 2 7 Sample input/output #2: Enter the length of the array: 4...
Assume that a sequence of 10 32-bit values exists in your data segment at label "x:"...
Assume that a sequence of 10 32-bit values exists in your data segment at label "x:" Write the MIPS statement(s) to read the 2nd 32-bit value into $t0
MUST BE DONE IN C (NOT C++) Using an array and a function, print the values...
MUST BE DONE IN C (NOT C++) Using an array and a function, print the values of an array backwards. Please follow these guidelines: - Setup your array manually (whichever values you want, as many as you want and whichever datatype you prefer). - Call your function. You should send two parameters to such function: the array’s length and the array. - Inside the function, go ahead and print the array backwards. - Your function shouldn’t return anything
The following algorithm returns true if the n items in an array are all distinct, i.e.,...
The following algorithm returns true if the n items in an array are all distinct, i.e., if no two items are equal to each other, otherwise it returns false. 1 ALGORITHM UniqueElements(A[1..n]) 2 // Determine whether all the elements in an array are distinct 3 // Input: Array A[1..n] 4 // Output: “true” if all elements of A are distinct, “false” otherwise 5 for i ← 1 to n – 1 do 6 for j ← i + 1 to...
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns...
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns the highest similarity score found in the genome as a double. Note: the term genome refers to the string that represents the complete set of genes in an organism, and sequence to refer to some substring or sub-sequence in the genome. Your function MUST be named findBestSimScore Your function should take two parameters in this order: a string parameter for the genome (complete set...
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
Programming In C Write a program that prints the values in an array and the addresses...
Programming In C Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
In C++, write a function that returns the average of all the values stored in an...
In C++, write a function that returns the average of all the values stored in an integer array (called arr) of size n. The return type of the function should be double. Test this function in the main program.
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT