Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Program

Status
Not open for further replies.

ccramer

Programmer
Dec 10, 2006
1
US
I'm having a problem in one of my textbooks and can't seem to come to a solution to it. It's in the book "Using TurboPascal 6.0-7.0" by Hennefield. It has to do with two-dimensional arrays.

The problem asks you to write a program that prints all Pythagorean triples with c<=25. Then it says to modify that program so you do not allow duplications of similar triangles. For example, 3,4,5 should appear but 6,8,10 should not (only reduced).

Please help me.
 
Do you know how to solve this problem in pencil and paper? Is this an algorithm issue to you or a coding issue? And the important question on this one: Are you getting a grade for this project?
 
Here is a partial solution written in Delphi, so it is close but not exact to Turbo Pascal:

The function "not_Already_there needs to be improved to check that each entry is not a multiple of an earlier entry. Right now all it does is eliminate the same entries in a different order (for example, it does not add {4,3,5} because it found {3,4,5}

It found these numbers:
3, 4, 5,
5, 12, 13,
6, 8, 10,
7, 24, 25,
8, 15, 17,
9, 12, 15,
12, 16, 20,
15, 20, 25,

Good luck


procedure TForm1.Button1Click(Sender: TObject);
var
i,j,k, test, test2 : integer;
r1, r2 : real;
function not_already_there(a,b,c:integer) : boolean;
var
idx : integer;
s1 : string;
begin
result := true;
for idx := 0 to listbox1.items.count-1 do
begin
s1 := listbox1.items[idx];
if (pos(inttostr(a)+',',s1) > 0)
and (pos(inttostr(b)+',',s1) > 0 )
and (pos(inttostr(c)+',',s1) > 0 ) then
result := false;
end;

end; // of function


begin // of procedure

for i := 1 to 25 do
for j := 1 to 25 do
for k := 1 to 25 do
begin
test := i*i + j*j;
r1 := sqrt(test);
r2 := k * 1.0;
if (r1) = r2 then
if not_already_there(i,j,k) then
listbox1.items.add(inttostr(i)+', '+inttostr(j)+ ', '+inttostr(k)+',');
end;

end;

end.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top