Efficient Matrix Column Sum and Count: Solve Urgent Deadline in Hours

  • Thread starter Thread starter brad sue
  • Start date Start date
  • Tags Tags
    Matrix
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 3K views
brad sue
Messages
270
Reaction score
0
Hi,
Please I need help to do the following:

add the elements of each colunm of a matrix and
Count the number of colunm whose the sum is less than 1.

I did this: i is the indice of line , j =indice of colunm.. size = dimension of the square matrix.

for(j=0;j<size;j++)
{ for(i=0;i<size;i++)
{sum=sum+A[j];
if (sum<1)
count=count+1;}
}


The compiler says I am wrong . I have to turn this in few hours...
please help me!

Thanks
B
 
Physics news on Phys.org
What do you mean the compiler "says you are wrong"? Is it throwing a syntax error?

Is sum a global or local variable? How is it initialized? How about count?

Is the assignment to come up with one sum total for all columns, or one total per column?
 
berkeman said:
What do you mean the compiler "says you are wrong"? Is it throwing a syntax error?

Is sum a global or local variable? How is it initialized? How about count?

Is the assignment to come up with one sum total for all columns, or one total per column?

The compiler(Unix) say that a semicolon is missing after the second for loop. sum and count are a local variables.

In fact the assignemt says , "if the sum of each colunm ( for the whole matrix ) is less than 1 print "OK".

Example, if it is a 2 x 2 matrix,

0.2 0.25
0.3 .4

it should print "OK" since the sum of each colunm is less than 1.



This is why I use the count. if count = size ( size of the matrix) ,then each the colunm has a sum less than 1 , so I can print "OK".

sum and count are initialzed to zero at the beginning of the program.(sum=count=0;)

Thanks
 
You should probably post the whole code because if it is a syntax error it might have propagated from previous code. Sounds like it.
 
-Job- said:
You should probably post the whole code because if it is a syntax error it might have propagated from previous code. Sounds like it.
the problem is that I am working directly in unix lab . there is no copy/paste in the editor I am using..
 
the problem is that I am working directly in unix lab . there is no copy/paste in the editor I am using..
That's a problem with debugging then -- many errors are simple typos. When you retype it, you're likely to get it right that time, and thus we cannot catch your typo. :frown:
 
brad sue said:
the problem is that I am working directly in unix lab . there is no copy/paste in the editor I am using..
Then just pipe a listing to a file that you can post.
 
I am not sure if i have reached u in time
i think the syntax should be as follows

count=0;
for(j=0;j<size;j++)
{sum=0;
for(i=0;i<size;i++)
{sum=sum+A[j];}
if (sum<1)
count=count+1;}