Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: Help / Advice please...

  1. #1

    Help / Advice please...

    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?)

  2. #2
    Instead of posting the answer, can you post your code for us to see, so we can tell you where you went wrong?

  3. #3
    sorry, I'm a bit ashamed of the code so far. am a bit lost on it.

    Code:
    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
    Last edited by stevengreen22; 21-03-2011 at 04:00 PM.

  4. #4
    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.

    Code:
    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...

  5. #5
    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!!!!

  6. #6

  7. #7
    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

  8. #8
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25
    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.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  9. #9
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    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 ) - 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.
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  10. #10
    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
    Code:
    pos:=numcheck>0;
    do this
    Code:
    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
    Code:
    procedure askNumber();
    var numcheck : integer;
    begin
       ...

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •