Question

In: Computer Science

I was not sure how to utilize this line because I made code that works but...

I was not sure how to utilize this line because I made code that works but not with this line specifically. C++

Function 2:
bool exists_trio_within_distance(int*,int,int);
//Input:
   //an integer array (param 1), its size (param 2), and
   //a distance (param 3)

//Output:
   //True or false

//Behavior:
   //Returns true is there exists
   //a sequence of 3 values in the array
   //such that sum of the first two elements
   //is equal to the third element in the
   //sequence. 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.

   //For the array {3,4,1,3,3,7},
   //if dist is 2,
   //the function returns false.
   //This is because, even though, for example, 3+4=7,
   //element 7 is not within a distance of 2
   //from element 4.

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

#include<bits/stdc++.h>
using namespace std;

bool exists_trio_within_distance(int* arr, int n , int d) {

  for (int i = 0; i < n - 2; ++i) {

    int sum = arr[i] + arr[i + 1];
    int j = i + 2;

    for (int k = 0; k < d ; ++k) {

      if (arr[k + j] == sum) {
        return true;
      }

    }

  }
  return false;
}
//Input:
//an integer array (param 1), its size (param 2), and
//a distance (param 3)

//Output:
//True or false

//Behavior:
//Returns true is there exists
//a sequence of 3 values in the array
//such that sum of the first two elements
//is equal to the third element in the
//sequence. 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.

//For the array {3,4,1,3,3,7},
//if dist is 2,
//the function returns false.
//This is because, even though, for example, 3+4=7,
//element 7 is not within a distance of 2
//from element 4.

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

C++ bool exists_trio(int*,int); (it must use this line here) I was not sure how to utilize...
C++ bool exists_trio(int*,int); (it must use this line here) I was not sure how to utilize this line because I made code that works but not with this line specifically. //Input:    //an integer array (param 1) and its size (param 2) //Output:    //True or false //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...
How would I test a code i made in FreeBSD? I created a dictionary called ls...
How would I test a code i made in FreeBSD? I created a dictionary called ls and inside of it i made a makefile and a code called ls.c; trying to recreate the ls -l command and want to test it.
Make sure it works on jsfiddle and keep the code seperate html: css: javascript: -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Make sure it works on jsfiddle and keep the code seperate html: css: javascript: ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Assignment You should already have some experience with jQuery and some simple experience with JSON (from charting). In this assignment we will be creating some JSON data, parsing it, and displaying it. Step 1 – If you are not familiar with JSON you should complete the JSON tutorial at w3schools Step 2- You will now create a JSON file to represent some data of your...
How can I edit this C code to make sure that the letter P and L...
How can I edit this C code to make sure that the letter P and L would show up separately one at a time on an interval of one second on a raspberry pi? 1 #include <stdio.h> 2 #include <unistd.h> 3 #include "sense.h" 4 5 #define WHITE 0xFFFF 6 7 int main(void) { 8     // getFrameBuffer should only get called once/program 9     pi_framebuffer_t *fb=getFrameBuffer(); 10     sense_fb_bitmap_t *bm=fb->bitmap; 11 12      bm->pixel[0][0]=WHITE; 13      bm->pixel[0][1]=WHITE; 14      bm->pixel[0][2]=WHITE; 15      bm->pixel[0][3]=WHITE; 16      bm->pixel[0][4]=WHITE; 17      bm->pixel[0][5]=WHITE;...
Write pseudo-code to solve the following problem using MapReduce and explain how it works. Each line...
Write pseudo-code to solve the following problem using MapReduce and explain how it works. Each line in the file lists a user ID, the ID of the movie the user watched, the rating the user gave for the movie, and the timestamp. For example line 1 indicates that the user’s ID is 196, the movie ID is 242, the user gave this movie a rating of 3, and the timestamp is 881250949. Given the file, find out the top similar...
I have a code and it works and runs as it supposed too. What is the...
I have a code and it works and runs as it supposed too. What is the UML for it? Any help will be awesome. Thanks. import java.util.Scanner; public class StringToMorseCode { public static void main(String[] args){ Scanner input = new Scanner(System.in);    char[] letters = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6',...
Question 3. In the following code, answer these questions: Analyze the code and how it works?...
Question 3. In the following code, answer these questions: Analyze the code and how it works? How can we know if this code has been overwritten? Justify how? #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int changed = 0; char buff[8]; while (changed == 0){ gets(buff); if (changed !=0){ break;} else{     printf("Enter again: ");     continue; } }      printf("the 'changed' variable is modified\n %d", changed); } END of the questions :::::::::::::::::::::::::: :::::::::::::::::::::::::: Submission...
Using Python, I am trying to integrate 'iloc' into the line of code below? This line...
Using Python, I am trying to integrate 'iloc' into the line of code below? This line is replacing all the '1' values across my .csv file and is throwing off my data aggregation. How would I implement 'iloc' so that this line of code only changes the '1's integers in my 'RIC' column? patient_data_df= patient_data_df.replace(to_replace=[1], value ="Stroke") Thank you:)
If a person was unable yo utilize beta ocidation, because of specific mutations, how would this...
If a person was unable yo utilize beta ocidation, because of specific mutations, how would this complicated the persons life? what would be the results?
How is money made? This is about the banking system, and how it works to “make”...
How is money made? This is about the banking system, and how it works to “make” money.  Please use words and examples or whatever you can.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT