Recent

Author Topic: Find the sum of the elements of an array  (Read 797 times)

garandr

  • New Member
  • *
  • Posts: 10
Find the sum of the elements of an array
« on: November 28, 2020, 11:38:11 am »
Another problem with outputting an array to a Stringgrid
Code: Pascal  [Select][+][-]
  1. unit Unit3;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Grids;
  9.  
  10. type
  11.  
  12.   { TForm3 }
  13.  
  14.   TForm3 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     Edit2: TEdit;
  18.     Edit3: TEdit;
  19.     StringGrid1: TStringGrid;
  20.     procedure Button1Click(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.   const N=10;
  27.  var k1,k2,i,sum:INTEGER;
  28.      D : array[1..N] of integer;
  29.   Form3: TForm3;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm3 }
  36.  
  37. procedure TForm3.Button1Click(Sender: TObject);
  38. begin
  39.   k1:=StrToInt(Edit1.Text);
  40.   k2:=StrToInt(Edit2.Text);
  41.     begin
  42. for i := 1 to 10 do D[i] := random (256);
  43. //StringGrid1.Cells[i-1,0]:=FloatToStr(D[i])
  44. end;
  45.     Begin
  46.       if (k1 > n) or (k1 < 1) then
  47.       Edit3.Text:=('error')
  48.       else   if (k2 > n) or (k2 < 1) then
  49.         Edit3.Text:=('error') ;
  50.  
  51.     end;
  52.      sum:= 0;
  53.   for i:= k1 to k2  do
  54.     sum:= sum + d[i];
  55.  
  56. end;
  57. end.
  58.  
  59.  

bytebites

  • Sr. Member
  • ****
  • Posts: 369
Re: Find the sum of the elements of an array
« Reply #1 on: November 28, 2020, 12:01:30 pm »
Problem is what?

winni

  • Hero Member
  • *****
  • Posts: 2117
Re: Find the sum of the elements of an array
« Reply #2 on: November 28, 2020, 12:22:28 pm »
Hi!

The first problem is that sometimes people can read minds.
For the compilers it will take some decades.

The second is: No Sum output.

Insert in line 55:

Code: Pascal  [Select][+][-]
  1. ShowMessage (IntToStr(sum));


The third problem is:
If you don't insert something in the stringgrid it is useless.
You can delete it.

If that's all not the right answer to the unasked question,
my abilities as mind reader are at the end.

Winni

garandr

  • New Member
  • *
  • Posts: 10
Re: Find the sum of the elements of an array
« Reply #3 on: November 28, 2020, 12:29:40 pm »
Tell me how to display the generated array in StringGrid

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9016
  • FPC developer.
Re: Find the sum of the elements of an array
« Reply #4 on: November 28, 2020, 12:31:50 pm »
Probably extra begin ..end  needed after for to keep the stringgrid assignment in the loop

winni

  • Hero Member
  • *****
  • Posts: 2117
Re: Find the sum of the elements of an array
« Reply #5 on: November 28, 2020, 12:38:58 pm »
Hi!

Code: Pascal  [Select][+][-]
  1. procedure TForm3.Button1Click(Sender: TObject);
  2. begin
  3.   k1:=StrToInt(Edit1.Text);
  4.   k2:=StrToInt(Edit2.Text);
  5.   StringGrid1.RowCount := 11;  
  6. for i := 1 to 10 do
  7.   begin
  8.   D[i] := random (256);
  9.   StringGrid.Cells[0,i-1] := IntToStr(D[i]);
  10. end;
  11. //StringGrid1.Cells[i-1,0]:=FloatToStr(D[i])
  12. end;
  13.       if (k1 > n) or (k1 < 1) then
  14.       Edit3.Text:=('error')
  15.       else   if (k2 > n) or (k2 < 1) then
  16.         Edit3.Text:=('error') ;
  17.  
  18.     end;
  19.      sum:= 0;
  20.   for i:= k1 to k2  do
  21.     sum:= sum + d[i];
  22. StringGrid[0,10] := 'Sum: '+IntToStr(sum);
  23.  
  24. end;


Remark:
StringGrid1.cells[colum, row]; 
or
StringGrid1.cells[x,y]; 

Winni

wildfire

  • Jr. Member
  • **
  • Posts: 77
Re: Find the sum of the elements of an array
« Reply #6 on: November 28, 2020, 04:39:51 pm »
@garandr

I've made no attempt to solve your problem but I have to comment on your indentation and spurious begin/ends.

It makes your code difficult to read therefor making it difficult to debug.

Compare your version of Button1Click with mine at the bottom. Which is easier to read and understand? Both are functionally identical.

I know you're only just starting with Pascal, and it doesn't really care about format but if you stick to a layout that you're happy with rather than haphazardly indenting or throwing in begin/ends here and there it will help your learning process.

Quote
Code: [Select]
procedure TForm3.Button1Click(Sender: TObject);
begin
  k1:=StrToInt(Edit1.Text);
  k2:=StrToInt(Edit2.Text);
    begin
for i := 1 to 10 do D[i] := random (256);
//StringGrid1.Cells[i-1,0]:=FloatToStr(D[i])
end;
    Begin
      if (k1 > n) or (k1 < 1) then
      Edit3.Text:=('error')
      else   if (k2 > n) or (k2 < 1) then
        Edit3.Text:=('error') ;
 
    end;
     sum:= 0;
  for i:= k1 to k2  do
    sum:= sum + d[i];
 
end;

Code: [Select]
procedure TForm3.Button1Click(Sender: TObject);
begin
  k1:=StrToInt(Edit1.Text);
  k2:=StrToInt(Edit2.Text);
  for i := 1 to 10 do
    D[i] := random (256);
//StringGrid1.Cells[i-1,0]:=FloatToStr(D[i])
  if (k1 > n) or (k1 < 1) then
    Edit3.Text:=('error')
  else if (k2 > n) or (k2 < 1) then
    Edit3.Text:=('error') ;
  sum:= 0;
  for i:= k1 to k2  do
    sum:= sum + d[i];
end;
« Last Edit: November 28, 2020, 04:42:00 pm by wildfire »
Windows 10 64bit version 1809
Laz 2.0.0 FPC 3.0.4

OkobaPatino

  • Full Member
  • ***
  • Posts: 191
Re: Find the sum of the elements of an array
« Reply #7 on: December 02, 2020, 03:45:02 pm »
To complete wildfire's point, you can always use Lazarus Code Formatter. Normally you can use it using Ctrl + D.

wp

  • Hero Member
  • *****
  • Posts: 7951
Re: Find the sum of the elements of an array
« Reply #8 on: December 02, 2020, 04:39:32 pm »
To complete wildfire's point, you can always use Lazarus Code Formatter. Normally you can use it using Ctrl + D.
This is too late, in my opinion. Get accustomed to indent while you are typing. It forces you to think in terms of your code blocks.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Thaddy

  • Hero Member
  • *****
  • Posts: 10684
Re: Find the sum of the elements of an array
« Reply #9 on: December 02, 2020, 04:42:14 pm »
Why not simply use math.sum():
https://www.freepascal.org/docs-html/rtl/math/sum.html // floats
https://www.freepascal.org/docs-html/rtl/math/sumint.html // integers

Both have overloads to sum arrays.
« Last Edit: December 02, 2020, 04:49:45 pm by Thaddy »

Bart

  • Hero Member
  • *****
  • Posts: 4097
    • Bart en Mariska's Webstek
Re: Find the sum of the elements of an array
« Reply #10 on: December 02, 2020, 04:49:36 pm »
Why not simply use math.sum():
https://www.freepascal.org/docs-html/rtl/math/sum.html

Because that only accepts arrays of floating point types?

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 10684
Re: Find the sum of the elements of an array
« Reply #11 on: December 02, 2020, 04:50:29 pm »
@Bart
I added sumint() which only sums integers..... ;D :D

(It is a bit disturbing that they are not united as sum() overloads. I will file filed a bugreport as issue 0038160)
« Last Edit: December 02, 2020, 05:02:23 pm by Thaddy »

wp

  • Hero Member
  • *****
  • Posts: 7951
Re: Find the sum of the elements of an array
« Reply #12 on: December 02, 2020, 04:58:41 pm »
sum() and sumInt() operate on entire arrays. The "home work" of the OP, however, is to calculate the sum only for the values in a given range.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Thaddy

  • Hero Member
  • *****
  • Posts: 10684
Re: Find the sum of the elements of an array
« Reply #13 on: December 02, 2020, 05:03:47 pm »
sum() and sumInt() operate on entire arrays. The "home work" of the OP, however, is to calculate the sum only for the values in a given range.
That can also be done with standard provided routines and helpers.
Furthermore I only see some limit code for which OP should use low() and high()...
« Last Edit: December 02, 2020, 05:08:41 pm by Thaddy »

wp

  • Hero Member
  • *****
  • Posts: 7951
Re: Find the sum of the elements of an array
« Reply #14 on: December 02, 2020, 08:29:21 pm »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018