Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and exploring the core foundations for app libraries.

How do you do fractions in Pascal? Does it require a function if so how would it be written? Also how can I aternately add and subtract a series of numbers i.e. (1-3+5-7+9.....)?

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sum := 1;

pow:=1;

while(true) do begin

println(sum);

sum := sum + 2 * (-1)^pow;

pow:=pow+1;

end;

var

I, R: Real;

begin

I:= int(123.456);

R:= frac(123.456);

writeln(I, ' ', R);

end.

I returns 123; R returns 0.456

there you have it. The whole number on the left and the remainder on the right.

And to add a series of numbers is the same as 1+1. No different!!

writeln(1+8+5-3+9-8+2); { 13 }

that's it

I'll write a function for you then..

and Shlomoy! How do you get that code to work?

There is no built-in tools in pascal for handling fractions, although you can make it yourself.

In order to make it, you should declare a new type like this:

type

TFraction = record

num,den:longint

end;

and you should write some functions for common operators, such as addition of two fractions or addition of an integer and a fraction and so on (only write those which are necessary). some headers are:

procedure addtwofrac

(f1,f2:TFraction;

var res:TFraction);

procedure addinttofrac(i:integer;

f:TFraction;

var res:TFraction);

and be carefull. You should write your functions as efficient as possible; because in working with fractions, numerator and denomerator grow very fast and may overflow from a longint variable. As a simple solution to this problem you can frequently divide 'num' and 'den' to 'gcd(num,den)'----(gcd ::= greatest common divisor).

AND ABOUT the 2'nd question.

you can use at least two method to solve this problem:

First Method: you can do two operation(one summation and one subtraction) in the body of the loop.

Second Method: you can declare a simple variable, such as Mult, initiated to 1, and place a

Mult := Mult * -1;

in the body of the loop and then use Mult with the terms of that seri:

Sum := Sum + Mult * Term;

I am not so good in english writing. I hope my answer will be usefull for you.

Please give some points to me. :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialI forgot that in pascal it means "pointer".

Therefore it's wrong to use it the way I did.

let me try to give you that peace of code again:

sum:=0;

current:=1;

sign:=1;

while(true) do begin

println("current element: ", current);

sum:=sum+current;

println("current sum: ", sum);

current:= (current+2*sign)*(-1)*sign

sign:= sign*(-1);

end;

This is the correct code. Sorry for the previous stupid error I made.

Please try this code.

sum, current, sign are all Integers.

This is an infinite loop.

It will continue forever.

You can have the loop stop when you reach some known "current" or known "sum" or when you think that the abs(current) is bigger than some value you want.

Pascal

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

var

fraction : real;

the series can be printed like this:

sum := 1;

while(true) do begin

println(sum);

sum := sum + 2 * (-1)^(sum)

end;