Jump to content

Array in Pascal

- - - - -

  • Please log in to reply
4 replies to this topic

#1
berserks

berserks

    Newbie

  • Members
  • Pip
  • 2 posts
Hi i'm using freepascal
I need to generate array with random numbers, detect saw-like pattern, write lengt of it and each element of it.
saw-like pattern is like 1, 2, 1, 5, 3, 4, 2 because 1<2>1<5>3<4>2
2, 3, 3, 4,1 is not because 2<3=3>4>1

any advice how to make him do that?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Well, first I would worry about getting the random numbers into the array. Do you have that part done?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
Hello, welcome to the forum!

Well, my first advice is to start writing your code. I could not see any problem here. You have got all the algorithm covered. So, what is your problem? If you want some pointers, I'll try to give you some "random" hints (since I don't really know your where you are stuck).

  • Check out Random, Randomize, and RandSeed to generate random numbers.
  • Looks like for do loop is your friend here to iterate the array
  • Check out Break, to immediately get out of a loop once you detect certain condition occurred.


#4
berserks

berserks

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks for answer and sorry for incomplete question
No, random number array is not the problem
Program zagis;

Uses Crt;

Var A:Array[1..40] of Integer;

          i, j, temp, k:Integer;

Begin

ClrScr;

Randomize;

For i:=1 to 40 do A[i]:=Random(10);

For i:=1 to 40 do Write(A[i]:3);

Readln;
Its detecting that pattern
something with even and uneven numbers?
For now i got this
For i:=1 to 20 do

 if (A[2*i-1]<A[2*i]) and (A[2*i]>A[2*i+1]) then

 write(A[2*i-1], ' ', A[2*i], ' ') else writeln('nav zagis');
at least something :)

#5
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
If it's me, I will not worry about even and uneven numbers/index. Just iterate through all members of the array, when comparing use a flag to detect whether at that time the current array member should be larger of smaller than the previous one. If the condition does not meet, then Break out. If the condition met, toggle that flag so the next comparison will use the opposite term (from larger into smaller or vice versa).

Note that since in the first testing both condition (larger or smaller) will satisfy the test, you could not simply use a boolean. You must either use a tristate (use enumeration type for this) or use another boolean flag to accompany the main flag should you use boolean for the main flag.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users