Results 1 to 10 of 25

Thread: Help / Advice please...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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

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
  •