2017-03-17 60 views
0

我已經使用靜態數組創建了前向差分表,但無法通過使用動態數組解決它,請幫我解決此表。使用動態數組的C++正向差分表

這是靜態數組的代碼,但我需要它與動態數組

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


} 
+0

你有什麼tried.please添加代碼 – IsuruAb

+0

你應該添加* real *代碼。 'floatarray [20] [20];'? – crashmstr

+0

另外:數組不是* dynamic *,但可以動態分配。 'std :: vector'是動態的。 – crashmstr

回答

0

如何初始化二維數組:

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