DIscrete Math C++ -


x^2 + y^2 = z^2. how test truth of pythagoras theorem in code? assignment says read in 10 integers , test if statement true or false each of ten integers. have i'm not sure if right because i'm not sure if i'm solving z.

any appreciated

void esearch(int array[], int size) { int truecount = 0; //int falsecount = 0;  for(int = 0; < size; ++i) {     for(int j = 0; j < size; ++j)     {          int x = array[i];         int y = array[j];          int z = sqrt(pow(x, 2)+ pow(y, 2));          if(z == x || y)         {             ++truecount;         }      }   } if(truecount > 0) cout << "\ne true"; else cout << "\ne false";   } 

your code won't work way want. try this. have small data size, don't care efficiency, wrote simple (not yet efficient) solution using stl. define vector , sort once in order use binary search when want check whether pair (x,y) satisfies pyth. theorem other integer input data. takes log(size), should reasonably fast large data inputs. don't need run second loop beginning of data, since you'll checking same pair in different order before. code should simple, if have questions, please ask. luck.

void esearch(int array[], int size) {  int truecount = 0; std::vector<int> z(array, array + size); std::sort(z.begin(), z.end());  int x, y; double z_check;  for(int = 0; < size; i++) {     x = array[i];      for(int j = i+1; j < size; j++)     {         y = array[j];          z_check = sqrt(x*x + y*y);          if(std::binary_search(z.begin(), z.end(), z_check))         {             truecount++;         }      } } z.clear(); if(truecount > 0) cout << truecount; else cout << "\ne false"; } 

edit: can speed things bit more since know looking number greater sqrt(x*x+y*y) in sorted vector:

if(std::binary_search(z.begin() + ceil(z_check), z.end(), z_check)) 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -