13 Replies - 1244 Views - Last Post: 20 May 2012 - 01:05 PM Rate Topic: -----

#1 rolands94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-May 12

Pascal problem

Posted 19 May 2012 - 10:44 AM

Hi, I have a problem with my pascal code.
when d(discriminant) is below 0, it pascal shows me an error..
sorry, but pascal doesn't allow to normal copy code, here's a picture:
Posted Image
Is This A Good Question/Topic? 0
  • +

Replies To: Pascal problem

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,039
  • Joined: 21-June 11

Re: Pascal problem

Posted 19 May 2012 - 10:50 AM

View Postrolands94, on 19 May 2012 - 07:44 PM, said:

when d(discriminant) is below 0, it pascal shows me an error..


What's the error message?

Quote

sorry, but pascal doesn't allow to normal copy code, here's a picture


One of the lines is cut off in that picture. No idea whether it's relevant.

PS: You can just open the source file in notepad (or whatever) and copy and paste from there.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,039
  • Joined: 21-June 11

Re: Pascal problem

Posted 19 May 2012 - 11:41 AM

Also what are you doing with the colons in the second and third calls to writeln (i.e. x1:2:2 etc.)? That doesn't look legal to me.
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1291
  • View blog
  • Posts: 2,859
  • Joined: 30-January 11

Re: Pascal problem

Posted 19 May 2012 - 11:44 AM

They are legal Pascal - the first number meaning the :width and the second meaning the precision for numbers. It's essentially just Pascal's implementation of string formatting.
Was This Post Helpful? 1
  • +
  • -

#5 rolands94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-May 12

Re: Pascal problem

Posted 19 May 2012 - 01:56 PM

I figured out, that the error only appears if the d is a negative number.
error: exitcode = 207
program uzd2;
uses crt;
var x,y,z:integer;
zime:char;
procedure kv(a, b, c:integer);
        var x1, x2, x3, d:real;
        begin
                d:=-200;
              //  d:=b*b-4*a*c;
                x1:=(-b+sqrt(d))/(2*a);
                x2:=(-b-sqrt(d))/(2*a);
                x3:=-b/(2*a);
                if d<0 then writeln('Realu saknu nav');
                if d=0 then writeln('Viena sakne - x1: ',x3:2:2);
                if d>0 then writeln('Divas saknes - x1: ',x1:2:2,', x2: ',x2:2:2);
                writeln;
        end;
begin
        clrscr;
        writeln('Kvadratvienadojuma atrisinasana');
        zime:='j';
        while zime='j' do
                begin
                write('Ievadiet kvadratvienadojuma koeficientus:');
                readln(x,y,z);
                kv(x,y,z);
                writeln('Turpinat darbu(j - ja, citi taustini - ne)?');
                readln(zime);
                end;
        writeln('Programmas beigas! Piespiediet jebkuru tausinu!');
        readln;
end.



Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,039
  • Joined: 21-June 11

Re: Pascal problem

Posted 19 May 2012 - 02:03 PM

Bah, I should have seen the error right away: A negative number doesn't have a real square root.
Was This Post Helpful? 0
  • +
  • -

#7 rolands94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-May 12

Re: Pascal problem

Posted 20 May 2012 - 09:04 AM

Ok, then how to do the following thing: if discriminant from quadratic is negative(d<0), then output sentence: "There are no real roots".?
Was This Post Helpful? 0
  • +
  • -

#8 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1291
  • View blog
  • Posts: 2,859
  • Joined: 30-January 11

Re: Pascal problem

Posted 20 May 2012 - 09:44 AM

If d < 0 Then
     Writeln('There are no real roots');

Was This Post Helpful? 0
  • +
  • -

#9 rolands94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-May 12

Re: Pascal problem

Posted 20 May 2012 - 10:44 AM

Well, that's exactly what is written in my code at line 13.
                if d<0 then writeln('Realu saknu nav');



And the error pops out if the d is below 0. That's the problem, I need the output without the error, so that the program works.
Was This Post Helpful? 0
  • +
  • -

#10 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1291
  • View blog
  • Posts: 2,859
  • Joined: 30-January 11

Re: Pascal problem

Posted 20 May 2012 - 11:13 AM

Well of course you get the error. You are trying to find the sqrt of -200 and THEN print out the message.

You only want to do the calculations if the discriminant >= 0.
Was This Post Helpful? 1
  • +
  • -

#11 rolands94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-May 12

Re: Pascal problem

Posted 20 May 2012 - 11:36 AM

Uhh, oups, i copied the test code, without d:=-200; there is the same error.
Was This Post Helpful? 0
  • +
  • -

#12 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1291
  • View blog
  • Posts: 2,859
  • Joined: 30-January 11

Re: Pascal problem

Posted 20 May 2012 - 11:46 AM

Yes that is expected, read by previous post again.
Was This Post Helpful? 1
  • +
  • -

#13 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,039
  • Joined: 21-June 11

Re: Pascal problem

Posted 20 May 2012 - 12:31 PM

To elaborate on Ryan's point: If d is negative, you print that there are no solutions. But before you do that, you try to calculate the square root of d - whether d is negative or not. You're not allowed to calculate the square root of d if d is negative. That's what causes your error.
Was This Post Helpful? 1
  • +
  • -

#14 rolands94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-May 12

Re: Pascal problem

Posted 20 May 2012 - 01:05 PM

Ok, so I solved my problem, thanks all.
Here's my code:
program uzd2;
uses crt;
var x,y,z:integer;
zime:char;
procedure kv(a, b, c:integer);
        var x1, x2, x3, d:real;
        begin
//              d:=b*b-4*a*c;
                if b*b-4*a*c<0 then writeln('Realu saknu nav');
//                if d:=b*b-4*a*c=0 then writeln('Viena sakne - x1: ',x3:2:2);
  //              if d:=b*b-4*a*c>0 then writeln('Divas saknes - x1: ',x1:2:2,', x2: ',x2:2:2);
    //            x1:=(-b+sqrt(d))/(2*a);
      //          writeln;
                if b*b-4*a*c=0 then
                begin
                x3:=-b/(2*a);
                writeln('Viena sakne x1: ',x3:2:2);
                end;
                if b*b-4*a*c>0 then
                begin
                d:=b*b-4*a*c;
                x2:=(-b-sqrt(d))/(2*a);
                x1:=(-b+sqrt(d))/(2*a);
                writeln('Divas saknes x1: ',x1:2:2,', x2: ',x2:2:2);
                end;
        end;
begin
        clrscr;
        writeln('Kvadratvienadojuma atrisinasana');
        zime:='j';
        while zime='j' do
                begin
                write('Ievadiet kvadratvienadojuma koeficientus:');
                readln(x,y,z);
                kv(x,y,z);
                writeln;
                writeln('Turpinat darbu(j - ja, citi taustini - ne)?');
                readln(zime);
                end;
        writeln('Programmas beigas! Piespiediet jebkuru tausinu!');
        readln;
end.



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1