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?
4 replies to this topic
#1
Posted 03 February 2011 - 01:36 PM
|
|
|
#2
Posted 04 February 2011 - 04:04 PM
Well, first I would worry about getting the random numbers into the array. Do you have that part done?
#3
Posted 04 February 2011 - 04:06 PM
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).
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
Posted 06 February 2011 - 12:10 PM
Thanks for answer and sorry for incomplete question
No, random number array is not the problem
something with even and uneven numbers?
For now i got this
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
Posted 06 February 2011 - 01:50 PM
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.
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


Sign In
Create Account

Back to top









