PDA

View Full Version : Help / Advice please...



stevengreen22
21-03-2011, 01:04 PM
Hello all,

I've encountered an issue that I cant resolve myself and in need of a little direction / help.

It seems so simple it's killing me.

The program is to check number to see whether they're negative, positive or zero. This I've done by setting up boolean variables and a simple "type in the number" statement & a readln.

The program works fine for single numbers. The issue I have is that I need the prog to ask how many numbers in total will be checked. This then needs to be stored somehow and for the program to allow this number of numbers to be entered, run the prog for each one and to then count and display how many of them were pos, neg or zero.

May not have explained it that well.

Can anyone point me in the right direction?
Thanks.

(I have a variable for total numbers to enter, the main bit I'm missing is the construct for the loop, each time I amend it I get odd results or it crashes. Also how do i then Count these results?)

Traveler
21-03-2011, 03:47 PM
Instead of posting the answer, can you post your code for us to see, so we can tell you where you went wrong?

stevengreen22
21-03-2011, 03:57 PM
sorry, I'm a bit ashamed of the code so far. am a bit lost on it.



var count,i,totnum,number,numcheck: integer;
pos,zero,neg:boolean;

begin {main program}

begin

write('Please type in how many numbers will be input ');
readln(totnum);

writeln('what numbers will be checked?');
read(numcheck);
pos:=numcheck>0; //boolean variables
neg:=numcheck<0;
zero:=numcheck=0;

// for count:= 0 to totnum do

if pos then write('This is a positive number');
if neg then write('This is a negative number');
if zero then write('This number is Zero');

cont;
end;

end.

{ program countspace(input,output);
var i,lenght,count:integer;
sentence:string;


begin
write('sentence: ');
readln(sentence);

for i:=0 to lenght (sentence) do //Do error??!?!?!?!
if (sentence[i] = ' ') or (sentence[i] ='.') then
count:=count+1;

writeln('spaces: ',count);
readln; end. }
The prog at the bottom is a simple one to read number of spaces but even that wouldn't compile. Simply don't knwo what I'#m doing wrong

Traveler
21-03-2011, 04:25 PM
No need to be ashamed. No matter how long you've been programming, there will always be people who write better, cleaner code. Just make sure you learn from it so you can use it the next time.

Anyway about your code. It's really not that bad at all. In fact, you're almost there already.



var count,i,totnum,numcheck: integer;
pos,zero,neg:boolean;

procedure askNumber();
begin
writeln('what numbers will be checked?');
read(numcheck);

pos:=numcheck>0; //boolean variables
neg:=numcheck<0;
zero:=numcheck=0;

if pos then writeln('This is a positive number');
if neg then writeln('This is a negative number');
if zero then writeln('This number is Zero');
end;

procedure askTotal();
begin
write('Please type in how many numbers will be input ');
readln(totnum);

for count:= 0 to totnum-1 do askNumber()
end;

begin //main
askTotal()
end.


In the code above, which is mostly yours, I have separated the first question and placed it in a new procedure, together with a for-loop, which repeats the second procedure with the 2nd question.

I have not completed your assignment which asks for the totals of positive/negative values, but I'm pretty sure you can do the yourself now. If not, we'll still be here... ;)

stevengreen22
21-03-2011, 04:35 PM
YOu absolute legend, Thank you so much! I've not tested it yet but it looks how i wanted it to. I really liek using procedures from previous tasks, they make everything cleaner but i didn't think to use them in this and the loops are fantastic. thanks!!!!

Traveler
21-03-2011, 04:54 PM
You're welcome :)

stevengreen22
21-03-2011, 05:12 PM
I just had a play and it works fine, thankyou, this is also the first I've seen a procedure call upon another, something else that I've learnt, One thing though...Why have you added'()' at the end of them? Is this necessary? I've not used it on any of mine, is it better practice to have them?
also...i just had an error message but what I'd now like it to do is store the pos / neg / zero boolean variables as a number. is this possible using these variables or should i have additional variables?
thanks for taking the time to read /reply

WILL
21-03-2011, 08:57 PM
The () is personal preference. They don't affect the compiled code what-so-ever only how your code is read. So honestly it would be up to you as how you prefer your own code-style to be. It's like that last semi-colon before the end in the main code block. You'll notice that he didn't add that either. I always add the semi-colon myself unless it's right before an else in an if .. than .. else statement.

code_glitch
21-03-2011, 10:10 PM
Hahaha, be careful there steven, you do not want to get into one of those style arguments... I always say indent 1 tab (set to 4 spaces in IDE), keep it all in line, break into blocks, add () whenever appropriate, same thing for ; add comments in blocks, separate it all out nicely and etc... If someone wants to read your code, then go ahead, just set tab to whatever spacing you like. Its sort of universal that way. Oh, and when passing variables to a procedure/function I prefer to do xyz(1, 2, y, h) instead of xyz(1,2,y,h) I just think it looks neater. Or you could abc( (cgdfd - uwergw), 8 ) with those spaces... its all about esthetics mainly. Compare it to ones taste in girls (talking bout you girls here so all girls officially banned from thread ;) and no looking up my email address either :D) - they are never the same so basically, no matter you're coding style: chances are, someone hates it. Now I'm not saying some people hate certain girls, but you get the picture. I might like (notice the MIGHT here) prefer thinner girls with a nice personality to the erm.... Models of our world. this is HYPOTHETICAL - don't make assumptions; female intuition does not solve the answer to everything. Its 42. ;)

Traveler
21-03-2011, 11:02 PM
You need to drink less coffee, code_glitch ;)

Anyway, to answer your last question about the variables. You're definitely going to need different variables to store the totals. However, it's also possible to get rid of some. For example you use a variable to check the value read and then use that variable to display the outcome. Why not do both at the same time? It saves code and makes the whole thing easier to read.
So instead of


pos:=numcheck>0;

do this


if (numcheck > 0) then writeln('This is a positive number');


Also, while I'm on the subject of variables. Try to have as few global variables as possible.
Any variable that isn't needed outside of a procedure should be declared inside. ie


procedure askNumber();
var numcheck : integer;
begin
...

stevengreen22
22-03-2011, 08:55 AM
ah, I see, thanks for clarifying. Code glitch - if you're not drinking obscene amounts of coffee then maybe you've got another substance taking it's toll :) As for woman...I know what you mean, not entirely sure I can compare my program to them, I like it to be perfect or as perfect as I know, doubt I'll ever find the perfect woman, thats a tall order but thanks for putting it in a way that any guy should understnads.

traveller - I just read up on procedures some more, I'm going to play woth the code some more and put the var inside the proc, again, if it makes the code cleaner then I'm all for it. I like it when the prog is clean / tidy. thanks for pointing out the numcheck bit, it's things likethat that I don't notice striaght away. thanks. I'l post the next installment a bit later for your perusal.

on the offchance...I'm puttign a website together (again this is an assignment that I wanted to take to the next level.) www.webdesignprofessionals.co.uk (any constructive criticism woudl be awesome.

stevengreen22
22-03-2011, 11:34 AM
Hi guys,

still having a couple of minor issues (by minor I mean...it's not working)

I've got the same code as before, tidied it up a bit and put the var withing the procedures to make them local



program EX7NOSORT(input,output);
uses crt;
var posi,negi,zero:integer;

heelo & press ret to cont proc

procedure AskNumber;
var numcheck:integer;
begin
writeln('What number/s would you like to check?');
read(numcheck);
begin---------------------added to test
if numcheck>0 then writeln('This is a positive number');
read(posi);---------------------added to test
if numcheck<0 then writeln('This is a negative number');
read(negi);---------------------added to test
if numcheck=0 then writeln('This number is Zero');
read(zero);---------------------added to test
end;
end;

procedure AskTotal;

var count,totnum:integer;
begin
writeln;
writeln('How many numbers will you like to check? ');
readln(totnum);

for count:= 0 to totnum-1 do AskNumber
end;

begin {main program}
hello;
AskTotal;
writeln(posi);---------------------added to test
cont;
{up to 33750}

end.




I tried several variations of read(posi) etc and write and i get nothing, the closest ic ame was when it displayed 3 zero's(when pos & neg were selected) I figure if I can get it to print one then I can work from there.
It seems when I put the read lns in the code doesn't crash but i have to input several numbers and carriage returns before it asks the next number, then repeats until tot numbers has been reached.
I'm a wee bit stuck, I thought by adding a begin / add it might separate the prog but no. I think I might have to ask chief(tutor) for help on this.
It looked so easy to do as well! AGGGH!!!!!

Traveler
22-03-2011, 12:25 PM
In pseudo code, you're now doing this.


Start program
Ask user to input an amount of numbers
loop over the given amount each time doing :
1) ask for a number
2) read the number into memory
3) check if number is great than zero and write output to screen
4) wait for user to input something
5) check if number is below zero and write output to screen
6) wait for user to input something
7) check if number is equal to zero and write output to screen
8) wait for user to input something //end of loop
write the value of posi to screen



You should change this so that it looks more like this:


Start program
initialize variables posi,negi,zero to 0
Ask user to input an amount of numbers
loop over the given amount each time doing :
1) ask for a number
2) read the number into memory
3) check if number is great than zero. If this is true then
write output to screen
save this instance to variable posi .
if this is not true then
4) check if number is below zero. If this is true then
write output to screen
save this instance to a variable negi.
if this is not true then
5) check if number is equal to zero. If this is true then
write output to screen
save this instance to variable zero //end of loop
write the value of posi, negi and zero to screen
end program.

stevengreen22
22-03-2011, 02:40 PM
I tried what you suggested and I did have some success but...still more questions...



begin
writeln('What number/s would you like to check?');
read(numcheck);

posi:=0;negi:=0;zero:=0
if numcheck>0 then writeln('This is a positive number');
read(posi);
if numcheck<0 then writeln('This is a negative number');
//read(negi);---------------------added to test
if numcheck=0 then writeln('This number is Zero');
// read(zero);---------------------added to test
end;
end;


Couple of problems that I can't seem to navigate, I've tried several
options.when i have the read statements after each line it forces me
to press a number a certain number of times before asking for the next
number. for example, if i say the first number to check is 5, i thne
have to input a number 5 times before moving on to the next one.
I can eliminate this by commenting out the other read lines as above.
although it stills asks for another number at the end of the statement
before moving on. this is minor though I guess.
I've tried variations of:


posi:=0;negi:=0;zero:=0
if numcheck>0 then writeln('This is a positive number');
read(posi);posit:=posi;

-----------------
posi:=0;negi:=0;zero:=0
if numcheck>0 then writeln('This is a positive number');
read(posi);posit:=posi+1;
------------------------


also tried it in the writlen at the end of the prog, tried to group
together as well. I think there is just something fundamentally wrong
that I'm not understanding. I'm also now thinking that the read is
not doing as I want. It needs to count the number of posi that are
entered but I get mixed results, on one test it did and on another it
simply output the number that i entered.

Hair pulling time.

as a side note....you guys could bang this out in a couple of minutes...I
have a tendency to make things more complicated for myself. Is this a
difficult task or am I a fking idiot :)

stevengreen22
22-03-2011, 05:24 PM
SOLVED IT!!!!!!!!!!

all was right but i had the initisling bit at the start of the loop, every time it repeated it reset to zero!!!! sorted!!!!! THANKYOU!!!!

Traveler
22-03-2011, 08:07 PM
as a side note....you guys could bang this out in a couple of minutes... I have a tendency to make things more complicated for myself. Is this a
difficult task or am I a fking idiot :)

No, it is not a difficult task, but that certainly doesn't make you an idiot.

The reason why it is difficult for you is because you appear to do two things at once.
1) learning how to program in general, the principles.
2) learning pascal.

While you can do both it is much harder than when you only have to do the 2nd. The first however is the most important. Do that one right and you can apply it to any language.

code_glitch
22-03-2011, 09:16 PM
You need to drink less coffee

Ah, what would life be without faithful ol' caffeine...

Edit: oh, and gals of course ;)

stevengreen22
23-03-2011, 07:27 PM
Hi guys,

Thanks for point a few bits out there. You're right of course, It's hard enough learning the concept let alone the language as well, I'm also burrowing my head in php which is killing me. I handed the work i today, 15/15 for one and 14/15 for the other so not too bad at all. thankyou for all your help. No doubt i'll be posting again soon when I jump on the next mission - having to read / write to files and stuff now......yay..

code_glitch
23-03-2011, 09:38 PM
read & write files? Think logically, slowly and in simple instructions. Then its simple. The more procedure to treat information the better. Oh, and dont forget the else statement in case you find data you did not expect and that sometimes its good for the program to say 'error' than hang forever ;) that really is all theory to that bit ahead of time.

stevengreen22
26-03-2011, 09:57 AM
Thanks code dude! I got it sorted, well the intially part of the excercise anyways, just need to print it to a file and job done. Out of curiosity...Do you know of anyone thats a dab hand in php that woudln't mind trying to "nicely" infiltrate a site i'm working on.

code_glitch
26-03-2011, 10:25 AM
Hmm. I haven't heard from Athena lately (thats AthenaOfDelphi) since I know she does all of the SQL and web stuff for PGD she might know... I run my own site from home, but thats mainly drupal with scarce fragments of hand written PHP and CSS (most of it is just changing elements I don't like in themes). I suppose it depends on what you need done.

deathshadow
30-03-2011, 01:55 PM
Some advice: Do not waste time checking the same value more than once... and do not bother storing values in a program that you are just going to discard. Your three boolean test condition variables likely are not being used, and it is generally as fast to test a condition as it is to test a boolean...

What I mean to say, is I'd get rid of the three 'pos, neg and zero' variables, AND I'd not run all three if's every time.

The reason for this is you are executing code you don't have to, and checking values you may already know the status of -- which means you have more code running than neccessary! You're running three conditional evaluations, three variables and three if statements to do the job of two conditionals INSIDE two if's.



if (numCheck>0) then begin
writeln('This is a positive number');
end else if (numcheck<0) then begin
writeln('This is a negative number');
end else writeln('This number is zero');


Is all you really need to be doing there. If the first check is true, it writes and ignores the rest of the code. If it's fals, we check the opposite direction, if so write the result, if not -- we know it's not >0, we know it's not <0, so it HAS to be zero, so there's no reason to put an IF there on that.

Basically, if it's >0 there is NO reason to check for <0 or 0. If it's <0 and we've eliminated >0, there's no reason to check for 0... if neither state is true, it has to be zero.

dazappa
30-03-2011, 02:20 PM
Hmm. I haven't heard from Athena lately (thats AthenaOfDelphi) since I know she does all of the SQL and web stuff for PGD she might know... I run my own site from home, but thats mainly drupal with scarce fragments of hand written PHP and CSS (most of it is just changing elements I don't like in themes). I suppose it depends on what you need done.
I imagine simple SQL injection testing to see if you can gain access to the db.

stevengreen22
02-04-2011, 10:52 AM
cheers for the advice with the boolean and else if code, it makes it cleaner and when programming i like it to be clean and tidy - nice one! :)

as for the sql injection, I've been using eregi to filter out unwanted characters, realised its obsolute and not in the process of checking and replacing with preg. It seems to be sound but as with pascal, theres so much I odn't know it woudl be easy to make a silly mistake.

stevengreen22
02-04-2011, 10:53 AM
the read / write text file thing....smashed it :)