Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Immediate Window statement? 3

Status
Not open for further replies.

Waspit

Technical User
Aug 4, 2000
3
US
I need to know how to construct a one-statement Qbasic program in the Immediate Window that computes the sum of the numbers, 1 to 100, directly instead of iteratively. I'm learning the FOR and NEXT statements and know how to use them to do this. Now I have to do it in the Immediate Window with only one statement.<br><br>Can someone please help me?<br><br>Thanks <br><br>Louie
 
FOR R = 1 TO 100: S = S + R: NEXT: PRINT S<br><br>Just use a colon to separate the lines. Good luck with the learning process.<br> <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
Thank you very much, I've been goin' nuts tryin to figure this out.<br><br>Louie<br><A HREF="mailto:wasp_1@msn.com">wasp_1@msn.com</A>
 
If you don't care about math don't read this message...;-)<br><br>My math teacher told me how Pascal was smarter than his teacher, so the teacher said, &quot;Go in the corner and don't come out until you have added the numbers from 1 to 100 up.&quot;&nbsp;&nbsp;So he did in a couple minutes and the teacher asked how he did it.&nbsp;&nbsp;This is how:<br><FONT FACE=monospace><br>1&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;9&nbsp;&nbsp;....<br>+&nbsp;&nbsp;&nbsp;+&nbsp;&nbsp;&nbsp;+&nbsp;&nbsp;&nbsp;+&nbsp;&nbsp;&nbsp;+&nbsp;&nbsp;&nbsp;+&nbsp;&nbsp;&nbsp;+&nbsp;&nbsp;&nbsp;+&nbsp;&nbsp;&nbsp;+<br>100 99&nbsp;&nbsp;98&nbsp;&nbsp;97&nbsp;&nbsp;96&nbsp;&nbsp;95&nbsp;&nbsp;94&nbsp;&nbsp;93&nbsp;&nbsp;92 ....<br>==================================<br>101<br></font><br>There are 50 pairs of 101, so 50*101=5050, the answer.<br><br>I just love math, don't you?<br>
 
That's cool, Rogue2, but it will be hard to express in the immediate window. Remember, <i>&quot;Show your work.</i>&quot; <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
I just might join, I'm new at programming, this is my first class. I'm taking online courses at Bryant & Stratton and most of what we learn comes from a book. We have to read a chapter then do some problems at the end. <br><br>Louie
 
But if you must be technical about the meaning of work being something that will show the answer by typing in an d running code in the immediate window, how about<br>? 50*101<br><br>or in long code,<br>PRINT 50*101<br><br>Ah the joys of math!<br><br>
 
Wumpus, there is truth in what you say but PRINT 50 * 101 doesn't allow the range to vary.<br><FONT FACE=monospace>L = 100: PRINT L / 2 * (L + 1)</font> seems to allow more flexibility. Perhaps somebody has a short formula to calculate the sum of <i>any</i> range?<br><br>I <b><font color=red>hate</b></font> math so I'll leave the exercise to sombody who appreciates its finer points.<br><img src=http:\\vorpalcom.com\images\anibrain.gif><br><br> <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
Well, what you said only works if it's an even number.&nbsp;&nbsp;For an odd number, I get to draw another diagram...JOY! :-D<br><br>1&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;3<br><br>+&nbsp;&nbsp;&nbsp;+&nbsp;&nbsp;&nbsp;+<br><br>?&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;4<br>=========<br>?&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;7<br><br>The equation for and odd range is this:<br><br>((range+2)*((range/2)-.5))+1<br><br>The sum of the pair (5 and 2, 3 and 4, etc.) is range+2 all the way across.&nbsp;&nbsp;To get the number of pairs, you take half of the range rounded down.&nbsp;&nbsp;We know it will end in .5 because the range is odd, so we just subtract .5.&nbsp;&nbsp;We multiply them just like we did 50 and 101.&nbsp;&nbsp;Now add 1 to compensate for that 1 left after the pairing.&nbsp;&nbsp;For example, 105 is ((105+2)*((105/2)-.5))+1, or<br>((107)*((52.5)-.5))+1, or<br>(107*52)+1, or<br>5564+1, or<br>5565.<br><br>There, I showed my work!<br><br>EVEN:&nbsp;&nbsp;(range*2)*(range+1)<br>ODD:&nbsp;&nbsp;&nbsp;((range+2)*((range/2)-.5))+1
 
That's good work, Rogue2.<br><br>I would give you a TipMaster vote if you had signed on as a Tek-Tips member.<br><br>I would give you another vote if you had shown a way to express it in the <i>immediate</i> window.<br><br>I'm not playing here. I have an interest in the same solution you appear to be playing with. How do you calculate the sum of any range?<br><br>Please respond with an answer that will work in the <i>immediate</i> window. I know it can be done. I haven't found a way to do it. <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
i've did it if you want to try it:<br><br><br>range = 100: IF range / 2 = INT(range / 2) THEN : r = range / 2 * (range + 1): PRINT r ELSE r = ((range + 2) * ((range / 2) - .5)) + 1: PRINT r<br><br> <p>miguel<br><a href=mailto:migoul@hotmail.com>migoul@hotmail.com</a><br><a href= page (not been updated recently)</a><br>
 
lived,<br><br>That only works if RANGE=100.&nbsp;&nbsp;You need to add an INPUT statement for the user.<br><br>Here's the modified version for the immediate window:<br><br><b><font color=red>INPUT &quot;What's the range&quot;;range</font> : IF range / 2 = INT(range / 2) THEN : r = range / 2 * (range + 1): PRINT r ELSE r = ((range + 2) * ((range / 2) - .5)) + 1: PRINT r</b><br><br>now if you put in either 100 or 105 you'll get the desired results.<br><br>ps - Lived, I'm giving you a TipMaster vote (cause you did most of the work)<br><br>pps - <b>General question</b>...I put in 10 for the range and it came back with 55.&nbsp;&nbsp;Why?&nbsp;&nbsp;10 / 2 = 5 and 5 * 10 = 50. Where's the other 5 coming in from?<br><br>thanks,<br>MiggyD <p> <br><a href=mailto: > </a><br><a href= > </a><br>Secrete of life: If Husband = Happy then Wife = Unhappy. If Wife = Happy then Husband = Unhappy. If Parents = Happy then Kids = Unhappy. If Kids = Happy then Parents = Unhappy.
 
Thank you for the answer.&nbsp;&nbsp;I read the code at least 3 times but just couldn't see it.&nbsp;&nbsp;I guess that sometimes another pair of eyes does help.<br><br>I think I need a new prescription or something.<br><br>Thanks again. <p> <br><a href=mailto: > </a><br><a href= > </a><br>Secrete of life: If Husband = Happy then Wife = Unhappy. If Wife = Happy then Husband = Unhappy. If Parents = Happy then Kids = Unhappy. If Kids = Happy then Parents = Unhappy.
 
Hmmm... nothing so far addresses the problem of finding the sum of <i>any</i> range. Say the range was 33 to 102?<br> <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
look at this one, it find the sum of any range:<br><br><br>INPUT &quot;What's the lower #&quot;; range1: INPUT &quot;What's the max #&quot;; range2: incr = range1 - 1: FOR r = range1 TO range2: incr = incr + 1: total = total + incr: NEXT: PRINT range1; &quot;to&quot;; range2; &quot;=&quot;; total<br><br><br>then i think i resolved the problem <p>miguel<br><a href=mailto:migoul@hotmail.com>migoul@hotmail.com</a><br><a href= page (not been updated recently)</a><br>
 
<i>Hey!</i> I think I've been tricked. Miguel just took the line of code I posted originally, changed some names, added a couple of input statements and posted it as a solution.<br><br>Hmmph. I feel like I've been Microsofted!<br><br>Bah, humbug.<br><br>Just joking &lt;grin&gt;. This forum has been a little dead recently and I had hoped to stir up some action. Code reuse has always been an acceptable practice and it still remains the programmer's most potent tool for getting an app up and running in the least time.<br><br>Now, could somebody please express Miguel's FOR/NEXT loop as a formula? That is what I <i>really</i> need.<br> <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
REM FONCTION<br>CLS<br>INPUT &quot;First: &quot;, First<br>INPUT &quot;Last: &quot;, Last<br>PRINT &quot;REP:&quot;; (First + Last) * ((Last - (First - 1)) / 2)<br><br>ENJOY :)<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top