使用动态数组使用 c++ 的前向差异表

Forward Difference table with c++ using Dynamic Array

本文关键字:动态 数组 c++      更新时间:2023-10-16

>我使用静态数组创建了前向差分表,但无法使用动态数组解决它,请帮助我解决此表。

这是带有静态数组的代码,但我需要动态数组的代码

#include<iostream>
int main()
{
    inti,noOfDifferenceColumns;
    //j is no of columnds. Initially it is 2. One for x, and other for f(x).
    int j=2;
//std::cout<< "Hello World!n";
//std::cout<<"Hello Hello";
    std::cout<<"Enter no of values for x: ";
    std::cin>>i;
    std::cout<<"Enter no of difference tables to be created: ";
    std::cin>>noOfDifferenceColumns;
    j+=noOfDifferenceColumns;
    floatarray[20][20];
    //Entering values of x 
    std::cout<<"Enter values for x: n";
    for(int a=0;a<2;a++)
    {
        for(int b=0;b<i;b++)
        {
            `enter code here`std::cin>>array[b][a];enter code here
            `enter code here`std::cout<<"n";
        }
        if(a==0)std::cout<<"Enter value for f(x): n";
    }
    std::cout<<"nPrinting values: ";
    std::cout<<"nPrinting x and f(x): ";
    //condition is c<2 as the outer loop will run twice. Once to print x and then to print f(x)
    for(int c=0;c<2;c++)
    {
        if(c==0) std::cout<<"X"<<"n";
        elsestd::cout<<"F(X)"<<"n";
        for(int d=0;d<i;d++)
        {
            std::cout<<array[d][c]<<"n";
        }
    }
    std::cout<<"Printing Difference Tables: n";
    int g=i-1; 
    for(int e=2;e<j;e++)
    {
        std::cout<<"Printing Difference Column: "<<e-1<<"n";
        for(int f=0;f<g;f++)
        {
            array[f][e]=array[f+1][e-1]-array[f][e-1];
            std::cout<<array[f][e];
            std::cout<<"n";
        }
        g--;
    }

}

如何初始化二维数组:

float** farray;
farray = new float*[c];
for (int ind = 0; ind < c; ind++) farray[ind] = new float[r];