将阵列白化的元素结构化

时间:2019-06-25 08:39:27

标签: c++ arrays structure

这是我的代码,如您所见,我使用了结构数组。问题是我应该如何直接由用户使用arrays元素?您可以在下面看到执行之前如何将元素赋予程序。

将提供任何帮助。

#include<iostream>
#include<math.h>

using namespace std;

struct Data
{
    int x, y;
};

double interpolate(Data f[], int xi, int n)
{
    double P = 0; 
    for (int i = 0; i<n; i++)
    {
        double p= f[i].y;
        for (int j = 0;j<n;j++)
        {
            if (j != i)
                p =p*(xi - f[j].x) / (f[i].x - f[j].x);
        }
        P += p;
    } 
    return P;
}

int main()
{
    Data f[] = { { 0,2 },{ 1,3 },{ 2,12 },{ 5,147 }};

    cout << "Value of f(3) is : " << interpolate(S, 3, 4) << endl;
    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:0)

#include<iostream>
#include<math.h>

using namespace std;

struct Data
{
    int x, y;
};

double interpolate(Data f[], int xi, int n)
{
    double P = 0; 
    for (int i = 0; i<n; i++)
    {
        double p= f[i].y;
        for (int j = 0;j<n;j++)
        {
            if (j != i)
                p =p*(xi - f[j].x) / (f[i].x - f[j].x);
        }
        P += p;
    } 
    return P;
}

int main()
{
    int input[8];

    cout << "Enter first value: \n";
    cin >> input[0];
    cout << "Enter second value: \n";
    cin >> input[1];
    cout << "Enter third value: \n";
    cin >> input[2];
    cout << "Enter fourth value: \n";
    cin >> input[3];
    cout << "Enter fifth value: \n";
    cin >> input[4];
    cout << "Enter sixth value: \n";
    cin >> input[5];
    cout << "Enter seventh value: \n";
    cin >> input[6];
    cout << "Enter eighth value: \n";
    cin >> input[7];

    Data f[] = {{input[0], input[1]}, {input[2], input[3]}, {input[4], input[5]}, {input[6], input[7]}};

    cout << "Value of f(3) is : " << interpolate(f, 3, 4) << endl;
    system("pause");
    return 0;
}