473,387 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Probelm with results

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[i][j];
}

for (i=0; i<rows; i++) {
sumcolumn=0;
sum=0;
for (j=0; j<column; j++) {
sum += ara[i][j]*j;
sumcolumn += ara[i][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]);
}
}
Nov 14 '05 #1
4 1148
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[i][j];
}

for (i=0; i<rows; i++) {
sumcolumn=0;
sum=0;
for (j=0; j<column; j++) {
sum += ara[i][j]*j;
sumcolumn += ara[i][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]);
}
}

Nov 14 '05 #2
pillip wrote:

for (j=0; j<column; j++) { for (j=0; j<column; j++) { } }


You have concentric loops with the same control variable.

--
pete
Nov 14 '05 #3
pillip wrote:
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[i][j];
}

for (i=0; i<rows; i++) {
sumcolumn=0;
sum=0;
for (j=0; j<column; j++) {
sum += ara[i][j]*j;
sumcolumn += ara[i][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

--
Artie Gold -- Austin, Texas
Nov 14 '05 #4
pillip wrote:

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[i][j];
}

for (i=0; i<rows; i++) {
sumcolumn=0;
sum=0;
for (j=0; j<column; j++) {
sum += ara[i][j]*j;
sumcolumn += ara[i][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[i] = 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[i][j];
meanrows[i] += ara[i][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[i] /= column;
printf (" %g", meanrows[i]);
}
printf ("\n");

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

--
Er*********@sun.com
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CharitiesOnline | last post by:
Hello, I have set this script up to add paging to a search results page. Which on the first page works fine. I calculates how many pages there should be depending on the number of results returned...
1
by: fremenusul | last post by:
am trying to have XML data show results in a combobox (Which works) and when the combobox has a selection, change the text in a label field. I am following the video from...
0
by: Pengyu Hong | last post by:
Hi, I host an AxWebBrowser control in my application. Some web sites have Javascript that will open a new window and show results in the new window. I used the following codes to create a new...
1
by: Don | last post by:
I am new to Indexing Services, have been researching the MS Site as well as web articles on DevHood, etc. I have set up a seperate catalog ("KnowledgeBase") on Win XP with a number of files. I am...
0
by: Rob | last post by:
I doubt this is the best way to do it, but what I came up with was to hide the XML in an HTML Comment then edit the file deleting the HTML stuff and keep the XML results. If anyone has a better...
1
by: Tim::.. | last post by:
Hi... I'm having a big problem with a datagrid that obtains data from 2 different locations... Active Directory and SQL Database The data is inserted into a datatable and the sorted by a...
4
by: jaYPee | last post by:
I have downloaded some source code but I want this to convert the results into datagrid. dr = cmd.ExecuteReader() '**************************************** ' SHOW THE RESULTS...
0
by: preeti13 | last post by:
i have a two tables employeenominations and reason if someone storing a data first time it will store into the employeenominations table if name is already exist it will store into the reason table...
1
by: oaklander | last post by:
Is it okay to keep creating queries with the same ResultSet Object reference (results)? Here is what I am currently using in my Database statements with Oracle and everything works but was wondering...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.