[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

Q and Attempt included : small Integer * small integer / 100 gives me 16890000

hi, i hope there is something small i miss here.

The Problem:
1) this is a simple mathematical equation which returns me extraordinarily big numbers
2) not major important : i dont know how to add a item to a listbox (im using delphi 5)
i have included my attempts.
i also have a link to a 50k zip file , including a JPG of the assignment.
this is not a homework exercise, i tried for at least a hour and a half and end up with jack.

http://home.ripway.com/2004-8/159283/dq7.zip


this is the question:
---------------------------------------------------------
a small program is writter to determine if groups of animals are becoming
bigger or smaller
 
this is the formula:
Next Population = Population * Growth Factor / 100

the user interface contains a spinedit : sedGrowthFactor
and a spinedit sedPopulation. when the user enter clikcs on Next
forecast button two things happen. Program calculates predicted number of animals
and displays this in sedPopulation.

thus for e.g. the user enters a growth factor of 120,
and Currentpopulation of 100, and Growth factor of 15 then
it would say:


.....
Iteration 5 Initial = 115  Final = 138
Iteration 6 Initial = 138 Final = 165
Iteration 7 Initial = 165 Final = 198 ( i get 16842838)



My Attempt:
---------------------------------------------------------
unit A1Q7;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ComCtrls, Spin;

type
  TForm1 = class(TForm)
    bmpResetClick: TBitBtn;
    sedFactor: TSpinEdit;
    sedPopulation: TSpinEdit;
    sedIteration: TSpinEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    btnNextForecast: TButton;
    lstPopulations: TListBox;
    procedure bmpResetClickClick(Sender: TObject);
    procedure btnNextForecastClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.bmpResetClickClick(Sender: TObject);
begin
        sedFactor.Value := 100;
        sedPopulation.value := 0;
        sedIteration.value := 0;
        sedPopulation.Enabled := True;
        lstPopulations.Clear;
end;

procedure TForm1.btnNextForecastClick(Sender: TObject);
var
 NextPopulation,Population,GrowthFactor,Iteration : Integer;
 s : string;
 i : integer;
begin
        Iteration := sedIteration.value;
        GrowthFactor := sedFactor.value;

        for  i := 1 to Iteration do
         begin
          Population := NextPopulation;
          //NextPopulation := strToInt(FloatToStr((Population * GrowthFactor) / 100));
          //NextPopulation := FloatToInt((Population * GrowthFactor) / 100);
          s := FloatToStr((Population * GrowthFactor) / 100);
          //lstPopulations.additem('Iteration ' + intToStr(i) + ' Initial = ' + intToStr(Population) +  ' Final = ' + inttostr(NextPopulation))
         end;

          sedPopulation.value := NextPopulation;
          sedPopulation.Enabled := False;
end;
end.
0
jxharding
Asked:
jxharding
  • 2
  • 2
1 Solution
 
jxhardingAuthor Commented:
i also tried :
population := sedPopulation.Value * sedFactor.Value div 100
0
 
pcsentinelCommented:
I think this is what you want

procedure TForm1.btnNextForecastClick(Sender: TObject);
var
 GrowthFactor,Iteration : Integer;
 Population: Integer;
 i : integer;
begin
        Iteration := sedIteration.value;
        GrowthFactor := sedFactor.value;
        Population := sedPopulation.value;
        for  i := 1 to Iteration do
           Population:=trunc((Population * GrowthFactor) / 100);
        sedPopulation.value := Population;
        sedPopulation.Enabled := False;

end;


The reason you are getting a strange number in your code is that you are not doing and initial set of NextPopulation so its just picking up any old number. In Delphi you need to set Local variables i.e. NextPopulation:=0; otherwise it will just pick up the number that occupies the memory area already


Hope this helps
0
 
pcsentinelCommented:
Also re adding items to a listbox just use

ListBox1.Items.Add('Item1');
ListBox1.Items.Add('Item2');
0
 
jxhardingAuthor Commented:
brilliant, thank you so much!!!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now