In: Computer Science
(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;
}
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: