从一个2D数组(数组[j][i])中输出的值,在Excel中形成一个包含i列和j行的表。

时间:2022-07-08 21:31:37

I have written a program that models heat flux through a 1D solid. It produces a value for the flux at all distances, x and then increments the time and repeats. My problem now is that I would like to write the results to Excel in a format that produces a basic table with the variation in distance running horizontally and the variation in time running vertically.

我已经写了一个程序,通过一个1D固体来模拟热通量。它对所有距离的通量产生一个值,然后增加时间和重复。我现在的问题是,我想把结果写在Excel表格中,它可以生成一个基本表格,它可以在水平方向上的变化和垂直运行时间的变化。

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

float fluxfunction(int x,int t)
{
    int n, L=10, q1=2, k = 5;
    float flux1=0, pi = 3.141592, exp = 2.718281;

    for(n=1;n<=1000;n++)
            {
                flux1+=q1*(2/(pi*n))*(cos(n*pi)-1)*(sin((n*pi*(x+L))/(2*L)))*pow(exp,-(k*t*(n*n*pi*pi)/(4*L*L)));
            }
            flux1+=q1;

    return flux1;
}


int main()
{
    int i, j, L=10,tmax=30;
    float f, results [tmax+1][L+1];
    FILE *fp;
    fp = fopen("fouriertest.csv", "w");

    for(j=0;j<=tmax;j++)
    {
        for(i=0;i<=L;i++)
        {
            results [j][i]=fluxfunction(i,j);
            if (i==L)
            {
                fprintf(fp, "%f\n",results [j][i]);
            }
            else
            {
                fprintf(fp, "%f",results [j][i]);
            }
        }
    }

    return 0;
}

This is what I've got so far, but unfortunately, where I try write the values for increasing i along a column, there is no cell separation. Shown by the picture here:

这是我目前为止所得到的,但不幸的是,我尝试在一个列中增加I的值,没有细胞分离。如图所示:

http://i.stack.imgur.com/rbN5y.png

http://i.stack.imgur.com/rbN5y.png

1 个解决方案

#1


0  

you have to separate field using ';' coma character; your main function should be like this:

你必须使用“;”昏迷字符;你的主要功能应该是这样的:

int main()
{
    int i, j, L=10,tmax=30;
    float f, results [tmax+1][L+1];
    FILE *fp;
    fp = fopen("fouriertest.csv", "w");

    for(j=0;j<=tmax;j++)
    {
        for(i=0;i<=L;i++)
        {
            results [j][i]=fluxfunction(i,j);
            fprintf(fp, "%f%c",results [j][i],(i==L)?'\n':';');

        }
    }
    fclose(fp);

    return 0;
}

#1


0  

you have to separate field using ';' coma character; your main function should be like this:

你必须使用“;”昏迷字符;你的主要功能应该是这样的:

int main()
{
    int i, j, L=10,tmax=30;
    float f, results [tmax+1][L+1];
    FILE *fp;
    fp = fopen("fouriertest.csv", "w");

    for(j=0;j<=tmax;j++)
    {
        for(i=0;i<=L;i++)
        {
            results [j][i]=fluxfunction(i,j);
            fprintf(fp, "%f%c",results [j][i],(i==L)?'\n':';');

        }
    }
    fclose(fp);

    return 0;
}