Cant find the mistake.... - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Cant find the mistake....

Posts: 1Member
[code]program ocjena;
uses wincrt;
var
i, k, S, y, n:real;
begin
S:=0;
i:=1;
y:=i+1;
K:=1/i -1/y;
write ('Unesi krajnji broj: ');
for i:=1 to n do
for i:=1 to 10 do
begin
S:=S+K;
i:=i+1;
if (i=n) then
S:=S+1/i ;
if (y=n) then
S:=S-1/y;
end;
writeln ('S iznosi', S:2:2);
end.[/code]

This is my program which calculate something like that...or should be..:)
1/1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + 1/7 - 1/8... +- 1/n...
But i cant find out why it sais that for loop (only one) is not good....

• Posts: 268Member
: [code]: program ocjena;
: uses wincrt;
: var
: i, k, S, y, n:real;
: begin
: S:=0;
: i:=1;
: y:=i+1;
: K:=1/i -1/y;
: write ('Unesi krajnji broj: ');
: for i:=1 to n do [color=Red]<----------[/color]
: for i:=1 to 10 do [color=Red]<--- Same variable to control 2 loops[/color]
: begin
: S:=S+K;
: i:=i+1;
: if (i=n) then
: S:=S+1/i ;
: if (y=n) then
: S:=S-1/y;
: end;
: writeln ('S iznosi', S:2:2);
: end.[/code]:
:
: This is my program which calculate something like that...or should
: be..:)
: 1/1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + 1/7 - 1/8... +- 1/n...
: But i cant find out why it sais that for loop (only one) is not
: good....
:

[code][color=Blue]
program ocjena;

var i,n:word;
s:real;

begin
write('Unesi krajnji broj: ');readln(n); {<-- Dunno what it means}
s:=0;
if n>0 then
if n<2 then s:=1 else begin
s:=1;
for i:=2 to n do
if odd(i) then s:=s+1/i
else s:=s-1/i;
end;
writeln('S iznosi: ', s:2:6);
end.
[/color][/code]
• Posts: 268Member
More simplified

[code][color=Blue]
program ocjena;

var i,n:word;
s:real;

begin
write('Unesi krajnji broj: ');readln(n);
s:=0;
if n>0 then begin
s:=1;
for i:=2 to n do
if odd(i) then s:=s+1/i
else s:=s-1/i;
end;
writeln('S iznosi: ', s:2:6);
end.[/color][/code]