[Tutor] I need help with my homework. No, really....
Steven D'Aprano
steve at pearwood.info
Wed Jul 29 15:59:45 CEST 2015
Part 2...
On Wed, Jul 29, 2015 at 04:16:58AM -0500, Lissa Hopson wrote:
> Given x as an array of [5,3] and y as an array of [3,7] perform the
> following:
>
> 1. Load array x column-wise and array y row-wise
> 2. Multiply x by y to compute array z
> 3. Compute the sum of all elements in column 2 of array x and add it to the
> sum of all elements in row 2 of y (the first row/column is 0, the second is
> 1, etc. That got me at first)
> 4. Compute the smallest element in row 1 of y
> ---using appropriate headings:
> 5. Print out matrices x, y, and z (display on screen, but y'all probably
> get that)
> 6. Print out sum and smallest element
>
> The data with which array x is loaded:
> 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1, 2, 3, 4
>
> The data with which array y is loaded:
> 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1
>
> Must use functions named as follows:
> LOADX, LOADY, COMPUTEZ, SMALLEST, SUMMATION, OUTDATA
>
> lab5.dat is simply a dat file with the data with which the arrays are
> loaded in one long line, each separated by commas.
> Thanks- in advance- no more comments after the program.
>
> This is what I have thus far:
>
> #load matrix x
> def LOADX(infile, A):
> #local variables
> n=0
> k=0
> s=0
It's hard to tell what those variables mean from the names. It may be
more useful to give them descriptive names. I think that k is the
column number, j (below) is the row number, n is an index into the
templist you generate next, and s is, well, I have no idea what s is.
You do some calculations on s, but then it never gets used, so I'm not
sure what it is for.
item = 0
column = 0
s = 0 # huh?
> templist = infile.readline().strip('\n').split(',')
To be clear, this reads the first line from the file, and one line only.
It removes the newline \n from the end, then splits on commas, and
returns a list of strings, say:
['1', '2', '3', '4', ...]
Is that what you expect?
> while (k<3):
> j=0
> while(j<5):
> A[j][k] = int(templist[n])
> s=s+A[j][k]
> j=j+1
> k=k+1
> n=n+1
Assuming s in not needed, this becomes:
while (column < 3):
row = 0
while(row < 5):
A[row][column] = int(templist[item])
row = row + 1
column = column + 1
item = item + 1
But that can't be right, because you end up processing:
column=0, row=0
column=1, row=1
column=2, row=2
and then stopping. That only gives you three numbers. What you need is
to process fifteen numbers:
column=0, row=0
column=0, row=1
column=0, row=2
column=0, row=3
column=0, row=4
column=1, row=0
...
column=2, row=4
The way to do that is to only increase the column when you've processed
all the rows. Here's a sketch, you can fill in the details:
while (column < 3):
while(row < 5):
process one element A[row][column]
add one to row
# when we get here (outdented), we've finished the inner
# while loop, but are still inside the outer while loop
add one to column
> #load matrix y
> def LOADY(infile, B):
LOADY should be almost exactly the same as LOADX, except that instead of
looping down the columns, you should loop across the rows. So:
while row < 3:
while column < 7:
but otherwise more or less the same as LOADX.
> #define computation of Z matrix
> def COMPUTEZ (A, B, C):
Try re-writing COMPUTEZ with row and columns, as above, and see if that
makes sense. I can see one obvious problem below:
> i=0
> while (i<5):
> j=0
> while (j<=7):
> k=0
> while (k<=3):
> C[i][j]= C[i][j]+ A[i][k] * B[k][j]
> k=k+1
This bit can't work, because you have a while loop where k never
advances!
while k <= 3:
process C[i][j] ...
but k doesn't change. So Python will loop forever, or until you get sick
of waiting and type Ctrl-C to halt it. You need to advance k inside the
while loop:
while k <= 3:
process C[i][j] ...
k = k + 1
Remember that the body of the while loop is defined by the *indented*
block beneath it. You do have a k = k+1 beneath the while loop, but it
isn't indented enough, so it counts as *outside* the while block.
I haven't studied it in detail, but you can try fixing that and see if
it works.
> #def summation
> def SUMMATION(x,y):
> s=0
> k=0
> j=0
> while (k<5):
> sumx=sumx + x[k][2]
> k=k+1
> while (j<7):
> sumy=sumy + y[2][j]
> j=j+1
> s=sumx + sumy
This can't work, because sumx and sumy don't have a value to start
with. You need to initialize them (perhaps zero?) first. Actually, I
don't think you need them at all. I think you can just calculate the
total directly:
total = 0
while row < 5:
total = total + x[row][2]
while column < 7:
total = total + y[2][column]
You'll need to initialize the variables, advance them, etc.
More to follow...
--
Steve
More information about the Tutor
mailing list