0

Recently I engaged in programming. In my school were told to write a program to solve systems of linear equations Gauss method, that's what I did, but I an error "'abs' cannot be used as a function", please tell me how to fix.

#include <iostream>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
// Вывод системы уравнений
void sysout(double **a, double *y, int n) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++){
      cout << a[i][j] << "*x" << j;
      if (j < n - 1) {
        cout << " + ";
      }
    }
  cout << " = " << y[i] << endl;
  }
  return;
}
double * gauss(double **a, double *y, int n) {
  double *x, max;
  int k, index;
  const double eps = 0.00001;  // точность
  x = new double[n];
  k = 0;
  while (k < n) {
  // Поиск строки с максимальным a[i][k]
      int abs;
    max = abs(a[k][k]);
    index = k;
    for (int i = k + 1; i < n; i++) {
      if (abs(a[i][k]) > max) {
        max = abs(a[i][k]);
        index = i;
      }
    }
  // Перестановка строк
    if (max < eps) {
    // нет ненулевых диагональных элементов
      cout << "Решение получить невозможно из-за нулевого столбца " ;
      cout << index << " матрицы A" << endl;
      return 0;
    }
    for (int j = 0; j < n; j++) {
      double temp = a[k][j];
      a[k][j] = a[index][j];
      a[index][j] = temp;
    }
    double temp = y[k];
    y[k] = y[index];
    y[index] = temp;
  // Нормализация уравнений
    for (int i = k; i < n; i++) {
      double temp = a[i][k];
      if (abs(temp) < eps) continue; // для нулевого коэффициента пропустить
      for (int j = 0; j < n; j++) {
        a[i][j] = a[i][j] / temp;
      }
      y[i] = y[i] / temp;
      if (i == k)  continue; // уравнение не вычитать само из себя
      for (int j = 0; j < n; j++) {
        a[i][j] = a[i][j] - a[k][j];
      }
      y[i] = y[i] - y[k];
    }
    k++;
  }
  // обратная подстановка
  for (k = n - 1; k >= 0; k--) {
    x[k] = y[k];
    for (int i = 0; i < k; i++) {
      y[i] = y[i] - a[i][k] * x[k];
    }
  }
  return x;
}
int main() {
  double **a, *y, *x;
  int n;
  system("chcp 1251>nul");
  system("cls");
  cout << "Введите количество уравнений: ";
  cin >> n;
  a = new double*[n];
  y = new double[n];
  for (int i = 0; i < n; i++) {
    a[i] = new double[n];
    for (int j = 0; j < n; j++) {
      cout << "a[" << i << "][" << j << "]= ";
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    cout << "y[" << i << "]= ";
    cin >> y[i];
  }
  sysout(a, y, n);
  x = gauss(a, y, n);
  for (int i = 0; i < n; i++){
    cout << "x[" << i << "]=" << x[i] << endl;
  }
  cin.get(); cin.get();
  return 0;
}

Change the variable to "fabs" tried to change to "std :: abs" tried. Compiler MiGW.

Sergey M
  • 23
  • 3
  • 4
    Why do you have `int abs;` at the beginning of the while loop? – NathanOliver Dec 01 '16 at 22:04
  • 3
    Get rid of `using namespace std;`. This kind of error is the reason that namespaces were invented. If you blow away `std` you're on your own to deal with the problems that you created. – Pete Becker Dec 01 '16 at 22:07
  • You don't want to include both `stdlib.h` and `cstdlib` - they have the same functions, but `cstdlib` has them in the `std` namespace. See [this answer](http://stackoverflow.com/a/1374361/2449857) for more info. – Jack Deeth Dec 01 '16 at 22:24

1 Answers1

1

If you #include <cmath> instead of stdlib.h and cstdlib then it works:

#include <iostream>
#include <cmath>
using namespace std;

// Вывод системы уравнений
void sysout(double **a, double *y, int n) {
    ...

Also you should remove the int abs; in the while loop.

I'm not sure why #include <cstdlib> should cause problems here - can anyone explain?

Here's an online demo of the code compiling.

Jack Deeth
  • 3,062
  • 3
  • 24
  • 39