Advertisement
visoft

Problema 4.6 Iteratia Add, cu bug.

Nov 9th, 2020
1,857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // 4.6
  4.  
  5. typedef struct {
  6.     int *tab;
  7.     int capacitate;
  8.     int index;
  9. }Vector;
  10.  
  11. Vector create(){
  12.     Vector t;
  13.     t.capacitate = 2;
  14.     t.index = 0;
  15.     t.tab = (int*)malloc(sizeof(int) * t.capacitate);
  16.     return t;
  17. }
  18.  
  19. void add(Vector t, int element){
  20.     //Caz 1, indexul este sub capacitate
  21.     if (t.index < t.capacitate){
  22.         t.tab[t.index] = element;
  23.         t.index++;
  24.     }
  25. }
  26.  
  27.  
  28. int main() {
  29.  
  30.     Vector  v = create();
  31.  
  32.     add(v, 10);
  33.     add(v, 20);
  34. //    add(v, 30);
  35. //    add(v, 40);
  36. //    for (int i = 0; i < v.index;i++){
  37. //        printf("tab[%2d] = %d\n", i, get(v, i));
  38. //    }
  39.  
  40.  
  41.     return 0;
  42. }
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement