Recent

Author Topic: Find the sum of elements belonging to the interval from A to B  (Read 1034 times)

garandr

  • New Member
  • *
  • Posts: 10
Find the sum of elements belonging to the interval from A to B
« on: December 01, 2020, 06:04:08 pm »
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.     Label1: TLabel;
  20.     StringGrid1: TStringGrid;
  21.     procedure Button1Click(Sender: TObject);
  22.   private
  23.  
  24.   public
  25.  
  26.   end;
  27.  
  28. var      C : array[1..10] of integer;
  29.          i,kol,sum:integer; A,B:integer;
  30. Form3: TForm3;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm3 }
  37.  
  38. procedure TForm3.Button1Click(Sender: TObject);
  39. begin
  40.        A:=StrToInt(Edit1.Text);
  41.        B:=StrToInt(Edit2.Text);
  42.        sum := 0;
  43.        kol:=0;
  44.        for i := 1 to 10 do
  45.  begin
  46.        C[i] := random (100);
  47.        StringGrid1.Cells[i-1,0] := IntToStr(C[i]);
  48.        if (A <= C[i]) and (B>= C[i]) then inc (kol);
  49.  end;
  50.        Edit3.Text:=( IntToStr(kol)+ ' element between '+IntToStr(A)+' and '+IntToStr(B) );
  51.        if (C[i] >= A) and (C[i] <= B) then
  52.        sum := sum + C[i] ;
  53.        if sum > 0 then
  54.             Edit3.Text:=IntToStr(sum);
  55. end;
  56.  
  57.  
  58.  
  59.  
  60. end.
  61.              
  62.  
  63.  
« Last Edit: December 01, 2020, 06:59:11 pm by garandr »

bytebites

  • Hero Member
  • *****
  • Posts: 624
Re: Find the sum of elements belonging to the interval from A to B
« Reply #1 on: December 01, 2020, 06:30:51 pm »
Format your code first. Random indentation is mess.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Find the sum of elements belonging to the interval from A to B
« Reply #2 on: December 01, 2020, 06:38:55 pm »
Other than the hideous indentation, that you forgot to (re)initialize kol, and the unnecessary repeated check, what's the problem?
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

garandr

  • New Member
  • *
  • Posts: 10
Re: Find the sum of elements belonging to the interval from A to B
« Reply #3 on: December 01, 2020, 07:00:09 pm »
Format your code first. Random indentation is mess.
Ok :-\

garandr

  • New Member
  • *
  • Posts: 10
Re: Find the sum of elements belonging to the interval from A to B
« Reply #4 on: December 01, 2020, 07:15:57 pm »
Other than the hideous indentation, that you forgot to (re)initialize kol, and the unnecessary repeated check, what's the problem?
What's up with kol?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Find the sum of elements belonging to the interval from A to B
« Reply #5 on: December 01, 2020, 07:44:34 pm »
What's up with kol?

Nothing, sorry. Looking more closely I see it is initialized right after sum; don't know how I missed that ... :-[

The question still holds, though, what's the problem?
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

garandr

  • New Member
  • *
  • Posts: 10
Re: Find the sum of elements belonging to the interval from A to B
« Reply #6 on: December 01, 2020, 07:53:41 pm »
What's up with kol?

Nothing, sorry. Looking more closely I see it is initialized right after sum; don't know how I missed that ... :-[

The question still holds, though, what's the problem?

Elements between A and B are indicated incorrectly, and the amount is not considered

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Find the sum of elements belonging to the interval from A to B
« Reply #7 on: December 01, 2020, 08:37:56 pm »
Let me fix the indentation to see what's going on:

Code: Pascal  [Select][+][-]
  1. procedure TForm3.Button1Click(Sender: TObject);
  2. begin
  3.   A:=StrToInt(Edit1.Text);
  4.   B:=StrToInt(Edit2.Text);
  5.   sum := 0;
  6.   kol:=0;
  7.   for i := 1 to 10 do
  8.   begin
  9.     C[i] := random (100);
  10.     StringGrid1.Cells[i-1,0] := IntToStr(C[i]);
  11.     if (A <= C[i]) and (B>= C[i]) then inc (kol);
  12.   end;      
  13.   Edit3.Text:=( IntToStr(kol)+ ' element between '+IntToStr(A)+' and '+IntToStr(B) );
  14.   if (C[i] >= A) and (C[i] <= B) then
  15.     sum := sum + C[i] ;
  16.   if sum > 0 then
  17.     Edit3.Text:=IntToStr(sum);
  18. end;

By the indentation of the begin/end pair you can see what is included inside the "for" loop. What follows the "end" is outside the loop. But you see that there is "i code" outside the loop: "if C[ i] > = >"... -- I guess this must be inside the loop because the loop variable is well-defined only inside the loop.

Paolo

  • Sr. Member
  • ****
  • Posts: 499
Re: Find the sum of elements belonging to the interval from A to B
« Reply #8 on: December 01, 2020, 08:54:15 pm »
it should be: (you also forget to join the two times call to Edit3.text, if sum was > 0 in your code you will never see the first string "..element between..")

Code: Pascal  [Select][+][-]
  1. procedure TForm3.Button1Click(Sender: TObject);
  2. begin
  3.   A:=StrToInt(Edit1.Text);
  4.   B:=StrToInt(Edit2.Text);
  5.   sum := 0;
  6.   kol:=0;
  7.   for i := 1 to 10 do
  8.   begin
  9.     C[i] := random (100);
  10.     StringGrid1.Cells[i-1,0] := IntToStr(C[i]);
  11.     if (A <= C[i]) and (B>= C[i]) then begin
  12.       inc (kol);
  13.       sum := sum + C[i] ;
  14.     end;
  15.   end;      
  16.   Edit3.Text:=( IntToStr(kol)+ ' element between '+IntToStr(A)+' and '+IntToStr(B) );
  17.   if sum > 0 then
  18.     Edit3.Text:=Edit3.Text+', and the sum is '+IntToStr(sum);
  19. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: Find the sum of elements belonging to the interval from A to B
« Reply #9 on: December 02, 2020, 09:20:40 pm »
Other than the hideous indentation, that you forgot to (re)initialize kol, and the unnecessary repeated check, what's the problem?
What's up with kol?
Besides that kol here means something different from the below remarks:
Dunno. I lost a bit of interest. Will contact Vladimir et. al. Will have to do that for Christmas anyway...
But it still works both for 32/64 bit Windows - including wince - and it is stable. (the FPC part, don't know about the mirror components since I rarely used them.)

Also note you can NOT mix Kol with rtl/fcl/lcl. It is a complete framework. You will loose its size benefits if you do and run into problems
« Last Edit: December 02, 2020, 09:32:02 pm by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018