Board index » delphi » Need help real bad with Pascal

Need help real bad with Pascal

hi all!
how can I write a program that reads 10 integers and keeps track of how
many of them have the same value as their position.Also use no more than
one if statement.
Thanks
JL

 

Re:Need help real bad with Pascal


Quote
DarkMan40 wrote:

> hi all!
> how can I write a program that reads 10 integers and keeps track of how
> many of them have the same value as their position.Also use no more than
> one if statement.
> Thanks
> JL

Hi,

I'd be very surprised if this question isn't a classrooom assignment.
Therefore, I won't write your code for you, but I will give you a few
pointers.

In the future, please post some of your own code to demonstrate that
you have already attempted the program on your own.  Otherwise, you
may get no help at all.  In general, posting homework assignements
on usenet news groups is a bit suspect.  As it is, you've given no
indication of which areas of the program are causing you  trouble.
Therefore my comments are written under the assumption that you are
a total beginner.

1) Since you need to read 10 different integers, you could write
   10 different read statements, or you could use one of Pascal's
   built-in looping constructs: FOR-TO-DO, WHILE-DO, and
   REPEAT-UNTIL.  Each loop type is designed for different purposes.

   FOR-TO-DO
      Use when the exact number of iterations is known in advance.

   WHILE-DO
      Use this type when the exact number of iterations is not known
      in advance, AND you may not even want to execute the loop at all.
      The loop exit condition is at the start of the loop, so the loop
      may not even be executed ata all.

   REPEAT-UNTIL
      Use this loop type when the exact number of iterations is not
      known, AND you want the loop to always execute at least once.
      The loop exit condition is at the end, so the loop will always
      execute at least once.

   If I were to write this program, there is no question that I would
   use a loop instead of writing multiple read statments.  It's your
   choice, though.  Would you rather look like a programmer, or a
   complete neophyte?  If you choose the loop, you'll need to decide
   which loop construct to use.  Do not proceed until you can read
   each integer without difficulty.

2) Next, you will need to check each integer to see whether
   it's value matches the order in which it was read.  Obviously
   this requires a decision, so the IF-THEN construct will be needed.
   You have a requirement to use only one IF-THEN statement, yet
   you must check all 10 integers.  Therefore, you'll need to execute
   the same IF-THEN statement multiple times (once for each integer).
   Do you know of any way to execute a single statement multiple times?
   Is there anything in the code you've written so far that already
   executes multiple times?  If so, could the IF-THEN statement be
   added to the code so that it executes multiple times too?

3) Finally, you need to keep track of the count of integers that match.
   You will need a counter variable which can be incremented each time
   you encounter a match.

You haven't stated that you need to keep the integers around for later
use.  Therefore, the method I've suggested does not facilitate this.  If
you need to use each integer later, you'll need to store them in a variable
after you read them in.  An array will work best here.

Hope this helps, without actually giving the solution away.
Craig

--
-----------------------------------------------------------------------------
Craig Jackson               "No matter how great your triumphs or how
cjack...@cmsa.gmr.com        tragic your defeats -- Approximately one billion
Electronic Data Systems      Chinese couldn't care less."    -  Lazlo

Re:Need help real bad with Pascal


Quote
DarkMan40 <jlo...@popalex1.linknet.net> wrote:
>hi all!
>how can I write a program that reads 10 integers and keeps track of how
>many of them have the same value as their position.Also use no more than
>one if statement.
>Thanks
>JL

Program Dole_in_96;
uses crt; var x,y,z:integer;
begin  z:=0; for x:=1 to 10 do begin read (y); if (y=x) then z:=z+1;
end;
writeln('The input integer and position were equal ',z,' times.');end.

See if this works. I'm kinda new to programming, too. It's a blast.
Good luck.    Mudgeo19

Re:Need help real bad with Pascal


Quote
> how can I write a program that reads 10 integers and keeps track of how
> many of them have the same value as their position.Also use no more than
> one if statement.

   Code your homework assignment for you, you want us to?  Haven't you
given ANY thought to (1) how to approach the problem or (2) some code to
get started with this?  I can recall at least 2 others who've posted this
_identical_problem_, and if it weren't obviously a class assignment from
the way you expressed (copied) it, its repeated nature sure makes this
clear.
   If you truly want our help, can't you demonstrate what you've done to
this point that isn't working or making sense, to which we _will_ be glad
to aid you.  But we WON'T do you homework for you (particularly for such
a trivial problem - you ought to be able to do _something_ with it...)!

Other Threads