Board index » delphi » New Student needing help!! PLease

New Student needing help!! PLease

Dear Newsgroup,

I am a new student and I need help my teacher has assigned a program
assignment.  The assignment is to write a program that gives a product
of an input number N!.  N! (pronounced N factorial) is the product of
all the numbers from one to N.  N being the input qunatity between 1 and
32.  I already have done the Var and Const's for the program and have an
 error message established for numbers greater than 32 and less than 1.
 I am having trouble doing the repetition loop.  Please help.  Here is
what I have .  If you can help please E-mail me at
warre...@accucomm.net.  
Program NFactorial

   VAR
     N,  {input term}
     NF: Real; {Product received from N!}

   BEGIN
       Writeln ('Please enter a value for N');
       Readln (N);

    If N>32 then
    Writeln ('Warning! Invalid input value.')
    else
    If N<1 then
    Writeln ('WArning! Invalid input value.')

Procedure Compute;
   Writeln ("this program computes the product of all numbers below N to
1');

THIS IS WHERE I GET STUCK.  I CAN"T GET AN EXECUTABLE STATEMENT LOOP.  
PROGRAM MUST BE LOOP AND REPETITION.

Thanks in advance.
S Warren (warre...@accucomm.net)

 

Re:New Student needing help!! PLease


Hi. I'm a bit hazy on what you meant by the product of everything under
that number so I'm assuming the number 7 would equal 1x2x3x3x4x5x6x7. If
I've misinterpreted this then ignore this method. Also your program only
displayed an error message if the input was out of range it didn't
actually correct the problem by getting new input.

uses crt;
var num, count, count2: integer;
    product: real;

begin
num := 0;
repeat
writeln('Enter a number between 1 and 32 -->');
readln(num);
if (num < 1) or (num > 32) then writeln('Out of range. Reenter');
until (num > 1) or (num < 32);

product := 0;
count := 1;
repeat
count2 := count + 1;
product := count * count2;
count := count + 1;
until count > num;
writeln('Product is ',product);
repeat until keypressed;
end.

--
The above-mentioned opinions are completely
those of my employers and my company. I am not
represented in any way and nothing should be
taken as my opinion.

Re:New Student needing help!! PLease


Amy McGowan <mcgow...@accucomm.net> wrote in article
<326D64AE....@accucomm.net>...

Quote
> Dear Newsgroup,

> I am a new student and I need help my teacher has assigned a program
> assignment.  The assignment is to write a program that gives a product
> of an input number N!.  N! (pronounced N factorial) is the product of
> all the numbers from one to N.  N being the input qunatity between 1 and
> 32.  I already have done the Var and Const's for the program and have an
>  error message established for numbers greater than 32 and less than 1.
>  I am having trouble doing the repetition loop.  Please help.  Here is
> what I have .  If you can help please E-mail me at
> warre...@accucomm.net.  

var
  total : longint;
  count, number : byte;

begin
  total := 0;
  write ('Enter number :');
  readln ( number);
  for count := 1 to number do
    total := total + count;
end.

--
-----------------------------------------------------------------
?? http://www.servcom.com/mrealm ??
??   mail to : mre...@servcom.com   ??
-----------------------------------------------------------------
'The one that squeaks the most gets oiled first!'

Re:New Student needing help!! PLease


   Since this is an housework, I can give you only one hint.  Take a look
to the definition of a Recursive function.

   Hope this can bring you to the good way.

Quote
Amy McGowan <mcgow...@accucomm.net> wrote:
>Dear Newsgroup,

>I am a new student and I need help my teacher has assigned a program
>assignment.  The assignment is to write a program that gives a product
>of an input number N!.  N! (pronounced N factorial) is the product of
>all the numbers from one to N.  N being the input qunatity between 1 and
>32.  I already have done the Var and Const's for the program and have an
> error message established for numbers greater than 32 and less than 1.
> I am having trouble doing the repetition loop.  Please help.  Here is
>what I have .  If you can help please E-mail me at
>warre...@accucomm.net.
>Thanks in advance.
>S Warren (warre...@accucomm.net)

--

           ___                                              ___
           L_|_                                            _|_J
          ( -O>                                            <O- )
       ___//\J  __________________________________________  L/\\___
      //-,\    |                                          |    /,-\\
     || / \\   L   AVONTURE Christophe (c) AVC Software   J___// \ ||
   _ ''/\/ '---J    Christophe.Avont...@is.belgacom.be    L---' \/\'' _
  / \ //\\.    |__________________________________________|    .//\\ / \
 |_/\'/  ||                                                    ||  \'/\_|
      '   ||_       "Postings are personnal, and don't        _||   '
          |__)         reflect Belgacom's opinion"           (__|

               SWAG: http://www.gdsoft.com/swag/swag.html              

                        ftp://ftp.gdsoft.com/swag/

Re:New Student needing help!! PLease


In article <326F055E....@web.net.au>, sven...@web.net.au says...

Quote

>Hi. I'm a bit hazy on what you meant by the product of everything under
>that number so I'm assuming the number 7 would equal 1x2x3x3x4x5x6x7. If
>I've misinterpreted this then ignore this method. Also your program only
>displayed an error message if the input was out of range it didn't
>actually correct the problem by getting new input.

>uses crt;
>var num, count, count2: integer;
>    product: real;

>begin
>num := 0;
>repeat
>writeln('Enter a number between 1 and 32 -->');
>readln(num);
>if (num < 1) or (num > 32) then writeln('Out of range. Reenter');
>until (num > 1) or (num < 32);

-snip!-

Ain't recursion wonderful?

- ralph forsythe
ra...@contact-paging.com

Re:New Student needing help!! PLease


Quote
AVONTURE Christophe wrote:
>    Since this is an housework, I can give you only one hint.  Take a look
> to the definition of a Recursive function.

>    Hope this can bring you to the good way.
> >I am a new student and I need help my teacher has assigned a program
> >assignment.  The assignment is to write a program that gives a product
> >of an input number N!.  N! (pronounced N factorial) is the product of
> >all the numbers from one to N.  N being the input qunatity between 1 and
> >32.  I already have done the Var and Const's for the program and have an
> > error message established for numbers greater than 32 and less than 1.
> > I am having trouble doing the repetition loop.  Please help.  Here is
> >what I have .  If you can help please E-mail me at

I wouldn't have thought that recursion was the way to do this.  I'd use
for..next, cos it's a lot easier on the stack.

Paul

**********************************************
* http://pallando.trin.cam.ac.uk/users/pdm23 *
* mailto:pd...@cam.ac.uk                     *
**********************************************

Re:New Student needing help!! PLease


: > I am a new student and I need help my teacher has assigned a program
: > assignment.  The assignment is to write a program that gives a product
: > of an input number N!.  N! (pronounced N factorial) is the product of

Sounds to me like you want to use recursion.

Try using something like this but also try reading your text books
because this is hardly learning...

---begin code snippet---

program factorial_program (input, output);

uses wincrt;   {remove this line if you are not using TP 1.5 for Windows}

var answer, number : integer;

function factorial(n : integer) : integer;
begin
    if n = 1 then Factorial := 1
             else Factorial := n * Factorial(n-1)
end; {of function factorial}

begin
write('Input number to process: ');
readln(number);
answer := factorial(number);
writeln(answer, '   - done.');
end. {of program factorial_program}

---end code snippet---

I didn't account for your limiting of input to 32 and there is no
checking for reals, negatives, etc. so this is not a very robust function
but it does work with acceptable input.

This function is using recursion (note it calls itself from within the
function) which you should find quite helpfull as your experiecne with
programming grows.  Another method of doing this could have involved a
loop of sorts which would have only changed the function.  The following
is an example of that:

function factorial(n : integer) : integer;
var h, i : integer;
BEGIN
h := 1;
for i := 1 to n do
    h := h * i;
factorial := h;
END; {of function factorial}

as you can see this produces the factorial by just stepping through the
for loop the number of times as you specify (the factorial to be found)
and just multiplies each number onto each other until the end is found.  
This will, as I stated earlier, fail under many circumstances (reals,
negatives, etc) but the idea is there.

I don't think either of these has errors but I did not test them so if
any are found please post a followup message so we can all see the
solution as it should be.  :)

: var
:   total : longint;
:   count, number : byte;
:  
: begin
:   total := 0;
:   write ('Enter number :');
:   readln ( number);
:   for count := 1 to number do
:     total := total + count;
: end.

Hrmm ... I don't know who wrote this code (the origional poster or
another followup) but it won't work.  Perhaps you don't understand what
needs to be found.  In your example you are just adding the loop index
'count' onto the 'total' which is compunding.  Actually since you never
gave count a value you are really just adding 0 to all the values of
count in 1 to number.  By this method the factorial of 5 would be 15, not
the correct 120.

Do notice though the similarities from what was done here to the second
function I presented.

I will stop now.

PS - again ... please make sure you understand what went on here and
don't just copy the code.  You will have learned nothing and accomplished
less.  I, nor the rest of the internet community, won't be there to help
you when it comes to test time. :)  Just a thought.

--
 .-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.
 | Robert Horvick - <rphor...@gloria.cord.edu> - Concordia College, MN - USA |
 | Computer Science & Psychology - http://cobweb.cord.edu/horvick    [KaNiN] |
 `-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-.-_-'

For every credibility gap, there is a gullibility fill.
                -- R. Clopton

Other Threads