0

I'm not brilliant at coding, I'm just starting off and the code I created runs with that many errors I feel like I should just start again, but I have no clue what to do differently.

Below is the code I'm running that's coming back with all the errors, I just can't put my finger on what I'm doing wrong and it's so overwhelming.

Please help if you can, thank you x

Edit: sorry I forgot to add the errors! I've got 23 errors, most are 'array type is not assignable' and 'expression did not evaluate to a constant'

#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
    int i, j, k, n, N;
    cout.precision(4);                        //set precision
    cout.setf(ios::fixed);
    cout << "\nEnter the no. of data pairs to be entered:\n";        //To find the size of arrays that will store x,y, and z values
    cin >> N;
    double x[N], y[N];
    cout << "\nEnter the x-axis values:\n";                //Input x-values
    for (i = 0; i < N; i++)
        cin >> x[i];
    cout << "\nEnter the y-axis values:\n";                //Input y-values
    for (i = 0; i < N; i++)
        cin >> y[i];
    cout << "\nWhat degree of Polynomial do you want to use for the fit?\n";
    cin >> n;                                // n is the degree of Polynomial 
    double X[2 * n + 1];                        //Array that will store the values of sigma(xi),sigma(xi^2),sigma(xi^3)....sigma(xi^2n)
    for (i = 0; i < 2 * n + 1; i++)
    {
        X[i] = 0;
        for (j = 0; j < N; j++)
            X[i] = X[i] + pow(x[j], i);        //consecutive positions of the array will store N,sigma(xi),sigma(xi^2),sigma(xi^3)....sigma(xi^2n)
    }
    double B[n + 1][n + 2], a[n + 1];            //B is the Normal matrix(augmented) that will store the equations, 'a' is for value of the final coefficients
    for (i = 0; i <= n; i++)
        for (j = 0; j <= n; j++)
            B[i][j] = X[i + j];            //Build the Normal matrix by storing the corresponding coefficients at the right positions except the last column of the matrix
    double Y[n + 1];                    //Array to store the values of sigma(yi),sigma(xi*yi),sigma(xi^2*yi)...sigma(xi^n*yi)
    for (i = 0; i < n + 1; i++)
    {
        Y[i] = 0;
        for (j = 0; j < N; j++)
            Y[i] = Y[i] + pow(x[j], i) * y[j];        //consecutive positions will store sigma(yi),sigma(xi*yi),sigma(xi^2*yi)...sigma(xi^n*yi)
    }
    for (i = 0; i <= n; i++)
        B[i][n + 1] = Y[i];                //load the values of Y as the last column of B(Normal Matrix but augmented)
    n = n + 1;                //n is made n+1 because the Gaussian Elimination part below was for n equations, but here n is the degree of polynomial and for n degree we get n+1 equations
    cout << "\nThe Normal(Augmented Matrix) is as follows:\n";
    for (i = 0; i < n; i++)            //print the Normal-augmented matrix
    {
        for (j = 0; j <= n; j++)
            cout << B[i][j] << setw(16);
        cout << "\n";
    }
    for (i = 0; i < n; i++)                    //From now Gaussian Elimination starts(can be ignored) to solve the set of linear equations (Pivotisation)
        for (k = i + 1; k < n; k++)
            if (B[i][i] < B[k][i])
                for (j = 0; j <= n; j++)
                {
                    double temp = B[i][j];
                    B[i][j] = B[k][j];
                    B[k][j] = temp;
                }

    for (i = 0; i < n - 1; i++)            //loop to perform the gauss elimination
        for (k = i + 1; k < n; k++)
        {
            double t = B[k][i] / B[i][i];
            for (j = 0; j <= n; j++)
                B[k][j] = B[k][j] - t * B[i][j];    //make the elements below the pivot elements equal to zero or elimnate the variables
        }
    for (i = n - 1; i >= 0; i--)                //back-substitution
    {                        //x is an array whose values correspond to the values of x,y,z..
        a[i] = B[i][n];                //make the variable to be calculated equal to the rhs of the last equation
        for (j = 0; j < n; j++)
            if (j != i)            //then subtract all the lhs values except the coefficient of the variable whose value                                   is being calculated
                a[i] = a[i] - B[i][j] * a[j];
        a[i] = a[i] / B[i][i];            //now finally divide the rhs by the coefficient of the variable to be calculated
    }
    cout << "\nThe values of the coefficients are as follows:\n";
    for (i = 0; i < n; i++)
        cout << "x^" << i << "=" << a[i] << endl;            // Print the values of x^0,x^1,x^2,x^3,....    
    cout << "\nHence the fitted Polynomial is given by:\ny=";
    for (i = 0; i < n; i++)
        cout << " + (" << a[i] << ")" << "x^" << i;
    cout << "\n";
    return 0;
    }
  • 1
    what are the errors? – 463035818_is_not_an_ai Nov 11 '20 at 22:09
  • dont get overwhelmed by errors, anyhow it doesn't make too much sense to read more than the first. The others might be caused by the first. First problem I see in the code is `double x[N], y[N];`. Array sizes must be compile-time constants. – 463035818_is_not_an_ai Nov 11 '20 at 22:16
  • @idclev463035818 sorry I forgot to add that! I've got 23 errors, most are 'array type is not assignable' and 'expression did not evaluate to a constant' – ellen_230920000 Nov 11 '20 at 22:24
  • double[N] types, where N is known only at runtime, are called variable-length arrays and are not part of C++, though some compilers might have compiler-specific extensions that support them. Use a `std::vector` instead; this'll handle the two error message you mentioned, mostly, I think. – Anonymous1847 Nov 11 '20 at 22:25
  • 1
    As I said, start with the first, fix it compile again then fix the next. If you dont understand the error message you should include it in the quesiton – 463035818_is_not_an_ai Nov 11 '20 at 22:25
  • btw you made a common beginners mistake: You wrote a wall of code and now you face a wall of compiler errors. Instead write one line of code, make sure it is correct, only then write the next line. Sounds cumbersome, but thats how you end up with many lines of code and maximum one line that is wrong as opposed to many lines of code with many errors – 463035818_is_not_an_ai Nov 11 '20 at 22:29
  • I'm sorry I'm new to it all that was my fault I know now I should've included the error :) Thanks for taking the time to help me – ellen_230920000 Nov 11 '20 at 22:44

1 Answers1

1

You have in your code several variables which are declared as double x[N], where N is a variable that is known only at run-time. This is not guaranteed to be supported. Instead, you should use a std::vector, initialized like this: std::vector<double> x(N). This creates a vector of doubles of size N, zero-initialized. Look up how to use vectors here: https://en.cppreference.com/w/cpp/container/vector . Also, you should use descriptive variable names, which will help you read and understand your own code (and others you're asking for help). Don't be afraid of 23 error messages, I routinely get 100+ on first compilation of a fresh batch of code. Often they can cascade where one causes a lot of others down the line, so work starting from the one that's earliest in the code, recompiling after every bugfix. The 100+ effectively become 30 or so, sometimes.

Also, it would be helpful to split your function into several functions, and test each one individually, then bring them all together.

Anonymous1847
  • 2,568
  • 10
  • 16