Advertisement
Guest User

array

a guest
Jan 28th, 2015
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct elem{
  5.     int *vect;
  6.     int lenght;
  7. };
  8. typedef struct elem array;
  9. typedef array *Array;
  10.  
  11. Array newArray(int);
  12. void insert(Array, int);
  13. void stampa(Array);
  14.  
  15. int main()
  16. {
  17.     int i, n, x;
  18.     Array a = newArray();
  19.     printf("Quanti elementi?");
  20.     scanf("%d", &n);
  21.     for(i = 0; i < n; i++){
  22.         printf("Elemento n.%d: ", i+1);
  23.         scanf("%d", &x);
  24.     insert(a,x);
  25.     }
  26.     stampa(a);
  27.     return 0;
  28. }
  29.  
  30. Array newArray(){
  31.     Array new = (Array)malloc(sizeof(array));
  32.     new->vect = (int *)malloc(sizeof(int));
  33.     new->lenght = 0;
  34.     return new;
  35. }
  36.  
  37. void insert(Array a, int elem){
  38.     a->dim++;
  39.     a->vect = (int *)realloc(a->dim,sizeof(int));
  40.     a->vect[a->dim-1] = elem;
  41. }
  42.  
  43.  
  44. void stampa(Array a){
  45.     int i;
  46.     for(i = 0; i < a->lenght; i++)
  47.         printf("%d -> ", a->vect[i]);
  48.     printf("STOP.\n");
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement