Calling methods with arrays in C++ -


my problem have trouble calling int computepattern method in int main(). able use insertnumber method. don't know how use computepattern method. method compute number of patterns , return count. pattern both adjacent numbers must greater 7. hence, there 5 pairs check there 10 numbers. output random arrays worked, need print out number of patterns too. need use methods required in question.

int insertnumber(int b ) {  int = rand()%10+1;  return a; } int computepattern(int* inarray, int size ) { int patterns=0;  for(int z=0;z<size;z=z+2)  {     if(inarray[z]+inarray[z+1]>7)     {         patterns ++ ;     } }  return patterns; } int main() { int arrays[10]; srand(time(0)); ( int b=0; b<10 ; b++) {  arrays[b]  = insertnumber(b); } cout << "arrays"; ( int c= 0; c<10; c++) {     cout << " " ;     cout << arrays[c]; } int patterns =computepattern(arrays,10); cout<<endl; cout << "patterns" <<patterns; } 

you have different arrays in main in computepattern. because share name doesn't mean they're same variable. each 1 of them function scope. better way pass in address , size of array.

int computepattern(int* inarray, int size) {     int patterns=0;     for(int z=0; z<size-1; z+=2)     {         if((inarray[z] + inarray[z+1]) > 7)         {             patterns++;         }     }     return patterns; } //... in main...  int patterns=computepattern(arrays, 10); cout << patterns; 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -