![]() |
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 |
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:
|
Re: Calculative help !!
Quote:
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;Code:
for Count := 1 to 20 doFrom 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 Int64Code:
Sum := Int64(2000000000) + 147483648 //i.e. bigger than Integer, but OK for an Int64 |
Re: Calculative help !!
Quote:
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. |
| All times are GMT -5. The time now is 03:21. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi