Link to home
Start Free TrialLog in
Avatar of gandalph
gandalph

asked on

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’);
          readln (columns,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



                
Avatar of sumant032199
sumant032199
Flag of United States of America image

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 : ');
          readln (columns,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;
     readln;
end.
Avatar of gandalph
gandalph

ASKER

sorry sumant's
but the program is only working for certain inputs
input 6 columns 3 rows total should be 5
your total gives 7

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
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
Avatar of ozo
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
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;
    answer : char;
procedure get_numbers;

    begin
          write('Enter the number of columns and rows : ');
          readln (columns,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');
        readln (answer);
        answer := upcase (answer);
    end;

begin
     repeat
         get_numbers;
         calculate_lines;
         try_again;
     until answer = 'N'
end.

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

gandalph
try 60 columns 140 rows
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
going to add your bit
 
{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

tutor hinted something about factors
he said one formula would work for all
inputs he also said it was going to snow but it never did

cheers
ASKER CERTIFIED SOLUTION
Avatar of sumant032199
sumant032199
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
cheers sumant the jobs a gooden
many thanks

gandalph
thanks hypo but i have not yet done functions the tutor wanted the program setout in procedures only
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?
The Accepted Answer is not correct.
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
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
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
So your doing your first year as an engineering student of
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
     
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;
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