?
Solved

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

Posted on 2005-03-29
4
Medium Priority
?
181 Views
Last Modified: 2010-04-05
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
Comment
Question by:jxharding
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Author Comment

by:jxharding
ID: 13651672
i also tried :
population := sedPopulation.Value * sedFactor.Value div 100
0
 
LVL 11

Accepted Solution

by:
pcsentinel earned 2000 total points
ID: 13651726
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
 
LVL 11

Expert Comment

by:pcsentinel
ID: 13651740
Also re adding items to a listbox just use

ListBox1.Items.Add('Item1');
ListBox1.Items.Add('Item2');
0
 

Author Comment

by:jxharding
ID: 13651854
brilliant, thank you so much!!!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question