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
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 : )
+ 2
NicolasHM cool! : )
+ 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;
}
+ 1
Ipang that's true. Fixed! Thanks đ
0
thank you