Please convert this c program into matlab code

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

Réponses (2)

Walter Roberson
Walter Roberson le 26 Nov 2020

0 votes

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 commentaires

My guess at the formatting of the original code would be
#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;
}
(Remember that in C, there is no explicit end for equivalent unless you deliberately use { } to group statements, so the for i, for j, y[i][j] set of lines is a complete double-nested loop that does not extend further -- the printf after is not inside the loops.)
PULAK Kumer
PULAK Kumer le 26 Nov 2020
Modifié(e) : Walter Roberson le 26 Nov 2020
If this code was started from index 1(array index) instead of 1 then what will be done for the following part?
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"); }
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

Connectez-vous pour commenter.

What is the wrong for the following code:
clc;
clear all;
close all;
fprintf('\n************************************************************');
fprintf('\n****************** NIRALI PUBLICATIONS *********************');
fprintf('\n******** CONM by M. T. Puranik & V. N. Chougule ************');
fprintf('\n*** Interpolation by Newtons Forward Difference Formula ***');
fprintf('\n************************************************************');
n = input('\nEnter number of data points = ');
h = input('\nEnter step size (h) = ')
x(1) = input('\nx0 = ');
y(1) = input('y0 = ');
for i=2:n
x(i)=x(i-1)+h;
fprintf('\nX%d = %f',i,x(i));
fprintf('\t\tY%d: ',i);
y(i) = input('');
end
x_reqd = input('\nEnter X for which value of Y is sought: ');
s=(x_reqd-x(1))/h;
for i=1:n
diff(i,1)=y(i);
end
%% Calculate Forward Differance Table
for j=2:n
for i=1:n-j+1
diff(i,j)=diff(i+1,j-1)-diff(i,j-1);
end
end
fprintf('\n\t Forward Differance Table');
fprintf("\n");
fprintf("============================================================================")
fprintf("\n");
fprintf("x\t\t\ty\t\t\tdy");
for j=2:1:n-1
fprintf("\t\t\t");
fprintf("D%dy",j);
end
fprintf("\n");
fprintf("============================================================================")
%% Print Forward Differance Table
offset=1;
for i=0:n*2-2
index=floor(i/2);
fprintf("\t\t")
if mod(i,2)==0
fprintf('\n %.6f',x(index+offset));
end
if n>i
j_max=i
else
j_max=n*2-i-1
end
for j=0:j_max
if mod(i,2)==mod(j,2)
fprintf('\t\t%.6f',diff(index-j/2+offset,j+offset));
end
end
end

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by