Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
please help!! Posted by pixie123 on 8 Feb 2012 at 11:32 PM
hello everyone! i wanted to know if enyone can help me with this: i am learning pascal basics in school so i have one little problem with this:

i want to make programm where the 'x' value type user, and then the sum is calculated, but i dont know how to make formula for this

(1/2)+(3/2^2)+(5/2^3)+...+((2x-1)/2^x)

so the formula is ((2x-1)/2^x); for example: user types that x is 3;
then the programm puts 3 in x place; then 2; and then 1 and then all this sum together like this example where x is 3
((6-1)/8) + ((4-1)/4) + ((2-1)/2) = ...

so i want to create summing like this, i think it is something related with cycles, but i dont know for sure, so if anyone can help me i'll be grateful :)
Report
Re: please help!! Posted by dragon6158 on 9 Feb 2012 at 6:39 AM
What you are trying to do is called recurrsion where you loop calling the same function until the variable reaches a set point.
Report
Re: please help!! Posted by Actor on 10 Feb 2012 at 4:32 PM
It's called iteration, not recursion.

Report
Re: please help!! Posted by pixie123 on 12 Feb 2012 at 10:54 PM
ok, thanks, i wll try to find information about iteration, how to use it; but for now i have got this far:

program number;
var i,x,s:integer;
begin
writeln('type number');
readln(x);
s:=0;
for i:=1 to x do
s:=s+((2*i-1)/_____);

writeln('sum:',s);
readln;
end.

everything works luckily, but i dont know how to add 2^x where i have this underline, i even read that in pascal you cant do the multiplication involution, is that true?
Report
Re: please help!! Posted by Actor on 13 Feb 2012 at 9:58 AM
: ... i even read that in pascal you cant do the
: multiplication involution, is that true?
:

It's been a long time since I've heard the word involution.

The answer is yes and no. You can do involution but Pascal does not provide an operation for it the way Fortran and Basic do. You have to write your own routine, like below.
program number ;

      function power (x : real ; n : integer) : real ;
      {
         raise a real number to an integer power   
      }
      var
         product : real ;
         i       : integer ;
      begin
         product := 1.0 ;
         for i := 1 to n do
            product := x * product ;
         power := product
      end ;

var
   i,x : integer ;
   s   : real ;

begin
   writeln('type number') ;
   readln(x) ;
   s := 0 ;
   for i := 1 to x do
      s := s + ((2*i-1) / power(2,i)) ;
   writeln ('sum:', s) ;
   readln
end.

BTW, you can't do this with integers. Try it. s has to be real, otherwise the answer will always be zero. Do you know why?

Report
Re: please help!! Posted by pixie123 on 13 Feb 2012 at 11:38 PM
i think that because integers are 5, 6, 99... but real is 88,99; 0,000003; 1,5 ... and when i am going to divide numbers i wont get integer, so i need real numbers

and big thanks to you :) :* :D



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.