Qbasicnews.com
February 22, 2020, 07:47:14 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2
  Print  
Author Topic: Addition Loop Problem  (Read 6884 times)
Hybr!d
Member
*
Posts: 33



« on: January 27, 2006, 08:00:17 AM »

Hello guys. I have another problem this time.

I am trying to make a program that keeps on adding the number that is found.

Example:

1) User put input of how many time loop occurs.

2) 1 is first number then is added by itself. Then keeps going on like this

1 + 1 = 2
2 + 2 = 4
4 + 4 = 8

3) Then stops at the number they specified.
Logged

img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
SSC
Guru
**
Posts: 288



WWW
« Reply #1 on: January 27, 2006, 08:08:37 AM »

Code:
screen 12
dim loop_max as integer
dim number as integer
dim result as integer
result = 0

print "Enter number of loops"
input loop_max
print "Enter number to be multiplied"
input number

for count = 1 to loop_max
  result += number
next
print result
sleep

if you have any question reguarding this just ask =)
Logged

url=http://www.smithcosoft.com]SmithcoSoft Creations[/url]
"If you make it idiot proof, someone will make a better idiot" - Murphy's Law
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #2 on: January 27, 2006, 08:20:06 AM »

That's not quite what he asked for.

Code:
n% = 1
Input "Iterations"; iterations%

For i% = 1 To iterations%
   n% = n% + n%
Next i%

Print n%


Anyway, no need for loops:

Code:
Input "Iterations"; iterations%

Print 2^iterations%


Anyway, I'm not for posting code if the requester doesn't demonstrate he has, at least, tried. I did this time 'cause SSC's solution was for a different problem Smiley
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Hybr!d
Member
*
Posts: 33



« Reply #3 on: January 27, 2006, 09:08:11 AM »

nathan none of them are working. The second one works but only squares it by 2. Im looking for like it to go up by 1 it self so when it finds a answer it does that one then when it gets that answer another.
Logged

img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #4 on: January 27, 2006, 09:56:54 AM »

My progs do what you specify in your first post. Explain it correctly, and we'll be able to help you.

Quote from: "Hybr!d"
1) User put input of how many time loop occurs.

2) 1 is first number then is added by itself. Then keeps going on like this

1 + 1 = 2
2 + 2 = 4
4 + 4 = 8

3) Then stops at the number they specified.


1, 2 and 3 just describe an algorithm to find a power of two.

Have you tried to solve this [strike]homework[/strike] problem? Show us what you got so far and we'll be able to help you fill in the gaps Smiley

And I don't understand this text:

Quote
Im looking for like it to go up by 1 it self so when it finds a answer it does that one then when it gets that answer another.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Hybr!d
Member
*
Posts: 33



« Reply #5 on: January 27, 2006, 10:09:30 AM »

nathan I mean like htis more clearly.

Say this is the program

******************************************************
Welcome to Program

How many Loops or whatever:  4

1+1=2

2+2=4

4+4=8

8+8=16

*****************************************************

Thats what I mean.. Gets the answer then it adds it self then does it again. Till the end of specification.
Logged

img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
SSC
Guru
**
Posts: 288



WWW
« Reply #6 on: January 27, 2006, 10:33:20 AM »

ya that what i made my code example to do, but i realized that you need to replace
Code:
result += number
with
Code:
number += number
so that its always adding to its self (including the first time)
Logged

url=http://www.smithcosoft.com]SmithcoSoft Creations[/url]
"If you make it idiot proof, someone will make a better idiot" - Murphy's Law
Hybr!d
Member
*
Posts: 33



« Reply #7 on: January 27, 2006, 10:56:56 AM »

SSC your code doesnt seem to work!! Ive got no idea what to do
Logged

img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #8 on: January 27, 2006, 11:34:03 AM »

Code:
input loops
number = 1
for i = 1 to loops
   number = number + number
   print number
next


If I understand exactly what he wrote... He wanted it to print the result every iteration.
Logged
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #9 on: January 27, 2006, 12:04:17 PM »

Our programs do exactly what the guy is asking for, only that they just give the final answer instead of showing every step.

If you want the program to output every step,  you just have to add a PRINT to the loop, as KiZ mentioned. The changes are very simple and I think you should try to do them. We are not here to do your homework, but to help you doing it Smiley
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
axipher
Ancient Guru
****
Posts: 544



« Reply #10 on: January 27, 2006, 12:49:34 PM »

Do exactly what KiZ said

Code:
input loops
number = 1
for i = 1 to loops
   number = number + number
   print number
next


And if it closes right away add Sleep to the end.
Logged
Dio
I hold this place together
*****
Posts: 874



« Reply #11 on: January 27, 2006, 03:47:43 PM »

i think he means this:

Code:
A = 1
do
A = A + A
? A
loop until [insert END number here ex: loop until a = 1048576]
Logged

quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Hybr!d
Member
*
Posts: 33



« Reply #12 on: January 27, 2006, 08:37:01 PM »

Hey Kiz thanx it works a charm.. I tried to put a save function to save all numbers to a text file... I did this but it only saves the last number generated.

Code:
CLS
INPUT loops
number = 1
FOR i = 1 TO loops
   number = number + number
   PRINT number
NEXT
PRINT
INPUT "Save as: ", sf$
OPEN sf$ FOR OUTPUT AS #1
WRITE #1, number
CLOSE #1



Also how would I show the 1 + 1 = 2 instead of showing 2?
Logged

img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
yetifoot
Ancient Guru
****
Posts: 575



« Reply #13 on: January 27, 2006, 10:52:32 PM »

Code:
Dim ostr As String
CLS
INPUT "Loopa?  : ", loops
number = 1
INPUT "Save as : ", sf$
OPEN sf$ FOR OUTPUT AS #1
FOR i = 1 TO loops
   ostr = str(number) + " + " + str(number) + " = "  + str(number + number)
   Print #1, ostr
   Print ostr
   number = number + number
NEXT
CLOSE #1
Logged

EVEN MEN OF STEEL RUST.
Hybr!d
Member
*
Posts: 33



« Reply #14 on: January 27, 2006, 11:44:50 PM »

yetifoot I dont understand this line in your code

Code:

ostr = str(number) + " + " + str(number) + " = "  + str(number + number)


and in qbasic it says it  is a type mismatch in " + ".

Could anyone please explain?
Logged

img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
Pages: [1] 2
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!