fork(4) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int main(int argc, char**argv){
  6. int matriz[5][5];
  7. int i, j;
  8. int soma=0;
  9.  
  10. srand( time( NULL ) );
  11.  
  12. printf( "Matriz:\n" );
  13. for( i = 0; i < 5; i++ ){
  14. for( j = 0; j < 5; j++ ){
  15. matriz[i][j] = 5 + rand() % 5;
  16. printf( "%3d", matriz[i][j] );
  17. if( i==j ){
  18. soma += matriz[i][j];
  19. }
  20. }
  21. printf( "\n" );
  22. }
  23. printf( "Soma da diagonal principal: %d", soma );
  24. return 0;
  25. }
Success #stdin #stdout 0s 2112KB
stdin
Standard input is empty
stdout
Matriz:
  6  7  7  9  7
  5  9  6  7  6
  9  7  5  8  9
  8  5  7  5  6
  7  6  5  9  6
Soma da diagonal principal: 31