I have some problems with an array of type of float. I'm trying to program the Gauss elimination algorithm, but I get the problem that my compiler for some reason performs integer division on the elements of the array, although it's declared to be an array of type float.
#include <iostream>
#include <iomanip>
#include <tgmath.h>
#include <cstddef>
#include <cmath>
#include <fstream>
#include <vector>
usingnamespace std;
// Gauss Algorithm
int main()
{
const size_t NDIM = 3;
float a[NDIM][NDIM] = {-3.0, 1.0, 2.0 , 1.0, 0.0, -1.0, 4.0, -1.0, 2.0};
float b[NDIM] = {1.0, -1.0, 8.0};
cout << "before gauss algorithm: " << endl;
for(int i = 0; i < NDIM; i++){
for (int j = 0; j < NDIM; j++)
cout << a[i][j] << " ";
cout << b[i];
cout << endl;}
// Outer loop runs through each row
for(int row = 0; row < NDIM; row++){
// 1. nested loop: Divides each element in row by pivot
for(int col = row; col < NDIM; col++)
a[row][col]=(a[row][col])/(a[row][row]);
b[row] /= a[row][row];
// 2. nested loop: Subtract l*row from elements of row i
for(int i = row + 1; i < NDIM; i++){
double l = a[i][row] / a[row][row];
for (int j = row; j < NDIM; j++)
a[i][j]=a[i][j] - l*(a[row][j]);
b[i] -= l*b[row];
}
cout << endl << "After elimination step: " << row + 1<< endl;
for(int i = 0; i < NDIM; i++){
for (int j = 0; j < NDIM; j++)
cout << a[i][j] << " ";
cout << b[i];
cout << endl;}
}
cout << endl << "final shape: " << endl;
for(int i = 0; i < NDIM; i++){
for (int j = 0; j < NDIM; j++)
cout << a[i][j] << " ";
cout << b[i];
cout << endl;}
return 0;
}
Now the problem is with how the array stores values in it after an elimination step. It has nothing to do with the standard output.
I can't seem to find the reason why it does this right now. I'd appreciate a quick response.