Guest User

Untitled

a guest
Dec 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. // structure
  2. typedef struct coordinates{
  3. double **a;
  4. } coord;
  5.  
  6. // counts the nmbr of points
  7. int count(){
  8. int i;
  9. int a1, a2, a3;
  10. FILE *fp;
  11. fp = fopen("abc.txt", "r+");
  12. while (fscanf(fp, "%d %d %d", &a1, &a2, &a3) != EOF){
  13. i++;
  14. if (feof(fp)){
  15. break;
  16. }
  17. }
  18. fclose(fp);
  19. return(i);
  20. }
  21.  
  22. // creates new structure with the right size of memory allocated
  23. coord *newVector(size_t s){
  24. coord *v;
  25. int j;
  26. v = malloc(sizeof(coord));
  27.  
  28. v->a = malloc(sizeof(double*)*3);
  29.  
  30. for (j=0; j<3; j++){
  31. v->a[j] = malloc(sizeof(double)*s);
  32. }
  33.  
  34. }
  35.  
  36. void get_coords(coord *points){
  37. int i=0;
  38. FILE *fp;
  39. fp = fopen("abc.txt", "r+");
  40. while (fscanf(fp, "%le %le %le", &points->a[i][0], &points->a[i][1], &points->a[i][2]) != EOF){
  41. i++;
  42. }
  43. fclose(fp);
  44. }
  45.  
  46.  
  47. int main(){
  48. int i = 0, j=0;
  49.  
  50. coord *points;
  51.  
  52. i = count();
  53.  
  54. points = newVector(i);
  55.  
  56. get_coords(points);
  57.  
  58. for (i=0; i<3; i++){
  59. printf("%lf %lf %lfn", points->a[i][0], points->a[i][1], points->a[i][2]);
  60. }
  61. }
  62.  
  63. 1 2 3
  64. 4 5 6
  65. 7 8 9
Add Comment
Please, Sign In to add comment