/* * * CSI2131 * * Shantanu Das * * Lab-9 * */ #include #include #include using namespace std; // Two given Arrays of integers int Array1[] = {110,120,130,140,150,160,170,180,190,200}; int Array2[] = { 98, 87, 76, 65, 54, 43, 32, 21, 10, 9 }; int addint(int a, int b) { return a + b; } int subint(int a, int b) { return a - b; } int mulint(int a, int b) { return a * b; } int divint(int a, int b) { return a / b; } /// Array of pointers to functions int (*FpArr[])(int,int) = { &addint,&subint,&mulint,&divint }; // performs the given operation on elements of the arrays int* performOp(int *A, int *B, int size, int (*f)(int,int)); // displays the elements of an array void display(int *X, int size); // main method int main( ) { int choice = 0; // user choice unsigned int size = 0; // size of the array int *res = NULL; // to store the result size = sizeof(Array1); // set size to minimum size of the two arrays if (sizeof(Array2) < size) size = sizeof(Array2); size = size / sizeof(int); while(choice != 5) { cout << "\n\nWhich operation do you want to perform? "<< endl; cout << "1.Add " << endl; cout << "2.Subtract " << endl; cout << "3.Multiply " << endl; cout << "4.Divide " << endl; cout << "5.None ... just quit the program! " << endl; cout << "\nEnter choice: " ; cin >> choice; if(choice == 5) break; if(choice < 1 || choice > 5) continue; int j = choice - 1; res = performOp(Array1,Array2,size,FpArr[j]); //perform the desired operation. cout << "\n\nThe Results are ..." << endl; display(res,size); if(res!=NULL) delete[] res; } cout << endl << "Program completed successfully !!" << endl; return 0; } // performs the given operation on all elements of the arrays A and B int* performOp(int *A, int *B, int size, int (*f)(int,int)) { int *C = new int[size]; for(int i=0; i < size; i++ ) { C[i] = (*f)(A[i],B[i]); } return C; } void display(int *X, int size) { if(X == NULL) return; for(int i=0; i < size; i++ ) { cout << X[i] << " "; } cout << endl; }