Please convert this c program into matlab code
2 次查看(过去 30 天)
显示 更早的评论
#include<stdio.h> #include<math.h> int main() { float x[10],y[15][15]; int n,i,j; printf("Enter n : "); scanf("%d",&n); printf("X\tY\n"); for(i = 0;i<n;i++){ scanf("%f %f",&x[i],&y[i][0]); } //forward difference table for(j=1;j<n;j++) for(i=0;i<(n-j);i++) y[i][j] = y[i+1][j-1] - y[i][j-1]; printf("\n***********Forward Difference Table *********\n"); //display Forward Difference Table for(i=0;i<n*2-1;i++) { int indx = i/2; printf("\t"); if(!(i%2)) printf("%.2f",x[indx]); int j_max = (n>i)? i : n*2-i-1; for(j=0;j<=j_max;j++) { printf("\t"); if(i%2 == j%2) printf("%.2f",y[indx-j/2][j]); } printf("\n"); }
return 0; }
0 个评论
回答(2 个)
Walter Roberson
2020-11-26
That code cannot be converted to MATLAB: it depends on standard input for reading from the user, but MATLAB does not have standard input.
4 个评论
Walter Roberson
2020-11-26
OFFSET = 1;
for i=0 : n*2-2
indx = floor(i/2);
fprintf("\t");
if mod(i,2) == 0
fprintf("%.2f",x(indx+OFFSET));
end
if n > i
j_max = i;
else
j_max = n*2-i-1;
end
for j = 0 : j_max
fprintf("\t");
if mod(i,2) == mod(j,2)
fprintf("%.2f", y(indx-j/2+OFFSET, j+OFFSET));
end
printf("\n");
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Foundation and Custom Domains 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!