Recent

Author Topic: So... I'm back  (Read 4041 times)

guest58006

  • Guest
So... I'm back
« on: October 23, 2015, 09:50:12 pm »
So far so good, you've helped me a lot guys. Really want to thank you. All your advice has helped me a lot through the next subjects.
But since I have these 'shiny' moments, I got stuck at an exercise i've been assigned for home.
So basically what I have to do is have an array , or how's it called , basically this :
Code: Pascal  [Select][+][-]
  1. type t=array [1..100] of integer;
(input - keyboard) and I have to find the biggest sequence of zeroes  (I have to input them myself, but the computer will compare the sequences (quantity of zeroes) and find out which sequence has the biggest quantity of all.), basically - let's say I introduce my numbers : 1,5,156,321,0 (here it starts counting),0,0,0,0,0,0,0,0,0,0,20(here it stops counting).. so the sequence here has 11 zeroes. Other one will have 9, other 20 blah blah blah and it will continue until I hit 100 values introduced. THEN it will tell me which sequence has the biggest number of zeroes..
Here is my code if anyone is interested :
Code: Pascal  [Select][+][-]
  1. type t=array [1..100] of integer;
  2.  var
  3.     a:t;
  4.     n,i,nr1,nr2,secmax,sec1,sec:integer;
  5.  begin
  6.   writeln ('Introduce the number of elements');
  7.   readln (n);
  8.  for i:=1 to n do
  9.  begin
  10.  writeln ('Introduce the element nr.', ' ', i);
  11.  readln (a[i]);
  12.  end;
  13.  secmax:=sec1;
  14. for i:=1 to n do
  15.   begin
  16. if (a[i]=0) then
  17. inc(nr1) else
  18.   end;
  19. for i:=i to n do
  20.   begin
  21. if (a[i]=0) then
  22. inc(nr2) else
  23.   end;
  24. if nr1>nr2 then nr1:=seci else nr2:=seci; // here I'm trying to use sec(i) as sec1,sec2,sec3 so that 'i' will always increase so I compare new values, instead of using the old ones..
  25. if (seci>secmax) then secmax:=seci;
  26.  

Your help would be really appreciated as I'm really stuck here and idk what to write.

Thanks in advance!
-Aerowei

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: So... I'm back
« Reply #1 on: October 23, 2015, 10:17:38 pm »
It might help to plan in pseudocode first.
Then try to code the individual parts in Pascal.
You'll need a function that can count the number of zeros in a string.

You might start with something like this:

repeat
  RequestUserInput;
  RecordEnteredSequence;
until UserIndicatesEnough;

Set maxZeroCount to length of Sequence1
LoopThroughRecordedSequences
  begin
  if LengthOfCurrentSequence > maxZeroCount then
   begin
     maxZeroCount:=LengthOfCurrentSequence;
     longestSequence:=CurrentSequence;
   end;
  end;

Display maxZeroCount and longestSequence;

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: So... I'm back
« Reply #2 on: October 23, 2015, 10:18:48 pm »
Mmmm, I see what you're trying to do there but I think you need to rethink your strategy.

First try to create this with the English language and then make pseudo-code. After that you can translate it to Pascal.

For instance... I would make some pseudo-code like this...
--------------
HaveZero = 0 // this would be a current zero sequence which your counting
MaxZero = 0 // this would be the maximum zero sequence already encountered
AtLocation = 0 // this would be the location of the previous largest zero sequence of maxzero
Make a big loop though all the numbers:
(
   if number is zero then
   (
      increase HaveZero
   ) otherwise (
      if HaveZero > MaxZero then
      (
         we have a new "winner"
         set the MaxZero to HaveZero
         and set AtLocation at current number.
      )
      set HaveZero back to 0 for the following sequence
   )
)     
print out MaxZero at AtLocation
--------------
Do you understand above pseudo-code??? Do you see how I get the correct values? If you do you can translate this to Pascal. If not please specify what you don't understand about it.

(nuts... howardpc just beat me to it :) Ah, well, now you have two pseudo-codes to check out)
« Last Edit: October 23, 2015, 10:22:34 pm by rvk »

guest58006

  • Guest
Re: So... I'm back
« Reply #3 on: October 23, 2015, 10:28:24 pm »
HaveZero = 0 // this would be a current zero sequence which your counting - so this one is the number of 0's in my currenct sequence? (then this one should be " if a=0 then inc HaveZero?)
MaxZero = 0 // this would be the maximum zero sequence already encountered - this one would be the biggest sequence of 0's?
AtLocation = 0 // this would be the location of the previous largest zero sequence of maxzero - this one i'm unsure how to do. what I'm imagining is using " for i:= i to n do "

To be honest, i've understood most of it, only
Quote
and set AtLocation at current number.
and
Quote
print out MaxZero at AtLocation
are a bit confusing for me.

Also what do you mean by ')', not used to writing pseudo-codes and it is also confusing for me..

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: So... I'm back
« Reply #4 on: October 23, 2015, 10:35:05 pm »
To be honest, i've understood most of it, only
Good :)

Quote
and set AtLocation at current number.
and
Quote
print out MaxZero at AtLocation
are a bit confusing for me.
I assumed you also want to know the location of the biggest zero sequence (at what number does the sequence start) and how large it is. The AtLocation would be the location and the MaxZero would be the size.

And now that I say that out loud, I realize I made a small error in the code  :-[ I set the AtLocation at the end of the determination of MaxZero. But that would mean AtLocation is the number at the END of the sequence. So either you need to set AtLocation to number - MaxZero or you need to set it elsewhere in the pseudo-code. (This mistake would have eventually cropped up during testing.

Also what do you mean by ')', not used to writing pseudo-codes and it is also confusing for me..
I used the ( and ) to show the blocks. In real Pascal these would be begin and end.

There isn't really a set of rules for pseudo-code (that I know of). It's just you writing in semi-code what you want to program. So you begin with the logic in your head and write it down in english/semi-code, like we did here. It's easier to read and if you have your logic and pseudo-code ready you can translate it into Pascal. It's much easier than just beginning to code in Pascal (like you did) :)
https://en.wikipedia.org/wiki/Pseudocode
« Last Edit: October 23, 2015, 10:39:21 pm by rvk »

guest58006

  • Guest
Re: So... I'm back
« Reply #5 on: October 23, 2015, 10:48:37 pm »
Quote
set AtLocation to number - MaxZero
- here you mean '-' as minus, right? Sorry for such questions, but it's getting pretty late and my mind isn't working as it's supposed to be.

Quote
I assumed you also want to know the location of the biggest zero sequence
- YES, but that's not all. I use that location TO CONTINUE finding NEW sequences of 0's until my array ( [1..100] ) finishes and THEN compare all those sequences to see which ones has the biggest size.

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: So... I'm back
« Reply #6 on: October 23, 2015, 10:54:12 pm »
Quote
set AtLocation to number - MaxZero
- here you mean '-' as minus, right? Sorry for such questions, but it's getting pretty late and my mind isn't working as it's supposed to be.
Yes. The AtLocation would be at the end so you want AtLocation minus MaxZero (maybe plus one but I'm too lazy to check that now. That will come out when you're testing this)

Quote
I assumed you also want to know the location of the biggest zero sequence
- YES, but that's not all. I use that location TO CONTINUE finding NEW sequences of 0's until my array ( [1..100] ) finishes and THEN compare all those sequences to see which ones has the biggest size.
Why would you want to first count all sequences and then compare them to get the biggest. If you loop over all the numbers and you find a bigger sequence, why is the smaller sequence still of importance to you???

You see... if you first find a sequence of 4, that's the biggest. Then if you find a sequence of 2, the 4 is still the biggest and that 2 doesn't matter. Then if you find a sequence of 9, that sequence is the one you want and you can forget about that older sequence of 4.

Why would you want to do that differently?
The way you describe it, you're only make it difficult for yourself because you need to do the compare at the end. And of how many sequences? 3? 10? Maybe 88?
No... when you encounter the biggest sequence, remember that one and forget about the previous one. That's the most efficient way and easiest to program.
« Last Edit: October 23, 2015, 10:56:55 pm by rvk »

 

TinyPortal © 2005-2018