0

C programming

write a program in C that prints the square of each element of a 2D matrix using a function that is called from main

25th Oct 2018, 1:38 PM
mozzie
mozzie - avatar
5 Answers
+ 3
NicolasHM, I think you probably forgot to update the for loops in the square_matrix function, in its current state, it will loop for 2 lines and 3 columns, disregarding the values passed for <lines> and <cols> arguments. Other than that it was good solution : )
25th Oct 2018, 3:14 PM
Ipang
+ 2
NicolasHM cool! : )
25th Oct 2018, 4:31 PM
Ipang
+ 1
#include <stdio.h> #include <stdlib.h> #include <math.h> void square_matrix(int lines, int cols, int matrix[lines][cols]){ for(i=0; i<lines;i++){ for(j=0; j<cols; j++){ printf("%lf\n", pow(matrix[i][j], 2)); } } } int main(){ int matrix[2][3], i, j; for(i=0; i<2;i++){ for(j=0; j<3; j++){ printf("\n m[%d][%d]=",i, j); scanf("%d", &matrix[i][j]); } } square_matrix(2, 3 matrix); return 0; }
25th Oct 2018, 1:50 PM
NicolasHM
NicolasHM - avatar
+ 1
Ipang that's true. Fixed! Thanks 🖒
25th Oct 2018, 4:29 PM
NicolasHM
NicolasHM - avatar
0
thank you
25th Oct 2018, 2:10 PM
mozzie
mozzie - avatar