简体   繁体   English

取一个结构体的数组元素

[英]taking the elements of array whitin a structure

here is my code, as you can see I used arrays of structures.这是我的代码,你可以看到我使用了结构数组。 The question is how should I take the arrays element by user directly?问题是我应该如何直接由用户获取数组元素? You can see below how the elements are given to the program before execution.您可以在下面看到在执行之前如何将元素提供给程序。

any help will be appericiated.任何帮助都会得到帮助。

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

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

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