# Using arrays and loop control variables

Page 1 of 1

## 3 Replies - 10829 Views - Last Post: 29 November 2007 - 05:55 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=37978&amp;s=715ae857fbdc2a3f0b5d1e8c0276774b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 nathunoel

Reputation: 0
• Posts: 2
• Joined: 12-October 07

# Using arrays and loop control variables

Posted 22 November 2007 - 08:03 PM

This is an exercise I am struggling with. I can not get my results to show up like the array below and my computations for columns B and C are just not woking out.

EXO------------------------

Write a python program that prompts the user to enter 5 unique positive numbers into array A.
Then utilize the values of A to calculate the values for arrays B & C using a loop ( "lcv" is the loop control variable).
The values for array B are calculated based on the values entered into array A as follows:
B[lcv] = A[lcv] + ( lcv + 2) * 6.5
The program should compute the values of array C as follows:
C[lcv] = (B[lcv] / A[lcv] ) * 3.14
The program should display the arrays in the following manner:
A B C
99 99 99
99 99 99
99 99 99

CODE-----------------------

# Prompt the user to enter 5 unique positive numbers into array A.
# use the values of A to calc. values for arrays B & C
# use a loop (w/ a lcv).
# values for array B: B[lcv] = A[lcv] + ( lcv + 2) * 6.5
# values for array C: C[lcv] = (B[lcv] / A[lcv] ) * 3.14

from numarray import *

def getInputs(): # Creating the Arrays/ declaring A as 'f')
cols=3
rows=5
n=0

print "Enter 5 UNIQUE positive numbers."
while n<5 and inp>0:
inp= raw_input("Enter a number:")
if inp>0:
inp2=raw_input("Enter second number:")
if
n=n+1

array=[inp1,inp2,inp3,inp4,inp5]

array=zeros([rows,cols], type='f')
arrayB=zeros([rows,cols], type='f')
arrayC=zeros([rows,cols], type='f')
return array,rows

#--------------------------------------------------------
def doCalc(array): # Calc. column B and C
rows=5
n=0
for row in range(0,rows,1):
array[row,2]= array[row,0] + ( row + 2) * 6.5 # arrayB
array[row,3] = array[row,1] + ( row + 2) * 6.5 # arrayC

return array,rows

#--------------------------------------------------------
def doOutput(array): # Printing the Array

format01='%8.2f %8.2f %8.2f'
print format01 % ( "A", "B", "C")
print format01 % ( "--------", "--------", "--------")

for row in range (0,rows,1):
print "%8.2f %8.2f %8.2f" % (array, arrayB, arrayC)

def main():
getInputs()
doCalc(array)
doOutput(array)

main()

Is This A Good Question/Topic? 0

## Replies To: Using arrays and loop control variables

### #2 rockstar_

Reputation: 34
• Posts: 189
• Joined: 16-October 06

## Re: Using arrays and loop control variables

Posted 27 November 2007 - 06:25 PM

nathunoel, on 22 Nov, 2007 - 08:03 PM, said:

This is an exercise I am struggling with. I can not get my results to show up like the array below and my computations for columns B and C are just not woking out.

EXO------------------------

Write a python program that prompts the user to enter 5 unique positive numbers into array A.
Then utilize the values of A to calculate the values for arrays B & C using a loop ( "lcv" is the loop control variable).
The values for array B are calculated based on the values entered into array A as follows:
B[lcv] = A[lcv] + ( lcv + 2) * 6.5
The program should compute the values of array C as follows:
C[lcv] = (B[lcv] / A[lcv] ) * 3.14
The program should display the arrays in the following manner:
A B C
99 99 99
99 99 99
99 99 99

CODE-----------------------

# Prompt the user to enter 5 unique positive numbers into array A.
# use the values of A to calc. values for arrays B & C
# use a loop (w/ a lcv).
# values for array B: B[lcv] = A[lcv] + ( lcv + 2) * 6.5
# values for array C: C[lcv] = (B[lcv] / A[lcv] ) * 3.14

from numarray import *

def getInputs(): # Creating the Arrays/ declaring A as 'f')
cols=3
rows=5
n=0

print "Enter 5 UNIQUE positive numbers."
while n<5 and inp>0:
inp= raw_input("Enter a number:")
if inp>0:
inp2=raw_input("Enter second number:")
if
n=n+1

array=[inp1,inp2,inp3,inp4,inp5]

array=zeros([rows,cols], type='f')
arrayB=zeros([rows,cols], type='f')
arrayC=zeros([rows,cols], type='f')
return array,rows

#--------------------------------------------------------
def doCalc(array): # Calc. column B and C
rows=5
n=0
for row in range(0,rows,1):
array[row,2]= array[row,0] + ( row + 2) * 6.5 # arrayB
array[row,3] = array[row,1] + ( row + 2) * 6.5 # arrayC

return array,rows

#--------------------------------------------------------
def doOutput(array): # Printing the Array

format01='%8.2f %8.2f %8.2f'
print format01 % ( "A", "B", "C")
print format01 % ( "--------", "--------", "--------")

for row in range (0,rows,1):
print "%8.2f %8.2f %8.2f" % (array, arrayB, arrayC)

def main():
getInputs()
doCalc(array)
doOutput(array)

main()

Post your error. Without the python indentation, I can't tell anything from your code.

### #3 nathunoel

Reputation: 0
• Posts: 2
• Joined: 12-October 07

## Re: Using arrays and loop control variables

Posted 27 November 2007 - 08:15 PM

rockstar_, on 27 Nov, 2007 - 06:25 PM, said:

nathunoel, on 22 Nov, 2007 - 08:03 PM, said:

This is an exercise I am struggling with. I can not get my results to show up like the array below and my computations for columns B and C are just not woking out.

EXO------------------------

Write a python program that prompts the user to enter 5 unique positive numbers into array A.
Then utilize the values of A to calculate the values for arrays B & C using a loop ( "lcv" is the loop control variable).
The values for array B are calculated based on the values entered into array A as follows:
B[lcv] = A[lcv] + ( lcv + 2) * 6.5
The program should compute the values of array C as follows:
C[lcv] = (B[lcv] / A[lcv] ) * 3.14
The program should display the arrays in the following manner:
A B C
99 99 99
99 99 99
99 99 99

CODE-----------------------

# Prompt the user to enter 5 unique positive numbers into array A.
# use the values of A to calc. values for arrays B & C
# use a loop (w/ a lcv).
# values for array B: B[lcv] = A[lcv] + ( lcv + 2) * 6.5
# values for array C: C[lcv] = (B[lcv] / A[lcv] ) * 3.14

from numarray import *

def getInputs(): # Creating the Arrays/ declaring A as 'f')
cols=3
rows=5
n=0

print "Enter 5 UNIQUE positive numbers."
while n<5 and inp>0:
inp= raw_input("Enter a number:")
if inp>0:
inp2=raw_input("Enter second number:")
if
n=n+1

array=[inp1,inp2,inp3,inp4,inp5]

array=zeros([rows,cols], type='f')
arrayB=zeros([rows,cols], type='f')
arrayC=zeros([rows,cols], type='f')
return array,rows

#--------------------------------------------------------
def doCalc(array): # Calc. column B and C
rows=5
n=0
for row in range(0,rows,1):
array[row,2]= array[row,0] + ( row + 2) * 6.5 # arrayB
array[row,3] = array[row,1] + ( row + 2) * 6.5 # arrayC

return array,rows

#--------------------------------------------------------
def doOutput(array): # Printing the Array

format01='%8.2f %8.2f %8.2f'
print format01 % ( "A", "B", "C")
print format01 % ( "--------", "--------", "--------")

for row in range (0,rows,1):
print "%8.2f %8.2f %8.2f" % (array, arrayB, arrayC)

def main():
getInputs()
doCalc(array)
doOutput(array)

main()

Post your error. Without the python indentation, I can't tell anything from your code.

### #4 rockstar_

Reputation: 34
• Posts: 189
• Joined: 16-October 06

## Re: Using arrays and loop control variables

Posted 29 November 2007 - 05:55 PM

WTF? Hey, preview your post before you submit it. --:-) If you actually post your error, or put your code in the code tags, I'll be able to help you