Recent

Author Topic: Array  (Read 7450 times)

JohnCena

  • Newbie
  • Posts: 1
Array
« on: March 18, 2017, 10:43:43 am »
I think I solve it thanks
« Last Edit: March 18, 2017, 12:09:27 pm by JohnCena »

sky_khan

  • Guest
Re: Array
« Reply #1 on: March 18, 2017, 11:06:27 am »
<mode yoda> laziness i sense in you </mode>

What is this ? A homework ? I think you could make it yourself if you really have tried. Its not that hard.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Array
« Reply #2 on: March 18, 2017, 12:23:00 pm »
I think I solve it thanks

I've just finished writing it.  :'(

Here, I show you the code. You didn't mention the requirements, so I wrote it using many more advanced things like OOP style, TStringList, ShowMessage. It means you can't submit it to your teacher if it is your homework, but actually if you're really want to be a programmer, you can learn many things from the code.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Dialogs;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     FResult: TStringList;
  16.     procedure FormActivate(Sender: TObject);
  17.   private
  18.     procedure ShowTheResult;
  19.     procedure PrepareData;
  20.     procedure FreeData;
  21.     procedure ProcessSingleLine(Input, Target: integer);
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. const
  28.   Input = 4; // <-------- Put your input here
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.FormActivate(Sender: TObject);
  37. begin
  38.   PrepareData;
  39.   ShowTheResult;
  40.   FreeData;
  41. end;
  42.  
  43. procedure TForm1.ShowTheResult;
  44. var
  45.   S: AnsiString;
  46.   i: integer;
  47. begin
  48.   S := '';
  49.   for i := 0 to (FResult.Count-1) do
  50.     S := S + FResult.Strings[i];
  51.   if (S = '') then S := '< nothing >';
  52.   ShowMessage(S);
  53. end;
  54.  
  55. procedure TForm1.PrepareData;
  56. var
  57.   LineTodo: integer;
  58.   i       : integer;
  59. begin
  60.   FResult := TStringList.Create;
  61.   LineTodo := Input;
  62.   if (LineTodo >= 100) then LineTodo := 100;
  63.   for i := 1 to LineTodo do ProcessSingleLine(i, LineTodo);
  64. end;
  65.  
  66. procedure TForm1.FreeData;
  67. begin
  68.   FResult.Free;
  69. end;
  70.  
  71. procedure TForm1.ProcessSingleLine(Input, Target: integer);
  72. var
  73.   S: AnsiString;
  74.   i: integer;
  75. begin
  76.   S := '';
  77.   for i := 1 to Target do
  78.     if (i > Input) then
  79.       S := IntToStr(i) + ' ' + S
  80.     else
  81.       S := IntToStr(Input) + ' ' + S;
  82.   FResult.Add(S + LineEnding);
  83. end;
  84.  
  85. end.
  86.  

Note: it won't compile using Turbo Pascal, you need Lazarus/FPC or Delphi.
« Last Edit: March 18, 2017, 12:24:31 pm by Handoko »

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Array
« Reply #3 on: March 18, 2017, 01:59:15 pm »
I think I solve it thanks

Plese do not remove the original post.
Now it is totally unclear what this topic was about.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Array
« Reply #4 on: March 18, 2017, 02:01:37 pm »
And we can not improve the answers. Which I am sure I can in this case.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

sky_khan

  • Guest
Re: Array
« Reply #5 on: March 18, 2017, 03:55:26 pm »
Believe me, there was nothing to improve. Handoko's answer is already overkill :)
Question was how to produce output below (using loop)

4 3 2 1
4 3 2 2
4 3 3 3
4 4 4 4


Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Array
« Reply #6 on: March 18, 2017, 04:12:42 pm »
I was referring to the syntax. That's all. That can be improved as of 3.0.2
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Array
« Reply #7 on: March 18, 2017, 04:18:16 pm »
It is sad if the OP doesn't come back.

This forum is a good place to learn Pascal programming. Students can learn a lot and ask advices here if they get troubles with their school lessons, as long as they don't ask for the code for their homework.

Yep, I intentionally make it overkill :D
Because:

- OP didn't mention the requirement
- To avoid my code being copied and submitted to teacher
- Nothing wrong with unit CRT, but they should try something more modern
- Unit CRT can't run properly on my Linux system
- To show OP OOP (object oriented programming) style
- To show OP how to use ShowMessage, TStringList, IntToStr
« Last Edit: March 18, 2017, 04:32:06 pm by Handoko »

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Array
« Reply #8 on: March 18, 2017, 04:19:11 pm »
Believe me, there was nothing to improve. Handoko's answer is already overkill :)
Question was how to produce output below (using loop)

4 3 2 1
4 3 2 2
4 3 3 3
4 4 4 4
How about
4 3 2 1  // already taken care of
3 2 1 4  // not!
2 3 4 1 // not!
1 2 3 4 // not!

This is not even complete....
That's also part of the solution according to my maths.... Which says 4!X3!X2!X1!
Permutations on a matrix go for every possible combination in every plane.....
« Last Edit: March 18, 2017, 04:20:48 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Array
« Reply #9 on: March 18, 2017, 04:19:47 pm »
Please show us your code.

---edited---
I mean to speak to SkyKhan. Have you write anything? Please show us what you have already have.

---edited---
Sorry, I mistakenly think SkyKhan is the OP.
« Last Edit: March 18, 2017, 04:27:07 pm by Handoko »

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Array
« Reply #10 on: March 18, 2017, 04:22:05 pm »
Please show us your code.
Yup, why did he remove it? But I remember vaguely it was about permutations. I just added the formula.
Exclamation mark here means permutation. E.g. 4! means 4*3*2*1, 3! means 3*2*1 etc.
[edit] sorry I quoted Handoko, but there's nothing left to quote... The answer is 24 combinations.
« Last Edit: March 18, 2017, 04:29:46 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Array
« Reply #11 on: March 18, 2017, 04:31:12 pm »
I guess the OP felt shame because of asking someone to write the code for his homework. Please see the #2 post by SkyKhan.

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Array
« Reply #12 on: March 18, 2017, 04:37:02 pm »
You mean the homework(post #2) or the incorrect answer (his #2 post)?
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Array
« Reply #13 on: March 18, 2017, 04:42:57 pm »
Because SkyKhan think it was a homework and sense the OP's laziness. Some minutes after SkyKhan posted the the second post, the OP removed the original question.

The OP said too few words about the task. He just said if the input is 4 than the result is:

4 3 2 1
4 3 2 2
4 3 3 3
4 4 4 4

I guess the lesson is about array and looping. We can consider this topic closed. But if anyone is interested with this challenge, so we can keep continue.

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Array
« Reply #14 on: March 18, 2017, 04:45:57 pm »
I remember distinctly, and I am old but my brains aren't.... he wanted permutation, which should render  a lot more than a 4X4 square.... I leave it up to OP what he means, but I am not The Donald  >:D >:(
(But your input is always appreciated, Handoko)
(I am one of the persons that remember almost everything almost...., it's a curse..)
« Last Edit: March 18, 2017, 04:51:23 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018