fork download
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. int** setMatrix(int graph_size) {
  5. char ch;
  6. /*Выделение памяти под матрицу смежности*/
  7. int** matrix = (int**)malloc(graph_size * sizeof(int*));
  8. for (int i = 0; i < graph_size; i++) {
  9. matrix[i] = (int*)malloc(sizeof(int*) * graph_size);
  10. }
  11. /* Ввод матрицы смежности */
  12. enterAgain:
  13. printf("\nEnter matrix\n");
  14. for (int i = 0; i < graph_size; i++) {
  15. for (int j = 0; j < graph_size; j++) {
  16. int temp;
  17. //int rc = scanf("%d", &temp);
  18. if (!scanf("%d", &temp)) {
  19. fflush(stdin);
  20. do {
  21. ch = getchar();
  22. } while ((ch != EOF) && (ch != '\n'));
  23. goto enterAgain;
  24. }
  25. else
  26. {
  27. matrix[i][j] = temp;
  28. }
  29. }
  30. }
  31. return matrix;
  32. }
  33.  
  34. int main(void) {
  35. // your code goes here
  36. int ** M = setMatrix(2);
  37.  
  38. printf("\nOutput:\n");
  39. for (int i = 0; i < 2; i++)
  40. {
  41. for (int j = 0; j < 2; j++)
  42. {
  43. printf("%d\t", M[i][j]);
  44. }
  45. printf("\n");
  46. }
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 5544KB
stdin
a1b23c
11
12
21
22
stdout
Enter matrix

Enter matrix

Output:
11	12	
21	22