Recent

Author Topic: Array analysis  (Read 4621 times)

Isshen

  • Newbie
  • Posts: 2
Array analysis
« on: October 16, 2018, 11:24:52 am »
I have an array of numbers that I can enter at random
How can I analyze the array to display how many positive, negative, highest number, lowest number and zeroes are there

balazsszekely

  • Guest
Re: Array analysis
« Reply #1 on: October 16, 2018, 11:39:18 am »
Try this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   I: Integer;
  4.   Numbers: array of Integer;
  5.   Neg, Pos, Zero: Integer;
  6.   Highest, Lowest: Integer;
  7. begin
  8.   //random values
  9.   SetLength(Numbers, 5);
  10.   Numbers[0] := 4; Numbers[1] := -3; Numbers[2] := 10; Numbers[3] := -6; Numbers[4] := 0;
  11.  
  12.   Neg := 0; Pos := 0; Zero := 0;  Lowest := High(Integer); Highest := Low(Integer);
  13.   for I := Low(Numbers) to High(Numbers) do
  14.   begin
  15.     case Numbers[I] of
  16.       Low(Integer)..-1: Inc(Neg);
  17.                      0: Inc(Zero);
  18.       1..High(Integer): Inc(Pos);
  19.     end;
  20.     if Numbers[I] > Highest then
  21.       Highest := Numbers[I];
  22.     if Numbers[I] < Lowest then
  23.       Lowest := Numbers[I];
  24.   end;
  25.   ShowMessage('Negative: ' + IntToStr(Neg) + sLineBreak +
  26.               'Positive: ' + IntToStr(Pos) + sLineBreak +
  27.               'Zero:     ' + IntToStr(Zero) + sLineBreak +
  28.               'Highest:  ' + IntToStr(Highest) + sLineBreak +
  29.               'Lowest:   ' + IntToStr(Lowest));
  30. end;
  31.  

Edited: See @440bx suggestion below.
« Last Edit: October 17, 2018, 06:13:18 am by GetMem »

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Array analysis
« Reply #2 on: October 16, 2018, 11:40:00 am »
Set up counter variables for positive, negative and zero values (npos, nneg, nzero). Set a temporary variable (currmin) for the minimum larger than the highest value expected (or max float or max integer), and set a temporary variable for the maximum (currmax) smaller than the smallest value exected. Then iterate through the array value by value in a for loop and do these checks:
* if the current array value is positive increment the positive counter by 1 (e.g. --> if array[index] > 0 then inc(npos))
* if the current array value is negative increment the negative counter by 1
* if the current array value is zero increment the zero counter by 1
* if the current array value is larger than the temporary maximum variable set that to the current array value (e.g. if array[index] > currmax then currmax := array[index])
* if the current array value is smaller than the temporary minimum variable set that to the current array value.

I will not post ready-to-use code here, because this question sounds like some homework assignment, and you only can learn coding by doing it yourself.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Array analysis
« Reply #3 on: October 16, 2018, 02:38:59 pm »
- Sort the array first: TArray<integer>.sort(); // is quicksort O(log N)
- Lowest is element zero, highest is element high(array) // is a single operation both are O(1)
- Find element zero. // this is O(log N) for a sorted array
- anything below zero is the - count (first zero - 1) // note index start from zero you may have to add 1 So index-1 is the count O(1) as I wrote
- skip all zero's
- anything above the last zero is the + count (array size - last zero ) // see above but sizeof(array)-index+1 is the count . O(1) as I wrote
- anything in between is the zero count. which equals positive - minus....This is O(1) too...

Note this assumes there are negative and zero and positive values: you have to test that (after the sort).
But this algorithm can find all necessary values that you defined in O(log N). which is in theory the fastest for this problem.

This is probably faster. But what wp said is a good exercise too. wp's algorithm is linear, though.
There are also neat, fancy ways to do half of this with bit-twiddling, but....
« Last Edit: October 16, 2018, 03:42:43 pm by Thaddy »
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Array analysis
« Reply #4 on: October 16, 2018, 03:41:53 pm »
@GetMem,

Nice, simple, efficient and well written algorithm.

It needs a couple of small changes to ensure it always works correctly.  The variable lowest should be initialized not to zero but the highest possible integer and, the variable highest should be initialized to the lowest possible integer.  That's the only way to guarantee they will always take their final value from the array of integers, whatever range they may fall in.


(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Array analysis
« Reply #5 on: October 16, 2018, 03:44:42 pm »
@GetMem,

Nice, simple, efficient and well written algorithm.
Yes. but leave out efficient. (wp knows that: he gave an educational example)
It is much slower than the theory..... <grin  >:D >:D >:D >:D >
Your lack of knowledge lets you down sooo often... Just teasing, you know by know. :D
« Last Edit: October 16, 2018, 03:46:22 pm by Thaddy »
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Array analysis
« Reply #6 on: October 16, 2018, 04:02:32 pm »
@thaddy: you are so pathetic.  I knew you were going to barf something and, you did.  You really are quite amusing.

Try to work on not being grumpy so often.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Array analysis
« Reply #7 on: October 16, 2018, 04:35:01 pm »
No, but you are afraid or incapable to test my suggestions... Then you would see mine is faster... Your laziness amazes me...
Comment with merit, not with prejudice...
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Array analysis
« Reply #8 on: October 16, 2018, 04:37:24 pm »
Comment with merit, not with prejudice...
you'd improve a great deal if you followed your own advice.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Array analysis
« Reply #9 on: October 16, 2018, 08:13:05 pm »
Then you would see mine is faster...
@Thaddy, you made a little mistake. Sort is O(n*logN), not O(log N). In better case (alas, quick sorting does not guarantee even this). But @GetMem and @wp solution only O(n). Of course, their solution is faster.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Array analysis
« Reply #10 on: October 16, 2018, 08:19:03 pm »
ASerge, you also didn't test...?
And you are mistaken about O notation although I stand corrected for your quadratic. Which means my solution should be even faster  :D
The solutions of e.g. wp are O(N), my solution is O(log N) since its boundaries are O(1) and the sort is..... Don't play games.
« Last Edit: October 16, 2018, 08:22:33 pm by Thaddy »
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Array analysis
« Reply #11 on: October 16, 2018, 08:48:26 pm »
And you are mistaken about O notation although I stand corrected for your quadratic. Which means my solution should be even faster
From Sorting algorithm: "For typical serial sorting algorithms good behavior is O(n log n)".
And you didn't provide any code that could be tested.

balazsszekely

  • Guest
Re: Array analysis
« Reply #12 on: October 17, 2018, 06:09:17 am »
@440bx
Quote
It needs a couple of small changes to ensure it always works correctly.  The variable lowest should be initialized not to zero but the highest possible integer and, the variable highest should be initialized to the lowest possible integer.  That's the only way to guarantee they will always take their final value from the array of integers, whatever range they may fall in.
Correct. I wrongly assumed that is always a mix of positive and negative numbers, which of course is not necessarily true. Thanks for catching that.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Array analysis
« Reply #13 on: October 17, 2018, 06:54:12 am »
Thanks for catching that.
You're welcome.  Glad to help.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018