Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. /*
  2. Struktura typu S zawiera x,y typu int. Funkcja F zwraca sumę elementów x z tablicy
  3. struktur S, funkcja G zwraca sumę elementów y z tablicy struktur S, funkcja W
  4. wywołuje funkcję będącą jej parametrem, wyświetla wynik funkcji. W main()
  5. wczytać rozmiar tablicy struktur S, zadeklarować dynam. tablicę, wczytać tablicę,
  6. wywołać W: dla F, oraz dla G.
  7. */
  8.  
  9. /*
  10. * File: main.c
  11. * Author: admin-x55lj
  12. *
  13. * Created on 17 stycznia 2018, 00:59
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18.  
  19. /*
  20. *
  21. */
  22. struct S
  23. {
  24. int x;
  25. int y;
  26. };
  27.  
  28. int F(int a,struct S wczytaj[]);
  29. int G(int a,struct S wczytaj[]);
  30. void W(int a,struct S wczytaj[], int *P, int *Q);
  31.  
  32. int main(int argc, char** argv) {
  33. int n;
  34. scanf("%d",&n);
  35.  
  36.  
  37. struct S *wczytaj;
  38. wczytaj=malloc(n*sizeof(struct S));
  39. int i;
  40. for(i=0;i<n;i++)
  41. {
  42. printf("wczytaj[%d]: x=",i);
  43. scanf("%d",&(wczytaj[i].x));
  44. printf("wczytaj[%d]: y=",i);
  45. scanf("%d",&(wczytaj[i].y));
  46. }
  47. int *SX=(int*)malloc(n*sizeof(int));
  48. int *SY=(int*)malloc(n*sizeof(int));
  49. W(n,wczytaj,&SX,&SY);
  50. printf("suma x = %d",SX);
  51. printf("\nsuma y = %d",SY);
  52. return 0;
  53. }
  54. int F(int a,struct S wczytaj[])
  55. {
  56. int i;
  57. int S=0;
  58. for(i=0;i<a;i++)
  59. {
  60. S+=wczytaj[i].x;
  61. }
  62. return S;
  63. }
  64.  
  65. int G(int a,struct S wczytaj[])
  66. {
  67. int i;
  68. int S=0;
  69. for(i=0;i<a;i++)
  70. {
  71. S+=wczytaj[i].y;
  72. }
  73. return S;
  74. }
  75. void W(int a,struct S wczytaj[], int *P, int *Q)
  76. {
  77. int sumax,sumay;
  78. sumax=F(a,wczytaj);
  79. sumay=G(a,wczytaj);
  80. *P=sumax;
  81. *Q=sumay;
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement