• Status: Solved
• Priority: Medium
• Security: Public
• Views: 264

# array

given an array x[10][10] with initial values, compute the sum of all:-
b)the square of the elements in the even rows(i.e., rows 2,4,6,8,10)
c)the square root of the elements in the odd columns(i.e.,columns 1,3,5,7,9).
0
samuelccs
1 Solution

Commented:
#include <math.h>

//assuming x[row][column]
//s is squares, sr is square root, return value is diagonal elements

int calculate(int *x[10][10], int *s, int *sr) {
int i,j,k, sum=0;
*s=0; *sr=0;
for(i=0,j=9;i<10;i++ ,j--){
sum+=*x[i][i]+*x[i][j];
for(k=0;k<9;k++) if(i%2)      *s+=*x[i][k]**x[i][k]; else *sr+=sqrt(*x[k][i]);
}
return(sum-=*x[4][4]);
}
0

Commented:
Hey ScrapDog, keep doing this guys homework and you'll have to stay after school :)
0

Commented:
Hehehe, someone obviously hasn't been paying attention during his programming classes.
0

Commented:
Class project folks - I set it - beware!
0

Commented:
Well you can do this .. it is shorter
Question (A)
printing from top-left to bottom right
for index = 1 to MAX do
print(numbers[index][index])
end for
printing from bottom-left to top-right
for diag=1 to MAX do
print(numbers[MAX-diag+1][diag])
enfor

Question (B)
index2=0
for index=1 to MAX
sum=sum+(numbers[index][index2]*numbers[index][index2])
index=index+2
index2++
endfor

Question (C)
I think the answer above is quite ok to follow. Most importantly must understand the concept
0

Author Commented:
i am sorry, sir. i just want to have more opinion and more alternatives to do one programming work. i'll promise that i will not copy the direct answer
0

Commented:
samuel, your teacher gotcha right at the place of crime :)

0

Commented:

int sum = 0;

for (int i = 0; i < 10; i++)
sum += array[i][i];

b)the square of the elements in the even rows(i.e., rows 2,4,6,8,10)

int sum = 0;

for (int j = 2; j < 10; j += 2) {
for (int i = 0; i< 10; i++)
sum += sqr(array[j][i]);
}

c)the square root of the elements in the odd columns(i.e.,columns 1,3,5,7,9).

int sum = 0;

for (int j = 1; j < 10; j += 2) {
for (int i = 0; i< 10; i++)
sum += sqrt(array[j][i]);
}

Hope this helps.

-Viktor
--Ivanov
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}