#include <iostream> #include <vector.h> using namespace std; bool foundPos(double **a, int n, int m, double x, int &iPos, int &jPos) { iPos = -1; jPos = -1; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(a[i][j] == x) { iPos = i; jPos = j; break; } } if(iPos != -1 && jPos != -1) { break; } } return (iPos != -1 && jPos != -1); } double sumOfNegative(double **a, int n, int m) { double s = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(a[i][j] < 0) { s += a[i][j]; } } } return s; } void main() { int n; int m; double **a; cout<<"Enter rows count: "; cin>>n; cout<<"Enter columns count: "; cin>>m; cout<<endl; a = new double*[n]; for(int i = 0; i < n; i++) { a[i] = new double[m]; for(int j = 0; j < m; j++) { cout<<"Enter element ["<<i<<", "<<j<<"]: "; cin>>a[i][j]; } } // Method #1 cout<<endl; double x; int iPos; int jPos; cout<<"Enter value: "; cin>>x; cout<<endl; if(foundPos(a, n, m, x, iPos, jPos)) { cout<<"Element that has value "<<x<<" is at ["<<iPos<<", "<<jPos<<"]"; } else { cout<<"Element that has value "<<x<<" was not found"; } cout<<endl; // Method #2 cout<<endl; cout<<"Sum of negative elements is "<<sumOfNegative(a, n, m); getchar(); } |