I'm trying to compile the following code that I wrote out:

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

void gauss(double, double, double, int);

int main()
{
const int n1 = 4;
double prompt;
double A1[n1][n1] = {{3,0,3,-4},{0,1,1,1},{1,1,1,2},{2,3,1,3}};
double b1[n1] ={7,0,6,6};
double x1[n1];

gauss(A1, x1, b1, n1);

cout << x1;

cout << "\n Enter \'r\' to continue..." << endl;
cin >> prompt;
return 0;
}

void gauss(double A[][4], double x[], double b[], int n)
{
for(int k = 0; k < n; k++)
{
double divisor = A[k][k];
if(divisor == 0)
{
cout << "divisor is zero" << endl;
return;
}
else
for (int i = k + 1; i < n; i++)
{
double multiplier = A[i][k]/divisor;
for(int j = k + 1; j < n; j++)
A[i][j] = A[i][j] - multiplier * b[k];
}
}
if(A[n-1][n-1] == 0)
{
cout << "The last divisor is zero" << endl;
return;
}
for(int i = n - 1; i >= 0; i--)
{
x[i] = b[i];
for(int j = i + 1; j < n; j++)
x[i] = x[i] - A[i][j] * x[j];
x[i] = x[i]/A[i][i];
}
}
return;
}``````

When I do, the following errors come up (Not sure if they're interrelated):
16 cannot convert `double (*)[4]' to `double' for argument `1' to `void gauss(double, double, double, int)'
56 expected unqualified-id before "return"
56 expected `,' or `;' before "return"
and
57 expected declaration before '}' token
I'm wondering if perhaps it has something to do with the double data type or if maybe it's actually in the way that I'm passing the variables. I'm using Bloodshed Dev C++ as my compiler, if that helps.

2
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by philosafari

When I do, the following errors come up (Not sure if they're interrelated):
16 cannot convert `double (*)[4]' to `double' for argument `1' to `void gauss(double, double, double, int)'

Look at the definition of the function and carefully check the values used in the call on line 16. Are they identical?

56 expected unqualified-id before "return"
56 expected `,' or `;' before "return"
and
57 expected declaration before '}' token

Use consistent formatting in your code and you will see immediately what the problem is. See this if you need help.

I fixed some things, I think, at least most of the error messages went away(Not exactly comforting, however). The only error message now is:
54 expected declaration before '}' token

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

void gauss(double, double, double, int);

int main()
{
const int n = 4;
double prompt;
double A[n][n] = {{3,0,3,-4},{0,1,1,1},{1,1,1,2},{2,3,1,3}};
double x[n];
double b[n] ={7,0,6,6};

gauss(A[n][n], x[n], b[n], n);

cout << x;

cout << "\n Enter \'r\' to continue..." << endl;
cin >> prompt;
return 0;
}

void gauss(double A[][4], double x[], double b[], int n){
for(int k = 0; k < n; k++){
double divisor = A[k][k];
if(divisor == 0){
cout << "divisor is zero" << endl;
return;
}
else
for (int i = k + 1; i < n; i++){
double multiplier = A[i][k]/divisor;
for(int j = k + 1; j < n; j++){
A[i][j] = A[i][j] - multiplier * b[k];
}
}
}
if(A[n-1][n-1] == 0)
{
cout << "The last divisor is zero" << endl;
return;
}
for(int i = n - 1; i >= 0; i = i - 1)
{
x[i] = b[i];
for(int j = i + 1; j < n; j++)
x[i] = x[i] - A[i][j] * x[j];
x[i] = x[i] / A[i][i];
}
}
}``````

Using two different styles for curly braces is not being consistent. Using 4 SPACEs for indents then 8 SPACEs is also being inconsistent.

I recommend

``````if (xxx)
{
code;
code;
}``````

to immediately see any mismatched braces -- which is your problem.

Using two different styles for curly braces is not being consistent. Using 4 SPACEs for indents then 8 SPACEs is also being inconsistent.

I recommend

``````if (xxx)
{
code;
code;
}``````

to immediately see any mismatched braces -- which is your problem.

I fixed the indents(I think that what I did was fixing that)
I am confused, however, as to what exactly you mean by two different kinds of curly brackets. {} and () or just {} used improperly?
Also, after making more consistent indents (I went with 4 spaces), the error message seems to be:
[Linker error] undefined reference to `RREF(double, double, double, int)'
ld returned 1 exit status

``````void RREF(double A[][4], double x[], double b[], int n)
{
for(int k = 0; k < n; k++)
{
double divisor = A[k][k];
if(divisor == 0)
{
cout << "divisor is zero" << endl;
return;
}
else
for (int i = k + 1; i < n; i++)
{
double multiplier = A[i][k]/divisor;
for(int j = k + 1; j < n; j++)
{
A[i][j] = A[i][j] - multiplier * b[k];
}
}
if(A[n-1][n-1] == 0)
{
cout << "The last divisor is zero" << endl;
return;
}
for(int i = n - 1; i >= 0; i = i - 1)
{
x[i] = b[i];
for(int j = i + 1; j < n; j++)
{
x[i] = x[i] - A[i][j] * x[j];
x[i] = x[i] / A[i][i];
}
}
}
}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.