Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
HELP!!!!!!!!!!!!!!Counting Var / Number Posted by stevengreen22 on 22 Mar 2011 at 9:24 AM
Hi,

I've been struggling with this for hours now.
Can someone please help / point me in the right direction.

It was set out as a boolean excercise but I've amended it a little by using procedures instead, this is minor as I shoudl jsut be able to revert it, I was trying to eliminate the global variables.

procedure AskNumber;
var numcheck:integer;
begin
        writeln('What number/s would you like to check?');
        readln(numcheck);
        posi:=0; negi:=0; zero:=0;

        if  numcheck>0 then writeln('This is a positive number');
        readln(posi);posit:=posi;    {read as last number input, not counting}
        if  numcheck<0 then write('This is a negative number');
     //   read(negi); negit:=negit+1;
        if  numcheck=0 then write('This number is Zero');
     //   read(zero);zerot:=zero+1;

end;

procedure AskTotal;

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

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

begin {main program}
      AskTotal;

      writeln(posit,negit,zerot);
                             {up to 33750}
end.


I need the program to decided whether a number is positive etc, this works fine. The part I'm stuck on is asking it to 'count' the number of pos/neg etc and to then display that number at the end.

The read line that i have is reading the number as an integer and not as a (+1) to be added. I've played arounfg with it so much but can't fget the fking thing to work.
Help!

Report
Re: HELP!!!!!!!!!!!!!!Counting Var / Number Posted by quikcarl on 22 Mar 2011 at 11:04 AM
I've borrowed your code and made some alterations to get what I think you want.
  var
    posit,negit,zerot : integer;
procedure AskNumber;
var numcheck:integer;
begin
        writeln('What number/s would you like to check?');
        readln(numcheck);
(*        posi:=0; negi:=0; zero:=0; *)
        if  numcheck>0 then begin
          writeln('This is a positive number');
          posit := posit + 1
        end;(*      readln(posi);posit:=posi;    {read as last number input, not counting} *)
        if  numcheck<0 then begin
          write('This is a negative number');
          negit := negit + 1
        end;     //   read(negi); negit:=negit+1;
        if  numcheck=0 then begin
          write('This number is Zero');
          zerot := zerot + 1
        end     //   read(zero);zerot:=zero+1;

end;

procedure AskTotal;

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

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

begin {main program}
  posit := 0;
  negit := 0;
  zerot := 0;
      AskTotal;

      writeln(posit,negit,zerot);
                             {up to 33750}
end.

Report
Re: HELP!!!!!!!!!!!!!!Counting Var / Number Posted by DaiMitnick on 22 Mar 2011 at 11:19 AM
EDIT: Ignore post, seems Carl addressed these issues in the time I was away from my PC.




Haven't done Pascal in a few years so forgive my ignorance. First, is this line:

readln(posi);posit:=posi;

actually part of the if statement? To me it looks like it will be run whether the number is positive or not. Whether it is or isn't it looks like you're setting it to 0 each time, because of this line:

posi:=0; negi:=0; zero:=0;

As I said, I'm rusty so perhaps I'm wrong. Also, where are posit, zerot and negit declared, because if they are not global then (I believe) they will go out of scope each time you call the procedure and so it will not return any values to your main procedure.

Regards, Dai


------------------------------------------
Do or do not, there is no try. |
------------------------------------------



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.