Board index » delphi » "odd" statement

"odd" statement

I am stuck, I am trying to get the odd integer only from a field 11 to
23, like this;

var Loop:integer;

procedure OddLoop (eleven:integer);
      begin
           eleven:=odd(eleven);
           writeln (eleven);
      end;
begin
   for Loop:=11 to 23 do begin
       OddLoop (Loop);
   end
end.

I've tried without procedures, different variables, etc... I've been
able to get all outputs, 11 thru 23, outputs of just 25, when doing a
(Loop + 2), and even a output of every sum of 23 + 13,(!), this is
suppose to be just a simple "for statement"

Thanks,

M

 

Re:"odd" statement


Quote
> I am stuck, I am trying to get the odd integer only from a field 11 to
> 23, like this;
> var Loop:integer;
> procedure OddLoop (eleven:integer);
>       begin
>            eleven:=odd(eleven);
>            writeln (eleven);
>       end;
> begin
>    for Loop:=11 to 23 do begin
>        OddLoop (Loop);
>    end
> end.

> I've tried without procedures, different variables, etc... I've been
> able to get all outputs, 11 thru 23, outputs of just 25, when doing a
> (Loop + 2), and even a output of every sum of 23 + 13,(!), this is
> suppose to be just a simple "for statement"

   Well, this is a homework assignment for sure, but you have at least
made an attempt to do it yourself - putting yourself light years beyond
the usual person who comes here "for" such help.  Accordingly, here is
some:

  for Loop := 11 to 23 do
    if Odd(Loop) then writeln (Loop)

   You see, Loop cycles/iterates from 11 through 23 (13 times), with Loop
containing each of the integers 11 through 23.  While in that loop, if
you test the value of Loop for being odd (with Odd(Loop)), every time
that expression is true, the writeln will be executed.  Conversely, when
Odd(Loop) is false, the writeln won't be executed.
   Note that Odd() produces a _boolean_result_ (true or false), not some
numeric value.  You use the Odd() function to tell you if some integer
value is odd, and the function will return true when that's so.
   Good luck with your class...

Re:"odd" statement


Quote
Mike Copeland wrote:

> > I am stuck, I am trying to get the odd integer only from a field 11 to
> > 23, like this;
> > var Loop:integer;
> > procedure OddLoop (eleven:integer);
> >       begin
> >            eleven:=odd(eleven);
> >            writeln (eleven);
> >       end;
> > begin
> >    for Loop:=11 to 23 do begin
> >        OddLoop (Loop);
> >    end
> > end.

> > I've tried without procedures, different variables, etc... I've been
> > able to get all outputs, 11 thru 23, outputs of just 25, when doing a
> > (Loop + 2), and even a output of every sum of 23 + 13,(!), this is
> > suppose to be just a simple "for statement"

>    Well, this is a homework assignment for sure, but you have at least
> made an attempt to do it yourself - putting yourself light years beyond
> the usual person who comes here "for" such help.  Accordingly, here is
> some:

>   for Loop := 11 to 23 do
>     if Odd(Loop) then writeln (Loop)

>    You see, Loop cycles/iterates from 11 through 23 (13 times), with Loop
> containing each of the integers 11 through 23.  While in that loop, if
> you test the value of Loop for being odd (with Odd(Loop)), every time
> that expression is true, the writeln will be executed.  Conversely, when
> Odd(Loop) is false, the writeln won't be executed.
>    Note that Odd() produces a _boolean_result_ (true or false), not some
> numeric value.  You use the Odd() function to tell you if some integer
> value is odd, and the function will return true when that's so.
>    Good luck with your class...

Thank you, I tried three or four different procedures to write this, I
knew I should use "odd()", butwas unsure of how to use a boolean call
for an integer value.
As far as class goes, this is a corespondance course so it's even more
difficult, if I was in a classroom, I could ask the professor, so I
think I feel humbled to use the newsgroup as a instructor, I hope thats
alright.  I will not ask for answers, but assistance or guidance will be
appreciated ;-)

M

Re:"odd" statement


On Thu, 11 Sep 1997 16:40:54 -0700, "Mark G. Franz"

Quote
<mark.g.fr...@cpmx.saic.com> wrote:
>var Loop:integer;

>procedure OddLoop (eleven:integer);
>      begin
>           eleven:=odd(eleven);
>           writeln (eleven);
>      end;
>begin
>   for Loop:=11 to 23 do begin
>       OddLoop (Loop);
>   end
>end.
>I am stuck, I am trying to get the odd integer only from a field 11 to
>23, like this;

"Odd" problems...actually, this code doesn't do what you desire to
begin with.  It writes all numbers to begin with whether odd or even
(at first glance, read on those who want to oh so prove me wrong!),
and as you said you only want odd numbers in your range....

the next thing is that you're using a stock system function which
returns literally a true or false, or a 0 or 1....you should be
getting outputs of 1's and 0's in some form, if not even compiler
errors for the code you posted....pretty far away from your original
stated purpose, if I recall.

First thing I'd say is go ahead and think about what you're trying to
do and do it on paper, write out the steps you go through...a good
rule to remember in any kind of programming is that computers are
completely dumb and you have to tell it everything you want it to do
to the letter.  Novice programmers often make very human assumptions
which are fine in dealing with other humans but fall flat in dealing
with the computer.

Free thought in logic:

1) go through all numbers 11 to 23 and pick out the odd numbers.
Might come out as:
for i := 11 to 23 do
  if odd(i) then
    writeln(i);

Looks OK, not quite as elegant as it can get, but it'll work....other
thoughts....

2)
i := 11;
 while i < 23 do
   begin
     writeln(i);
  inc(i,2);
end;

3) Use algebra...
for i := 1 to 11 do
  writeln(2*i+1);

Lots of combinations there to get your odd # set.

Re:"odd" statement


Many thanks to all who provided input into solving this problem, here is
the final solution;

program ForLoop11To23;  {Prints only the odd numbers between 11 & 23}

var Loop:integer;

begin
    for Loop:=11 to 23 do begin {Starts loop count at variable set
below, but I thought 'for' set initial conditions to equal '0' (?)}
          if odd(Loop) then     {Checks the variable for an odd true}
          writeln (Loop);       {If 'true' then output truth, reject all
else}
    end;
Loop:=11                        {Sets varible 'Loop' to equal 11}
end.

Please provide any responses that may clear up any "oddities" that may
create any bad habits in the future...

Thanks again,

M

Quote
Glenn Grotzinger wrote:

> On Thu, 11 Sep 1997 16:40:54 -0700, "Mark G. Franz"
> <mark.g.fr...@cpmx.saic.com> wrote:

> >var Loop:integer;

> >procedure OddLoop (eleven:integer);
> >      begin
> >           eleven:=odd(eleven);
> >           writeln (eleven);
> >      end;
> >begin
> >   for Loop:=11 to 23 do begin
> >       OddLoop (Loop);
> >   end
> >end.

> >I am stuck, I am trying to get the odd integer only from a field 11 to
> >23, like this;

> "Odd" problems...actually, this code doesn't do what you desire to
> begin with.  It writes all numbers to begin with whether odd or even
> (at first glance, read on those who want to oh so prove me wrong!),
> and as you said you only want odd numbers in your range....

> the next thing is that you're using a stock system function which
> returns literally a true or false, or a 0 or 1....you should be
> getting outputs of 1's and 0's in some form, if not even compiler
> errors for the code you posted....pretty far away from your original
> stated purpose, if I recall.

> First thing I'd say is go ahead and think about what you're trying to
> do and do it on paper, write out the steps you go through...a good
> rule to remember in any kind of programming is that computers are
> completely dumb and you have to tell it everything you want it to do
> to the letter.  Novice programmers often make very human assumptions
> which are fine in dealing with other humans but fall flat in dealing
> with the computer.

> Free thought in logic:

> 1) go through all numbers 11 to 23 and pick out the odd numbers.
> Might come out as:
> for i := 11 to 23 do
>   if odd(i) then
>     writeln(i);

> Looks OK, not quite as elegant as it can get, but it'll work....other
> thoughts....

> 2)
> i := 11;
>  while i < 23 do
>    begin
>      writeln(i);
>   inc(i,2);
> end;

> 3) Use algebra...
> for i := 1 to 11 do
>   writeln(2*i+1);

> Lots of combinations there to get your odd # set.

Re:"odd" statement


Quote
> Many thanks to all who provided input into solving this problem, here is
> the final solution;
> program ForLoop11To23;  {Prints only the odd numbers between 11 & 23}
> var Loop:integer;
> begin
>     for Loop:=11 to 23 do begin    {Starts loop count at variable set
> below, but I thought 'for' set initial conditions to equal '0' (?)}
>           if odd(Loop) then     {Checks the variable for an odd true}
>           writeln (Loop);       {If 'true' then output truth, reject all
> else}
>     end;
> Loop:=11                   {Sets varible 'Loop' to equal 11}

..^^^^^^^^                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  Why?
   Unless it was part of the assignment (and there's no apparent reason
for it to be), this statement is completely unnecessary.  It is important
to note that, following the termination of a For loop, the value of the
control variable ("Loop", in this case) is _undefined_.  It should not be
used for any purpose where a value is to be known (e.g. it's NOT 23, 24,
or anything you'd expect or hope it to be), and you should consider it to
be only an uninitialized variable at that point.
Quote
> end.

> Please provide any responses that may clear up any "oddities" that may
> create any bad habits in the future...

Re:"odd" statement


On Mon, 15 Sep 1997 11:25:41 -0700, "Mark G. Franz"

Quote
<mark.g.fr...@cpmx.saic.com> wrote:
>Many thanks to all who provided input into solving this problem, here is
>the final solution;

>program ForLoop11To23;  {Prints only the odd numbers between 11 & 23}

>var Loop:integer;

>begin
>    for Loop:=11 to 23 do begin     {Starts loop count at variable set
>  below, but I thought 'for' set initial conditions to equal '0' (?)}

Nope, sets it to whatever you say:

  "for Loop := x to y do"

will start Loop at any x and end at any y that are legitimate values
for Loop; it doesn't even have to be an integer, just a discrete
scalar type; e.g.:

var
  Loop : char ;
begin
  for Loop := 'A' to 'Z' do
...

Quote
>          if odd(Loop) then     {Checks the variable for an odd true}
>          writeln (Loop);       {If 'true' then output truth, reject all
>else}
>    end;
>Loop:=11                    {Sets varible 'Loop' to equal 11}

 ^^^^^^^ this is unnecessary, and with the program as-is effectively
dead code, you assign the variable and immediately the program ends.

Quote
>end.

>Please provide any responses that may clear up any "oddities" that may
>create any bad habits in the future...

>Thanks again,

>M

HTH

Stephen Posey
slpo...@concentric.net

Re:"odd" statement


In article <341df071.10886...@news.concentric.net> of Tue, 16 Sep 1997
02:41:16 in comp.lang.pascal.borland, Stephen Posey

Quote
<slpo...@concentric.net> wrote:
>Nope, sets it to whatever you say:

>  "for Loop := x to y do"

>will start Loop at any x and end at any y that are legitimate values
>for Loop; it doesn't even have to be an integer, just a discrete
>scalar type;

Just to plug a loophole, so to speak; it is [not always realised to be]
perfectly legitimate to have y less than x, but in that case Loop does
not end at y, and the [compound] statement after "do" is not executed.
And vice versa for downto.

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web URL: http://www.merlyn.demon.co.uk/ -- includes FAQqish topics and links.
  Y2Kish :-
  My list of Critical Dates is : http://www.merlyn.demon.co.uk/miscinfo.htm#CDs

Other Threads