Hmm, a little bit stuck! - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Hmm, a little bit stuck!

Posts: 13Member
Hello. I am trying to design a program in Delphi 4 that will allow me to calculate statistics based on a range of information taken from the user.

I need the program to first take a range of ages from the user and from a inbuilt menu, alow the user to perform various different functions with this information.

I need the menu to contain the following options:

1.A procedure that will ask the user to introduce the age of every person of the community.

2.A procedure/function that calculates the average age of this population.

3.A procedure/function that finds the age range with a maximum number of people.

4.A procedure/function that finds the age range with the smallest number of people.

5.A procedure that shows graphically the number of people in the different age range:

For Example:
0-5 **
6-11 ****
12-18 ***
19-35 *****
36-55 **
56-65 *
66+ *******

I am however a little confused. I am still quite new to pascal and can't get my head around it!

How would you guys do it?

Any help would be greatly apprciated!

Kind regards

John

• Posts: 6,349Member
: Hello. I am trying to design a program in Delphi 4 that will allow me to calculate statistics based on a range of information taken from the user.
:
: I need the program to first take a range of ages from the user and from a inbuilt menu, alow the user to perform various different functions with this information.
:
: I need the menu to contain the following options:
:
: 1.A procedure that will ask the user to introduce the age of every person of the community.
:
: 2.A procedure/function that calculates the average age of this population.
:
: 3.A procedure/function that finds the age range with a maximum number of people.
:
: 4.A procedure/function that finds the age range with the smallest number of people.
:
: 5.A procedure that shows graphically the number of people in the different age range:
:
: For Example:
: 0-5 **
: 6-11 ****
: 12-18 ***
: 19-35 *****
: 36-55 **
: 56-65 *
: 66+ *******
:
: I am however a little confused. I am still quite new to pascal and can't get my head around it!
:
: How would you guys do it?
:
: Any help would be greatly apprciated!
:
: Kind regards
:
: John
:
I would use a TMemo to allow the user to enter the ages. The Lines property will be the main storage. Then I would use various TButtons for the tasks.
- Average Age: loop through the Lines, convert each line and add them to a total sum. Then divide by number of lines
- Ranges: Add an array field to the TForm and loop through the Lines and increment each range counter (=element in array).
- Min/Max of range: Use the MinValue() and MaxValue() functions
- Graphs: Use the TChart Component (Additional Tab). Here is how to add the data to the chart:
[code]
with Chart1 do
begin
RemoveAllSeries;
S := TBarSeries.Create(Self);
for i := Low(AgeRanges) to High(AgeRanges) do
end;
[/code]
Chart1 = name of TChart Component
AgeRanges = name of array holding the age-range counts

I hope this helps you get started.
• Posts: 13Member
This is awesome stuff, any chance you can elaborate a little more? I am still fairly new to delphi/pascal, been told that this language is easier than others! lol!

I am trying to follow what you said in this book i bought, but i can't follow it straight.

Any code I can copy in to delphi and experiment with or any futher pointers?

Sorry to sound nooby

Kind regards!
• Posts: 6,349Member
: This is awesome stuff, any chance you can elaborate a little more? I am still fairly new to delphi/pascal, been told that this language is easier than others! lol!
:
: I am trying to follow what you said in this book i bought, but i can't follow it straight.
:
: Any code I can copy in to delphi and experiment with or any futher pointers?
:
: Sorry to sound nooby
:
: Kind regards!
:
I already gave you a part of the code. Here is the sum of an TStrings object:
[code]
var
AgesSum: double;
i: integer;
begin
AgesSum := 0;
for i := 0 to {NumberOfLines} do
AgesSum := AgesSum + StrToFloat(Ages[i]);
end;
[/code]
This code will fail if any 1 string in the TStrings isn't a number. For more help on the TStrings object, see the help files. Look through the properties of that object to find out how to get the number of strings.
Here is a possible implementation of the AgeRanges array:
[code]
TForm1 = class(TForm)
// Automatic declarations of components on the form
private
FAgeRanges: array[1..10] of integer;
public
end;
[/code]
I've included some of the automatically written code. Of course you don't have to copy into the project.
The MinValue() and MaxValue() functions should be easy enough to find out on your own. Again start in the help files.
I leave you to try to write the code to fill the FAgeRanges array. One major part of this is the if-then-else structure:
[code]
if (StrToFloat(Ages[i]) > SomeValue) and (StrToFloat(Ages[i]) <= SomeValue) then
// Increment correct age range
else if ...
// Increment correct age range
else
// Increment correct age range;
[/code]
You can create as many if-then-else's after eachother as necessary. [b]Note: place [red]NO[/red] semi-colon (;) just before the word "else".[/b]
• Posts: 13Member

Can't seem to get the code to work in my version of delphi!!

Or maybe I am just crap at programming! lol

• Posts: 6,349Member
:
: Can't seem to get the code to work in my version of delphi!!
:
: Or maybe I am just crap at programming! lol
:
:
:
If you don't show some code then, I cannot help you by indicating what is wrong. I almost handed you the entire code, which is tested and found working.
• Posts: 13Member
Yes, I agree.

I have almost got the jist of it, seems to be going ok.

Would you be so kind as to provide some code for the FAgeRanges array, this part i can't get working. Then i can see and hopefully understand by looking at your code! I find that easier.

Then just a way to calculate the averages of ages.

You wouldn't happen to have the rest of the code for that would you? Perhaps then something i can compare against my own work. Parts of which are bugged!

Just need to tie up the loose ends.

Regards
• Posts: 6,349Member
: Yes, I agree.
:
: I have almost got the jist of it, seems to be going ok.
:
: Would you be so kind as to provide some code for the FAgeRanges array, this part i can't get working. Then i can see and hopefully understand by looking at your code! I find that easier.
:
: Then just a way to calculate the averages of ages.
:
: You wouldn't happen to have the rest of the code for that would you? Perhaps then something i can compare against my own work. Parts of which are bugged!
:
: Just need to tie up the loose ends.
:
: Regards
:
The FAgeRanges is a simple integer array. The description of the ages can be done in a string array or with a case-of.
I already gave you the code for the sum of an array. All you need to code is the necessary division.
Give your code and I'm willing to show you the bugs and provide possible solutions to them.