I need help with my C code. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I need help with my C code.

I stuck with these arrays and pointers. https://code.sololearn.com/cV20S9pk9EB1/?ref=app I need to calculate average value of each row of a 2d array and save it in one new 1d array. I got some outputs like 35982 which are definitely wrong

23rd Nov 2019, 8:13 PM
Lighton
Lighton - avatar
4 Answers
0
https://code.sololearn.com/c2SALiIIUkYf/?ref=app This should do the trick.... I am a good coder in c but i can tell that using int can give a +-2 error. If in the end you need integer output only, then cast the array in the end to int before printing
25th Nov 2019, 10:02 AM
Anirudh Sharma
Anirudh Sharma - avatar
0
My main 2d array should be float as well? I think averages need floats because it store the result of a division. Right?
25th Nov 2019, 11:44 AM
Lighton
Lighton - avatar
0
Absolutely!! This happens because division neglects decimal values in case of int datatype. For eg in an array of 3 values 4, 7 and 10, after division with 3, you're left with 1, 2 and 3. And summing them up you get 6 while your answer should be 7!!
25th Nov 2019, 11:56 AM
Anirudh Sharma
Anirudh Sharma - avatar
0
Anyhow you can cast the final result to int if you want to avoid the zeroes in the end
25th Nov 2019, 11:58 AM
Anirudh Sharma
Anirudh Sharma - avatar