FocusMePlss

Vector => Matrice POINTER

Nov 29th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. int* alocare(int n, int m, int k) {
  7.     int *p;
  8.     if ((p = (int*)malloc(sizeof(int)*n*m*k)) == NULL) {
  9.         printf("Nu exista spatiu in memorie!\n");
  10.         _getch();
  11.         exit(0);
  12.     }
  13.     return p;
  14. }
  15.  
  16. int maxim(int n, int m,int *p) {// pentru matrice bidimensionala
  17. int max, *p;
  18. max = *p;
  19. for (int i = 0;i < n*m;i++) {
  20.     if (max <= *p);
  21.     max = *p;
  22.     p++;
  23. }
  24. //printf("%d", max);
  25. return max;
  26. }
  27.  
  28. void init(int n, int m, int k, int *p) {
  29.     int j;
  30.     int *d;
  31.     d = p;
  32.     for (j = 0;j < k;j++) {
  33.         for (int i = 0;i < n*m;i++) {
  34.  
  35.             printf("[%d][%d][%d]= ", j , i / m, i % m);
  36.             scanf_s("%d", p);
  37.             p++;
  38.         }
  39.     }
  40.     printf("\n");
  41.     p = d;
  42.     printf("Matricea este:");
  43.     for (j = 0;j < k;j++) {
  44.         for (int i = 0; i < n*m; i++) {
  45.             if (i%m == 0)
  46.                 printf("\n");
  47.             for (int sp = 0;sp < j;sp++)
  48.                 printf(" ");
  49.             printf("%d ", *p);
  50.             p++;
  51.         }
  52.     }
  53. }
  54.  
  55. void main() {
  56.     int n, m, k;
  57.     int *p;
  58.  
  59.     printf("Linii: ");
  60.     scanf_s("%d", &n);
  61.     printf("Coloane: ");
  62.     scanf_s("%d", &m);
  63.     printf("Sectiuni: ");
  64.     scanf_s("%d", &k);
  65.     printf("\n");
  66.     p = alocare(n, m, k);
  67.     init(n, m, k, p);
  68.  
  69.     _getch();
  70. }
Add Comment
Please, Sign In to add comment