Probelm with results

P

pillip

I am using data stored in a 20-20 array to find the mean of the rows and
columns, the results being stored in two other arrays. My problem is that
printf just gives the result for the first row and doesnt increment i. In
addition the integer j does not increment in printf either. (i, j being the
number of rows and columns respectively). Here is part of the code:

for (j=0; j<column; j++) {
sumrows=0;
for (i=0; i<rows; i++) {
sumrows += ara[j];
}

for (i=0; i<rows; i++) {
sumcolumn=0;
sum=0;
for (j=0; j<column; j++) {
sum += ara[j]*j;
sumcolumn += ara[j];
}

meanc[column] = sum/sumcolumn;
meanr[rows] = sumrows/rows;


printf("%d\t %13.10f\t %d\t %13.10f\n", i, meanrows[rows], j,
meancolumn[column]);
}
}
 
E

Eric Sosman

pillip said:
I am using data stored in a 20-20 array to find the mean of the rows and
columns, the results being stored in two other arrays. My problem is that
printf just gives the result for the first row and doesnt increment i. In
addition the integer j does not increment in printf either. (i, j being the
number of rows and columns respectively). Here is part of the code:
[corrected as per follow-up and re-indented for clarity]

for (j=0; j<column; j++) {
sumrows=0;
for (i=0; i<rows; i++) {
sumrows += ara[j];
}

for (i=0; i<rows; i++) {
sumcolumn=0;
sum=0;
for (j=0; j<column; j++) {
sum += ara[j]*j;
sumcolumn += ara[j];
}

meancolumn[column] = sum/sumcolumn;
meanrows[rows] = sumrows/rows;

printf("%d\t %13.10f\t %d\t %13.10f\n", i, meanrows[rows], j,
meancolumn[column]);
}
}


There are several things wrong here. Perhaps the most
conspicuous error is in the way the loops are nested: you
surely don't intend to have a loop on `j' inside another
loop on `j', do you? I'd say it was just a case of getting
the closing } brackets in the wrong places -- but then the
positioning of the mean calculations and of the printf()
call look sort of screwy, too ...

Rather than try to fix up what you've got -- it's so far
off the mark that "fix" isn't a verb that's easily applied --
I'll exhibit a substitute implementation.

/* Initialize totals to zero.
*/
for (j = 0; j < column; ++j)
meancolumn[j] = 0.0;

for (i = 0; i < rows; ++i)
meanrows = 0.0;

/* Add each matrix element to its column total
* and to its row total. We're just accumulating
* raw totals at this point; we'll turn them into
* means later on.
*/
for (j = 0; j < column; ++j) {
for (i = 0; i < rows; ++i) {
meancolumn[j] += ara[j];
meanrows += ara[j];
}
}

/* Scale totals by row or column count, and print
* them out.
*/
printf ("Column means:");
for (j = 0; j < column; ++j) {
meancolumn[j] /= rows;
printf (" %g", meancolumn[j]);
}
printf ("\n");

printf ("Row means:");
for (i = 0; i < rows; ++i) {
meanrows /= column;
printf (" %g", meanrows);
}
printf ("\n");

(The code could be written more compactly, but this version
may make for easier study.)
 
A

Artie Gold

pillip said:
I forgot to name the two arrays correctly in the last code. Here is the
correction.

for (j=0; j<column; j++) {
sumrows=0;
for (i=0; i<rows; i++) {
sumrows += ara[j];
}

for (i=0; i<rows; i++) {
sumcolumn=0;
sum=0;
for (j=0; j<column; j++) {
sum += ara[j]*j;
sumcolumn += ara[j];
}

meancolumn[column] = sum/sumcolumn;
meanrows[rows] = sumrows/rows;


printf("%d\t %13.10f\t %d\t %13.10f\n", i, meanrows[rows], j,
meancolumn[column]);
}
}

Please post a minimal *compilable* snippet. Without the appropriate
declarations it is not possible to tell what's going on.

For example:

How is `ara' defined?
How is `meanrows' defined?
How is `meancolumn' defined?

HTH,
--ag
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,137
Messages
2,570,797
Members
47,342
Latest member
eixataze

Latest Threads

Top