简体   繁体   English

在C中将多个数组的元素添加到新数组中

[英]Adding elements of multiple arrays into a new array in C

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. 同样,无论数组的值或c的值是多少,乘法总是产生0。 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;
}

The compiler thinks you are trying to define an array of int of size "i", here: 编译器认为您正在尝试在此处定义大小为“ i”的int数组。

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

The identifier "i" is clearly a dynamic value. 标识符“ i”显然是一个动态值。 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). 该数组应在函数外部定义(假设您以后想使用这些值,而您似乎不希望将这些代码放在“ main”中;但这可能只是您发布的问题的最少代码)。 You probably also want to make it at least long int, not int. 您可能还希望至少将其设为int,而不是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]. 这使我期望打印“ w”而不是fn [i]。

I would just like to add that f's are always 0 because on the first loop: c1 = 0, c2 = 0, c3 = 0 . 我只想补充一点,因为总是在第一个循环上,f始终为0:c1 = 0,c2 = 0,c3 = 0。 So on all the subsequent ones you are just multiplying 0 * x = 0 . 因此,在所有后续值上,您只需乘以0 * x = 0即可。 Might be better to start from 1 and handle the 0 cases before the loop. 最好从1开始并在循环之前处理0种情况。

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. 但是即使在这种情况下,您也将很快用尽整数空间并获得意外的值,因此在32位之后,我相信您将开始获得负数,最终当您用完所有64时,它将仅为0。

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. 如果f应该保持恒定,则需要一个新变量来保存/打印乘法结果,否则f一直在变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM