Using arrays and loop control variables

Filling an array with a loop control variable

Page 1 of 1

3 Replies - 10125 Views - Last Post: 29 November 2007 - 05:55 PM Rate Topic: -----

#1 nathunoel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
I have included some of my code, please help me !


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_  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 189
  • Joined: 16-October 06

Re: Using arrays and loop control variables

Posted 27 November 2007 - 06:25 PM

View Postnathunoel, 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.
I have included some of my code, please help me !


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.
Was This Post Helpful? 0
  • +
  • -

#3 nathunoel  Icon User is offline

  • New D.I.C Head

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

Re: Using arrays and loop control variables

Posted 27 November 2007 - 08:15 PM

View Postrockstar_, on 27 Nov, 2007 - 06:25 PM, said:

View Postnathunoel, 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.
I have included some of my code, please help me !


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.

Was This Post Helpful? 0
  • +
  • -

#4 rockstar_  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1