Go to Post Publishing your code and designs = good. - MrRoboSteve [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 17-04-2006, 12:05
lesmayers lesmayers is offline
Registered User
no team
 
Join Date: Apr 2006
Location: kent
Posts: 2
lesmayers is an unknown quantity at this point
Calculative help !!

Hi

My friend has a problem with this delphi code..
She wants a set of integer numbers that people enter into boxes and results give total..Can anyone help.
This is what she has sent:

procedure TfrmAddFor.btnAddClick(Sender: TObject);
var
Number : string;
Count, Sum : integer;
begin
Count := 1;
Sum := 0;
for Count := 1 to 20 do;
Number := InputBox('Enter number', 'Number', ''); //create InputBox
Count := Count + 1;
Sum := Sum + StrToInt(Number);
lblIterationNumber.Caption := 'Iteration Number:' + Number;
lblSum.Caption := 'Cumulative Sum:' + IntToStr(Sum);
end;
end.

Tx for any help
  #2   Spotlight this post!  
Unread 17-04-2006, 12:07
Brandon Martus's Avatar Unsung FIRST Hero
Brandon Martus Brandon Martus is offline
busy.
AKA: B. Slash Kamen
no team
 
Join Date: May 2001
Rookie Year: 1998
Location: Nevada, TX USA
Posts: 5,271
Brandon Martus has a reputation beyond reputeBrandon Martus has a reputation beyond reputeBrandon Martus has a reputation beyond reputeBrandon Martus has a reputation beyond reputeBrandon Martus has a reputation beyond reputeBrandon Martus has a reputation beyond reputeBrandon Martus has a reputation beyond reputeBrandon Martus has a reputation beyond reputeBrandon Martus has a reputation beyond reputeBrandon Martus has a reputation beyond reputeBrandon Martus has a reputation beyond repute
Send a message via ICQ to Brandon Martus Send a message via AIM to Brandon Martus Send a message via Yahoo to Brandon Martus
Re: Calculative help !!

Someone may still be able to help you, but please read this thread as well:
http://www.chiefdelphi.com/forums/sh...ad.php?t=29944



Quote:
Originally Posted by lesmayers
Hi

My friend has a problem with this delphi code..
She wants a set of integer numbers that people enter into boxes and results give total..Can anyone help.
This is what she has sent:

procedure TfrmAddFor.btnAddClick(Sender: TObject);
var
Number : string;
Count, Sum : integer;
begin
Count := 1;
Sum := 0;
for Count := 1 to 20 do;
Number := InputBox('Enter number', 'Number', ''); //create InputBox
Count := Count + 1;
Sum := Sum + StrToInt(Number);
lblIterationNumber.Caption := 'Iteration Number:' + Number;
lblSum.Caption := 'Cumulative Sum:' + IntToStr(Sum);
end;
end.

Tx for any help
__________________
Brandon Martus
e-mail
  #3   Spotlight this post!  
Unread 17-04-2006, 13:07
Tristan Lall's Avatar
Tristan Lall Tristan Lall is offline
Registered User
FRC #0188 (Woburn Robotics)
 
Join Date: Aug 2001
Rookie Year: 1999
Location: Toronto, ON
Posts: 2,484
Tristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond repute
Re: Calculative help !!

Quote:
Originally Posted by lesmayers
Hi

My friend has a problem with this delphi code..
She wants a set of integer numbers that people enter into boxes and results give total..Can anyone help.
This is what she has sent:

procedure TfrmAddFor.btnAddClick(Sender: TObject);
var
Number : string;
Count, Sum : integer;
begin
Count := 1;
Sum := 0;
for Count := 1 to 20 do;
Number := InputBox('Enter number', 'Number', ''); //create InputBox
Count := Count + 1;
Sum := Sum + StrToInt(Number);
lblIterationNumber.Caption := 'Iteration Number:' + Number;
lblSum.Caption := 'Cumulative Sum:' + IntToStr(Sum);
end;
end.

Tx for any help
Alright, as Brandon said, this isn't a Borland Delphi forum—here, Delphi refers to the auto parts maker Delphi, a sponsor of several FIRST teams.

As for your code, you've got all sorts of trouble with the use of the variable Count. First of all, Delphi (but not all versions of Pascal) will initialize any ordinal type variable (e.g. integer, etc.) to 0 when the variable is created. Furthermore, all versions of Pascal (Delphi included) will automatically set the for loop variable to the lower bound (in your case 1), when the loop begins. So you don't have to specify Count := 1. Also, you don't need Count := Count + 1 because it will automatically increment the for loop variable for each iteration of the loop. (I don't remember what will happen if you do what you're doing. If I had to guess, I think that it will allow it, but it might confuse the loop, and maybe cause it to loop infinitely, or terminate prematurely.) The bottom line is, don't ever try to change the for loop variable during the loop.

Now that I look again, I realize that there's a problem with the loop itself. It's not what you mean that counts, it's what you actually write.... You've declared it as:
Code:
for Count := 1 to 20 do;
Number := InputBox('Enter number', 'Number', ''); //create InputBox
Count := Count + 1;
Sum := Sum + StrToInt(Number);
lblIterationNumber.Caption := 'Iteration Number:' + Number;
lblSum.Caption := 'Cumulative Sum:' + IntToStr(Sum);
end;
In fact, this won't do anything substantial, because of the semicolon after the loop's do keyword. (This means, "for Count equals 1 to 20, do nothing, then execute the rest of the statements once only".) Furthermore, you've got a begin keyword missing (to match the end). You likely mean:
Code:
for Count := 1 to 20 do
begin
	Number := InputBox('Enter number', 'Number', ''); //create InputBox
	Inc(Sum, StrToInt(Number));
	lblIterationNumber.Caption := 'Iteration Number: ' + Number;
	lblSum.Caption := 'Cumulative Sum: ' + IntToStr(Sum);
end;
Incidentally, you can write Inc(Sum, StrToInt(Number)) instead of Sum := Sum + StrToInt(Number), if you'd like. It does the same thing, but may be more readable, depending on your own preference.

From a more basic point of view, your specification calls for a set of integer numbers, with the output being the sum of those numbers. Depending on the version of Delphi that you're using (because the 64-bit version is possibly different), an integer is a 32-bit signed variable, meaning that it has possible values between 2 147 483 647 and -2 147 483 648. Intuitively, you should know that 20 × 2 147 483 647 > 2 147 483 647, so an integer variable can't necessarily hold the sum of 20 integers, without overflowing. Unless this situation is impossible (e.g. if it was explicitly stated in the problem to neglect this), you must account for it by using a larger type to store the sum. If you ever write a programming contest, you'll quickly learn that this is a common way to trip up unsuspecting competitors. (I suspect that declaring Sum as an Int64 will clear up matters, but you need to consult the help file about using Int64—it isn't quite a normal ordinal variable. Also, you may need Int64ToStr(Sum), though I'm not positive on that.)

Edit: Two things. Now that I'm home, I checked the Delphi help file, and found that IntToStr is overloaded for Int64 input—meaning that a different version of the function (with the same name) is called if you pass it Int64 rather than integer data. This lets it handle the two different data structures (integer and Int64) in a convenient way. In other words, use IntToStr(Sum), and don't use Int64ToStr(Sum), which doesn't exist.

I also just realized something else about Int64: be careful adding integer quantities and assigning them to Int64 variables; you're OK here, because Sum is Int64 to begin with, but if you were to add the following, you'd get bad results:
Code:
Sum := 2000000000 + 147483648 //i.e. bigger than Integer, but OK for an Int64
//Returns an error, or bad data, depending on the version of Pascal, because the (implicitly) integer quantity on the RHS of the equation overflows.
In fact, you need to typecast one as an Int64, so that the two integer quantities aren't treated as integers (which leads to an overflow). Use Int64 as a function name, like this:
Code:
Sum := Int64(2000000000) + 147483648 //i.e. bigger than Integer, but OK for an Int64
//Returns 2147483648 correctly.
Just things to watch out for....

Last edited by Tristan Lall : 17-04-2006 at 23:06. Reason: More info on Int64
  #4   Spotlight this post!  
Unread 17-04-2006, 13:35
Greg Ross's Avatar
Greg Ross Greg Ross is offline
Grammar Curmudgeon
AKA: gwross
FRC #0330 (Beach 'Bots)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Hermosa Beach, CA
Posts: 2,245
Greg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond repute
Send a message via AIM to Greg Ross Send a message via Yahoo to Greg Ross
Re: Calculative help !!

Quote:
Originally Posted by lesmayers
Hi

My friend has a problem with this delphi code..
She wants a set of integer numbers that people enter into boxes and results give total..Can anyone help.
This is what she has sent:

Code:
procedure TfrmAddFor.btnAddClick(Sender: TObject);
var
  Number : string;
  Count, Sum : integer;
begin
  Count := 1;
  Sum := 0;
  for Count := 1 to 20 do;
    Number := InputBox('Enter number', 'Number', '');    //create InputBox
    Count := Count + 1;
    Sum := Sum + StrToInt(Number);
    lblIterationNumber.Caption := 'Iteration Number:' +  Number;
    lblSum.Caption := 'Cumulative Sum:' + IntToStr(Sum);
  end;
end.
Tx for any help
It would be EXTREMELY helpful if you could describe the nature of the problem she is having. I.e. What is it doing or not doing that is unexpected? Is the compile failing, or is it failing at run time? Is there an error message?

But offhand, I can see a couple problems: She need a BEGIN following the DO (with no ";" there.) This SHOULD cause a compile error, since you have an unmatched END. And she probably doesn't want the "Count := Count + 1;", since the FOR...DO automatically increments Count.
__________________
Greg Ross (The Grammar Curmudgeon formerly known as gwross)
S/W Engineer, Team 330, the Beach 'Bots
<--The Grammar Curmudgeon loves this cartoon.
“Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" Hunter S. Thompson
"Playing a practical joke means doing something mean and calling it funny." Me
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 23:06.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi