Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

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

Report
highest of three numbers .. not working properly Posted by fanatic on 13 May 2006 at 8:14 AM
this program calculates the highest of the three numbers
but when i input
number 1
number 2
number 3

as 1 , 2 , 3

it prints the highest number as 41
?????????????????

program highestofthree;

function highest(v1, v2, v3 : integer): integer;

begin
if v1 > v2 then
begin
if v1 > v3 then
begin
result := v1;
end
else
begin
result := v3;
end;
end
else
if v2 > v3 then
begin
if v2 > v3 then
begin
result := v2;
end
else
begin
result := v3;
end;

end;
end;

procedure main();

var
num1, num2, num3 : integer;

begin
writeln('Enter num1 :');
read(num1);
writeln('Enter num2 :');
read(num2);
writeln('Enter num3 :');
read(num3);

writeln();

writeln('The highest out of the three numbers is :', highest(num1, num2, num3));
end;




begin
main();

end.
Report
Re: highest of three numbers .. not working properly Posted by zibadian on 13 May 2006 at 8:38 AM
: this program calculates the highest of the three numbers
: but when i input
: number 1
: number 2
: number 3
:
: as 1 , 2 , 3
:
: it prints the highest number as 41
: ?????????????????
:
: program highestofthree;
:
: function highest(v1, v2, v3 : integer): integer;
:
: begin
: if v1 > v2 then
: begin
: if v1 > v3 then
: begin
: result := v1;
: end
: else
: begin
: result := v3;
: end;
: end
: else
: if v2 > v3 then
: begin
: if v2 > v3 then
: begin
: result := v2;
: end
: else
: begin
: result := v3;
: end;
:
: end;
: end;
:
: procedure main();
:
: var
: num1, num2, num3 : integer;
:
: begin
: writeln('Enter num1 :');
: read(num1);
: writeln('Enter num2 :');
: read(num2);
: writeln('Enter num3 :');
: read(num3);
:
: writeln();
:
: writeln('The highest out of the three numbers is :', highest(num1, num2, num3));
: end;
:
:
:
:
: begin
: main();
:
: end.
:
You should check your if-then statements, because the result of the function is sometimes undefined.
Report
Re: highest of three numbers .. not working properly Posted by bosebonas on 13 May 2006 at 10:35 AM
hi
one thing you forget about the if..then statement is that you do not use semicolons anyhow so be carefull. where the semicolon is determines where the if statements ends.
for example get a look at this program :

program test;
var number:word;
begin
read(number);
if (number=10) then
writeln('you just entered the number 10');
else end.
this program will not even compile because the if statement ends at the level of the writeln statement.
if it is not clear then you can contact me for more explanation.

Report
Re: highest of three numbers .. not working properly Posted by zibadian on 14 May 2006 at 8:50 AM
: hi
: one thing you forget about the if..then statement is that you do not use semicolons anyhow so be carefull. where the semicolon is determines where the if statements ends.
: for example get a look at this program :
:
: program test;
: var number:word;
: begin
: read(number);
: if (number=10) then
: writeln('you just entered the number 10');
: else end.
: this program will not even compile because the if statement ends at the level of the writeln statement.
: if it is not clear then you can contact me for more explanation.
:
:
I don't think the problem is the semicolons, because his program compiles fine. My guess is the actual conditions of the if-then statements are somehow mixed up.
Report
Re: highest of three numbers .. not working properly Posted by fanatic on 14 May 2006 at 8:56 AM
: : hi
: : one thing you forget about the if..then statement is that you do not use semicolons anyhow so be carefull. where the semicolon is determines where the if statements ends.
: : for example get a look at this program :
: :
: : program test;
: : var number:word;
: : begin
: : read(number);
: : if (number=10) then
: : writeln('you just entered the number 10');
: : else end.
: : this program will not even compile because the if statement ends at the level of the writeln statement.
: : if it is not clear then you can contact me for more explanation.
: :
: :
: I don't think the problem is the semicolons, because his program compiles fine. My guess is the actual conditions of the if-then statements are somehow mixed up.
:
yep i think the same .. i m actually learning the language i m not familiar with the syntax of the language




 

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.