将阵列的元素变为结构

taking the elements of array whitin a structure

本文关键字:结构 元素 阵列      更新时间:2023-10-16

这是我的代码,因为您可以看到我使用了结构数组。问题是我应该如何直接将数组元素直接获取?您可以在下面查看执行之前如何给出程序的元素。

任何帮助都将被申请。

#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;
}
#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;
}