# difficult equation

i have been given a so called simple equation to solve
but if this is a simple equation after only 5 months of
programming then god knows what the next 4 years
is going to bring
i am to design a program that will ask the user to enter
any number of columns and any number of rows
given that the columns and rows are of equal height and length
the program will then calculate how many vertical and horizontal
lines a {secondary line would pass through if drawn from the top
left hand column to the bottom right hand column} alright that might
sound simple enough but heres the tricky part if the secondary line
passes directly through a corner of a box then you cannot count it as
passing through 2 lines vertical and horizontal but only 1 line

example is if the user inputs 1 column and 1 row a secondary
line passing from corner to corner would not pass through any
vertical or horizontal lines so the value would be zero if however
the user inputs 2 columns and 1 row the secondary line would
pass through 1 vertical line giving total value as 1
if 2 coloumns and 2 rows where inputted then  the value would still
be  1 because the secondary line passes directly through a corner of a box

so this where i need the help i need the formula to into my procedure to
calculate the correct number of lines the secondary line will pass through
my program goes like this

program passing_through;
uses wincrt;
var
columns, rows : integer;

procedure get_numbers;

begin
writeln (‘ enter the number  of columns and rows’);
if columns > 1
then
columns := columns + 1;
if rows > 1
then
rows := rows + 1;
end;

procedure calculate_lines;
var
total : integer;
begin
{formula required}
writeln (‘the number of lines is',total);
end;

begin
get_numbers;
calculate_lines
end.

as i think this formula is going to take some working out
i think 150 points will be a hard earned 150 points
cheers gandalph

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
This is the code I am offering with great efforts and hopes.

{ I don't know why you have added 1 in rows and columns }
program passing_through;
uses crt;
var
columns, rows : integer;

procedure get_numbers;

begin
write('Enter the number of columns and rows : ');
if columns > 1
then
columns := columns + 1;
if rows > 1
then
rows := rows + 1;
end;

procedure calculate_lines;
var
total : integer;
true,false,cornercut : integer;
begin
true := 1;
false := 0;
cornercut := -1;
columns := columns - 1;
rows := rows - 1;
if rows=columns then  total := columns - 1
else if odd(columns) AND odd(rows) then cornercut := false
else if odd(columns) AND NOT odd(rows) then cornercut := false
else if NOT odd(columns) AND odd(rows) then cornercut := false
else if NOT odd(columns) AND NOT odd(rows) then cornercut := true;

if cornercut=true then
begin
if (columns > rows) then
cornercut := (columns MOD rows) - 1
else if (rows > columns) then
cornercut := (rows MOD columns) - 1;
total := (columns-1)+(rows-1)-cornercut;
writeln ('The number of lines is = ',total);

end;
if cornercut=false then
begin
total := (columns-1)+(rows-1);
writeln ('The number of lines is = ',total);
end;

if (rows = columns) then writeln ('The number of lines is = ',total);
end;

begin
get_numbers;
calculate_lines;
end.
0
Author Commented:
sorry sumant's
but the program is only working for certain inputs
input 6 columns 3 rows total should be 5

1 column 3 rows should total 2 yours total gives 1

if you input 1 column and 2 rows the
result is a fatal error

please try again if you get it please tell me how you worked out the equation

thanks gandalph
0
Author Commented:
after looking at procedure to add 1 to rows and columns please ignore that bit it was my poor attempt at trying to solve at least a part of the equation hope this wasnt what through you off
gandalph
0
Commented:
If you count the intersections as two lines, the answer would obviously be columns-1 + rows-1
So you just need to subtract the number of intersections, which would be GreatestCommonFactor(rows,columns)-1
0
Author Commented:
i have altered sumant's program by taking out my equation and altering sumant's to suit
the program so far seems to be ok except that entering 3 columns 6 rows gives the total 7 whereas if drawn on a graph the answer totals 5 unless the cross over line is minute you cannot see it

i am now in the process of trying to work out if the total is true of larger numbers knowing my tutor his input will be hundreds of columns and rows

if sumant's formulas are correct then
so far
even columns odd rows = c+r-2
even columns even rows = c-2
odd columns even rows = c+r-2

the program now looks like this

program passing_through;
uses wincrt;
var
columns, rows : integer;
procedure get_numbers;

begin
write('Enter the number of columns and rows : ');

end;

procedure calculate_lines;
var
total : integer;
true,false,cornercut : integer;
begin
true := 1;
false := 0;
cornercut := -1;
columns := columns;
rows := rows;
if rows=columns then  total := columns - 1
else if odd(columns) AND odd(rows) then cornercut := false
else if odd(columns) AND NOT odd(rows) then cornercut := false
else if NOT odd(columns) AND odd(rows) then cornercut := false
else if NOT odd(columns) AND NOT odd(rows) then cornercut := true;

if cornercut=true then
begin
if (columns > rows) then
cornercut := (columns MOD rows) - 1
else if (rows > columns) then
cornercut := (rows MOD columns) - 1;
total := (columns-1)+(rows-1)-cornercut;
writeln ('The number of lines is = ',total);

end;
if cornercut=false then
begin
total := (columns-1)+(rows-1);
writeln ('The number of lines is = ',total);
end;

if (rows = columns) then writeln ('The number of lines is = ',total);
end;

procedure try_again;

begin
writeln (' Do you want to try again y/n');
end;

begin
repeat
get_numbers;
calculate_lines;
try_again;
end.

if the program is correct then i will reward sumant the points with grateful thanks

gandalph
0
Commented:
try 60 columns 140 rows
0
Author Commented:
hi ozo
tried what you asked total = 179
found problem with odd columns even rows = c+r-2

gives wrong totals for 3columns 6rows also 3columns 12rows

{If you count the intersections as two lines, the answer would obviously be columns-1 + rows-1
So you just need to subtract the number of intersections, which would be GreatestCommonFactor(rows,columns)-1}

see if that works

he said one formula would work for all
inputs he also said it was going to snow but it never did

cheers
0
Commented:
This is I think the final procedure.

procedure calculate_lines;
var
total : integer;
true,false,cornercut : integer;
begin
true := 1;
false := 0;
cornercut := -1;
columns := columns;
rows := rows;
if rows=columns then  total := columns - 1

else if (rows = 1) then total := columns - 1
else if (columns = 1) then total := rows - 1

else if ((columns MOD rows) = 0) then cornercut := rows - 1
else if ((rows MOD columns) = 0) then cornercut := columns - 1

else if odd(columns) AND odd(rows) then cornercut := false
else if odd(columns) AND NOT odd(rows) then cornercut := false
else if NOT odd(columns) AND odd(rows) then cornercut := false
else if NOT odd(columns) AND NOT odd(rows) then cornercut := true;

if (rows = columns) OR (rows = 1) OR (columns = 1)
then writeln ('The number of lines is = ',total)

else if ((columns MOD rows) = 0) OR ((rows MOD columns) = 0) then
begin
total := (columns-1)+(rows-1)-cornercut;
writeln ('The number of lines is = ',total);
end

else if cornercut=true then
begin
if (columns > rows) then
cornercut := (columns MOD rows) - 1
else if (rows > columns) then
cornercut := (rows MOD columns) - 1;
total := (columns-1)+(rows-1)-cornercut;
writeln ('The number of lines is = ',total);

end

else if cornercut=false then
begin
total := (columns-1)+(rows-1);
writeln ('The number of lines is = ',total);
end;
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Function CalcLines(Row,Col : longint) : longint;
var loop,count : longint;
begin
Count := Row+Col;
For Loop := 0 to Row do begin
if (Loop*Col) mod Row = 0 then dec(Count);
end;
CalcLines := Count;
end;

I think this Function works fine!
I'm not sure though if it is the kind of code you're looking for, but it works and it's small.
(it works as long as Row and Col > 0)

regards Hypo
0
Author Commented:
cheers sumant the jobs a gooden
many thanks

gandalph
0
Author Commented:
thanks hypo but i have not yet done functions the tutor wanted the program setout in procedures only
0
Commented:
Thanks a lot qandalph for your assessment.
I am very happy to know that my efforts were not invain.
Does the program work for very large numbers like 50,60 etc?
0
Commented:
The Accepted Answer is not correct.
0
Author Commented:
hi sumant the program seems to works fine except for when you input 9 columns 6 rows the programs total gives 13 where the actual total should be 11
if input is c 10 r 6 program gives 11 should be 13 then the program works fine again

i have worked out your formula i think

if c or r := 1
then
hf := -1;
if (c := r) or (r := c)
then
hf := -1;
if c + r are factors of each other
then
hf := -1;
else
hf := c + r - 2;

seem to ok on paper just the problem with the sixes

my tutor hasnt noticed the problem so no worries

cheers
gandalph
0
Author Commented:
yeh there is a minor glitch upto now it seems to work fine when total is lower than the highest factor but when total is above the higher factor then theres where the problems begin looking at the problem it must be something to do with square numbers do you think ?

gandalph
0
Author Commented:
Hello again sumant

I have found the missing formula its primes {cheers ozo thanks for nothing}
If c or r = primes and prime is multiple of 6
Then
C + r := - 4
Just got to work out the code for this little teaser no probs
IT (Information Technology) eh

Snap so iam but in programming lots of other stuff come with it mostly garbage
But what the hell only another four years to go it’s a breeze good luck and thanks again

gandalph

0
Commented:
use Lingua::Stem qw(:all);
set_locale('en');
sub keywords{
my \$file = shift;
open FILE,"<\$file" or die "can't open \$file : \$!";
my %wc=();
my %seen = ();
my %top;
my @words;
my \$paragraphs='';
local \$/='';
my @paragraphs = <FILE>;
close FILE;
for( map{/(\w['\w-]*)/g} @paragraphs ){
\$seen{(stem \$_)->[0]}++;
}
@top{(sort {\$seen{\$b} <=> \$seen{\$a} } keys  %seen)[0..9]} = ();
for( @paragraphs ){
%wc = ();
s/(\w['\w-]*)/exists \$top{(stem \$1)->[0]}?(\$wc{\$1}="<b>\$1<\/b>"):\$1/eg;
if( %wc ){
\$paragraphs .= join ' ',"<h1>",keys %wc,"</h1>:\n\$_";
}
}
\$paragraphs=~s/\$/<br>/gm;
return \$paragraphs;
}

\$paragraphs=keywords('fileparse.txt');
print \$paragraphs;

open FILE2, ">fileKW.html" or die "can't open fileKW because \$!";
print FILE2 \$paragraphs;
close FILE2;
0
Author Commented:
hi ozo
thanks for the solution to the equation
i spent some time trying to solve the problem i thought it was that if prime was multiple of six
then c + r - 4
would do the trick
but i was still getting erros
on larger numbers
the probs was having once got the total
to check it you had spend an hour drawing out the graphs to see if you had the correct totals
i asked my tutor if a simpler solution would be to try and get the program to draw the graph as well but apperntly pascal is not capableof do this.
have to wait to next year when i go on to more graphic based programming

cheers
gandalph
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.