pascal to C(polynomial)

please take a look at this
P(x) = Pn*xn + Pn-1*Xn-1 + ... + P0,(polynomial)
P0, P1, ..., Pn is given. find P(x+1)-P(x).(n ≤ 10)
print P0, P1, ..., Pn(P(x+1)-P(x))
here is the code in pascal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var n,i,k:longint;
    p,a:array[0..10] of double;
    c:array[0..10;0..10] of longint;
    s:double;
begin
 n:=-1;
while not eoln do
  begin
  inc(n);
  read(p[n]);
  end;
 for i:=0 to n do
  c[i,0]:=1;
 for i:=0 to n do
  c[i,i]:=1;
 for i:=2 to n do
  for k:=1 to i-1 do
  c[i,k]:=c[i-1,k-1]+c[i-1,k];
 fillchar(a,sizeof(a),0);
 for k:=0 to n do
 begin
   for i:=0 to k do
   begin
   a[i]:=a[i]+p[k]*c[k,i];
   end;
 end;
for i:=0 to n-2 do
 write((a[i]-p[i]):0:1,' ');
 writeln((a[n-1]-p[n-1]):0:1);
end.


I wrote it in C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<stdio.h>
#define MAX 10
double s,p[MAX],a[MAX];
long n,i,k,c[MAX][MAX];
int main() {
scanf("%1f ", &p[n]);
for(i=0; i<=n; i++) c[i][i]=1;
for(i=2; i<=n; i++)
for(k=1; k<=(i-1); k++) c[i][k]=c[i-k][k-1]+c[i-1][k];
 {  for(k=0;k<=n;k++)
  {  for(i=0; i<=k; i++)
    { a[i]=a[i]+p[k]*c[k][i]; }
  }
  for(i=0; i<=(n-2); i++)
  printf("%.1lf ",(a[n-1]-p[n-1]));
 }
}

it works, but doesn't print anything
it has to be
Input:
1 0 3 1

Output:
4.0 9.0 3.0

(oh sorry, for hurting your eyes)
Last edited on
n is never initialized as far as I can see. (when programming, the spacebar is your friend. trust me.)
wow...
try using the # button to the right of where you post
put the code between (code) and (/code).(except those are [ and ])
That way we can at least see some indenting.
It hurts my eyes just looking at those for loops
Last edited on
Also, please use some indentation or something...I can't even tell what is going on...
Last edited on
Topic archived. No new replies allowed.