Recent

Author Topic: Translate function  (Read 16486 times)

kmalpha

  • New Member
  • *
  • Posts: 27
Translate function
« on: December 23, 2014, 09:07:02 pm »
Can you translate into the following code function?
Code: [Select]
program farkbul;
uses crt;
var
  j,i,x,n,s1,s2,s3,largetotal,smalltotal,zerocontrol,result:integer;
  a:array [1..100] of integer;

begin clrscr;
  n:=3;
  write('1.Enter the number');readln(a[1]);
  for i:=2 to n do
  begin
   write(i,'.Enter the number');readln(a[i]);
   for j:=1 to i do
   begin
    if a[i]<a[j] then begin x:=a[i]; a[i]:=a[j]; a[j]:=x; end;
   end;
  end;
  clrscr;
  writeln('--------------small to large-------------');
  for i:=1 to n do
  begin
  writeln('  ',a[i]);
  end;
  s1:=a[1];
  s2:=a[2];
  s3:=a[3];
  writeln('---------------large to small--------------');
  writeln('  ',s3);
  writeln('  ',s2);
  writeln('  ',s1);
  if s1=0 then begin
  zerocontrol:=s2*100+s1*10+s3;
  writeln('  ',zerocontrol);
  end;
 
  largetotal:=s1*100+s2*10+s3;
  smalltotal:=s3*100+s2*10+s1;
  writeln('  ',largetotal);
  writeln('  ',smalltotal);
  if s1=0 then sonuc:=smalltotal-zerocontrol
  else result:=largetotal-smalltotal;
 
  writeln('result: ',result);
 
 

  readln;
  end.




wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Translate function
« Reply #1 on: December 24, 2014, 05:14:44 am »
Can you translate into the following code function?

Into what? A cursory glance doesn't show any glaring errors, though the layout could do with some tidying up.
A halo is a mere circle, when does it end?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Translate function
« Reply #2 on: December 24, 2014, 05:54:06 am »
Just my guess: the OP asks to split the main-body-only program into subroutines. Probably another homework.

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Translate function
« Reply #3 on: December 24, 2014, 06:02:24 am »
If that's the case then the OP should really make some effort.

@OP, let's try and make a small start, no need to change the code for now, just present it better. eg single statements per line, proper and consistent indentation and some comments won't go amiss (even if they are in your native language).

Once you've shown that you understand what the program is doing then it should be fairly simple to split it into procedures and functions.
A halo is a mere circle, when does it end?

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Translate function
« Reply #4 on: December 24, 2014, 08:52:56 am »
Code: [Select]
  if s1=0 then sonuc:=smalltotal-zerocontrol

One thing I will point out, where do you declare sonuc?
A halo is a mere circle, when does it end?

kmalpha

  • New Member
  • *
  • Posts: 27
Re: Translate function
« Reply #5 on: December 24, 2014, 09:52:57 am »
This must be the main body
Code: [Select]
write('1.Enter the number');readln(a[1]);
write(i,'.Enter the number');readln(a[i]);
this should be in the function part of us
Code: [Select]
program farkbul;
uses crt;
var
  j,i,x,n,s1,s2,s3,largetotal,smalltotal,zerocontrol,result:integer;
  a:array [1..100] of integer;

begin clrscr;
  n:=3;
 
  for i:=2 to n do
  begin
   for j:=1 to i do
   begin
    if a[i]<a[j] then begin x:=a[i]; a[i]:=a[j]; a[j]:=x; end;
   end;
  end;
  clrscr;
  writeln('--------------small to large-------------');
  for i:=1 to n do
  begin
  writeln('  ',a[i]);
  end;
  s1:=a[1];
  s2:=a[2];
  s3:=a[3];
  writeln('---------------large to small--------------');
  writeln('  ',s3);
  writeln('  ',s2);
  writeln('  ',s1);
  if s1=0 then begin
  zerocontrol:=s2*100+s1*10+s3;
  writeln('  ',zerocontrol);
  end;
 
  largetotal:=s1*100+s2*10+s3;
  smalltotal:=s3*100+s2*10+s1;
  writeln('  ',largetotal);
  writeln('  ',smalltotal);
  if s1=0 then sonuc:=smalltotal-zerocontrol
  else result:=largetotal-smalltotal;
 
  writeln('result: ',result);
 
 

  readln;
  end.

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Translate function
« Reply #6 on: December 24, 2014, 10:19:59 am »
OK let's work on your indentation first, it'll make your code more readable to yourself and others.

After every begin indent 2 spaces, and take them back when you have a matching end.

eg
Code: [Select]
if I_Must_Indent then begin
  writeln( 'Hey, This is looking better already');
  writeln( 'Why didn''t I do this before' )
end;

Now do the same with your above code and post the results.
A halo is a mere circle, when does it end?

kmalpha

  • New Member
  • *
  • Posts: 27
Re: Translate function
« Reply #7 on: December 24, 2014, 10:45:14 am »
Does that make it so hard?
I do not know much english

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Translate function
« Reply #8 on: December 24, 2014, 10:48:14 am »
Trust me if you work on your layout it will make Pascal a lot easier to learn and understand. It will also make it easier for others to help when you get into more tricky problems.
A halo is a mere circle, when does it end?

kmalpha

  • New Member
  • *
  • Posts: 27
Re: Translate function
« Reply #9 on: December 24, 2014, 11:13:02 am »
but you know that I need urgent help please

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Translate function
« Reply #10 on: December 24, 2014, 11:46:42 am »
The solution is simple, and I'm sure if you set out your layout properly you'll see it for yourself. As others have said, we're not here to do your homework for you. A little effort on your part is required.
A halo is a mere circle, when does it end?

kmalpha

  • New Member
  • *
  • Posts: 27
Re: Translate function
« Reply #11 on: December 24, 2014, 12:00:02 pm »
complete this task.
I just had to turn the function finishes
help me please

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Translate function
« Reply #12 on: December 24, 2014, 12:02:50 pm »
Not until you help yourself I'm afraid.

If you do it my way, you'll feel a lot happier as you'll have done most of the work yourself and hopefully learnt a few things as well. Programming in any language isn't as simple as copying and pasting other peoples work.

From what I can see your instructor is trying to teach you a fundamental part of Pascal and it is vital that you completely understand it, you won't get that by having others do your work.


@ Any Mods, can someone move this to the beginners board. I would like to help kmalpha and am willing to stick with him long term but don't want to clutter up the general board.
« Last Edit: December 24, 2014, 12:13:38 pm by wildfire »
A halo is a mere circle, when does it end?

kmalpha

  • New Member
  • *
  • Posts: 27
Re: Translate function
« Reply #13 on: December 24, 2014, 12:44:36 pm »
This program, I did somehow I endeavor
I've had some problems and I can not function.
Please help ensure applications are completed

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Translate function
« Reply #14 on: December 24, 2014, 12:55:08 pm »
No problem,

Start with what I asked you earlier, get your layout right. Indent when required, keep one statement to a line, separate conditions from actions (see code snippet).

Once your code is more presentable, we can take another look at it and I promise you will find it easier to understand.

Code: [Select]
// BAD
if ThisIsBad then WriteLn( 'why did I ever think this was a good idea'); ThisIsBad := True;

// Good
if ThisIsGood then
  WriteLn( 'Yep, it looks better and it''s obvious the next statement isn''t part of the condition');
ThisIsGood := True;
A halo is a mere circle, when does it end?

 

TinyPortal © 2005-2018