0

I am trying to add some polynomials after multiplying each by a different constant. I have been able to setup the arrays and multiply by the constants, but when I get to the addition of each position in the different arrays(polynomials) into a new array but I get the error "Variable-sized object may not be initialized" and the program does not compile. Also the multiplication always yields 0 no matter what the values of the arrays or the values of the c's are. I am not sure what exactly is wrong.

Any help would be appreciated

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{

    FILE *fp;
    fp = fopen("approximating the wronskian w/o wronskian.txt", "w");

    fprintf(fp, "|  c1  |  c2  |  c3  | value |\n");
    long int f1[3]= {1, 3, 4};
    long int f2[3]= {3, 4, 5};
    long int f3[3]= {3, 6, 8};


    for(int c1 = 0; c1 < 100; c1++)
    {
        for(int c2 = 0; c2 < 100; c2++)
        {
            for(int c3 = 0; c3 < 100; c3++)
            {
                for(int i = 0; i < 3; i++)
                {
                    f1[i] *= c1;
                    f2[i] *= c2;
                    f3[i] *= c3;

                    int w[i] = f1[i] + f2[i] + f3[i];

                    fprintf(fp, "|  %d  |  %d  |  %d  | %ld | %ld | %ld |\n", c1, c2, c3, f1[i], f2[i], f3[i]);
                }
            }
        }
    }

    fclose(fp);
    return 0;
}
2
  • int w[i] = f1[i] + f2[i] + f3[i];. What is that trying to do? Did you mean it to be instead: int w = f1[i] + f2[i] + f3[i]; Commented Mar 21, 2017 at 0:25
  • 1
    Or perhaps you meant to declare w at the top of the function as: long w[3]; and then within the loop: w[i] = f1[i] + f2[i] + f3[i]; Commented Mar 21, 2017 at 0:26

2 Answers 2

0

The compiler thinks you are trying to define an array of int of size "i", here:

int w[i] = f1[i] + f2[i] + f3[i];

The identifier "i" is clearly a dynamic value. So the array is dynamically sized. It can therefor not be initialised, which the compiler thinks you are trying.

My guess is that you want to fill an array with the calculated values. That array should be defined outside of the function (assuming you want to use the values later, which with this code inside "main" you seem not to want; but this probably only the minimal code for your posted question). You probably also want to make it at least long int, not int. If you do not want to use the values later, just print it to the file, then use

long int w = ...

instead.

Also, it seems to me that

fprintf(fp, "|  %d  |  %d  |  %d  | %ld | %ld | %ld |\n", c1, c2, c3, f1[i], f2[i], f3[i]);

is not matching what seems to be the column headers in your output file, according to

fprintf(fp, "|  c1  |  c2  |  c3  | value |\n");

which makes me expect printing of "w" instead of fn[i].

Sign up to request clarification or add additional context in comments.

1 Comment

@kaylum, your comment had the same info, sooner than my answer. Why not make it an answer? It could be accepted as the faster answer.
0

I would just like to add that f's are always 0 because on the first loop: c1 = 0, c2 = 0, c3 = 0 . So on all the subsequent ones you are just multiplying 0 * x = 0 . Might be better to start from 1 and handle the 0 cases before the loop.

But even in this case you will quickly run out of integer space and get unexpected values, so after 32 bit i believe you will start getting negatives and eventually when you use up all 64 it will be just 0s.

Also, i'm not sure that i understand exactly what you are trying to do. In case the f's are supposed to stay constant, you would need a new variable to save/print the multiplication result otherwise the f's are changing all the time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.