Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5. float aryt(int *table, int ile)
  6. {
  7.  
  8. float s1=0;
  9. s1 = (float)(table[0]+table[ile-1])/2;
  10. s1 = s1*ile;
  11.  
  12.  
  13. return s1;
  14. }
  15.  
  16. float geom(int *table, int ile)
  17. {
  18. int q=0, q2;
  19. float s2=0;
  20. q = table[1]/table[0];
  21. q2 = pow(q, ile);
  22. if(q==1){
  23. s2 = table[0] * ile;
  24. }
  25. else{
  26. s2 = table[0]*((1-q2)/(1-q));
  27.  
  28. }
  29. return s2;
  30.  
  31. }
  32.  
  33. int main()
  34. {
  35. int size=0, i;
  36. printf("Podaj rozmiar tablicy:\n");
  37. scanf("%d", &size);
  38. int *table = malloc(size); //dynamiczne rezerwowanie miejsca w pamieci, zwraca wskaznik do nowowo zarezerwowanego miejsca
  39. srand(time(0)); //losowanie liczb pseudo losowych do tablicy
  40. for(i=0; i<size; i++){
  41. table[i] = 1 + rand() % 10;
  42. printf("%d ", table[i]);
  43. }
  44. // free(table); //zwalnanie pamieci
  45.  
  46. printf("\n\nSuma ciagu arytmetycznego: %f", aryt(table, size));
  47. printf("\n\nSuma ciagu geometrycznego: %f", geom(table, size));
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement