Advertisement
Guest User

wwwssss

a guest
Jun 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. //Zadanie 1
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5.  
  6.  
  7. struct particle
  8. {
  9. int mass;
  10. int speed;
  11. int momentum
  12. };
  13.  
  14. typedef struct particle part;
  15.  
  16. void printParticle(part* p)
  17. {
  18. printf("mass: %i\n", p->mass);
  19. printf("speed: %i\n", p->speed);
  20. }
  21.  
  22.  
  23. void readParticle(part* p)
  24. {
  25. printf("Please enter the data for a new particle.\n");
  26. do {
  27. printf("mass: ");
  28. scanf("%i",&p->mass);
  29. }while(p->mass <= 0);
  30. printf("speed: ");
  31. scanf("%i",&p->speed);
  32. }
  33. void momentum(part* p){
  34. p->momentum = p->mass*p->speed;
  35. }
  36.  
  37. void printMomentum(part* p){
  38. printf("momentum: %i\n", p->momentum);
  39. }
  40.  
  41.  
  42. int main()
  43. {
  44. int i;
  45. int dim = 0;
  46. do {
  47. printf("How many particles do you want to have? ");
  48. scanf("%i",&dim);
  49. } while(dim <= 0);
  50.  
  51. part* p;
  52. p = (part*) malloc(dim*sizeof(part));
  53.  
  54. if (p==NULL) {
  55. printf("Problem with memory!\n");
  56. return EXIT_FAILURE;
  57. }
  58.  
  59. for (i=0; i<dim; i++)
  60. {
  61. readParticle(&p[i]);
  62. }
  63.  
  64. printf("\n\n");
  65. /*printf("The following particles\n");
  66.  
  67. for (i=0; i<dim; i++)
  68. {
  69. printParticle(&p[i]);
  70. }
  71. */
  72. for (i=0; i<dim; i++)
  73. {
  74. momentum(&p[i]);
  75. }
  76. int n = p[0].momentum;
  77.  
  78. for( i = 0; i < dim; i++){
  79.  
  80. if ( p[i].momentum < n){
  81.  
  82. n = p[i].momentum;
  83.  
  84. }
  85.  
  86. }
  87. printf("\nSmallest momentum is %i",n);
  88.  
  89.  
  90.  
  91. free(p);
  92.  
  93. return EXIT_SUCCESS;
  94. }
  95. //Zadanie 2
  96. #include <stdio.h>
  97. #include <stdlib.h>
  98. #include <time.h>
  99.  
  100.  
  101. int main()
  102. {
  103. srand (time(NULL));
  104. int dim,i;
  105. do{
  106. printf("Please, type the dimension of the vector: ");
  107. scanf("%i",&dim);
  108. }while(dim < 0);
  109. int* vec = (int*) malloc(dim*sizeof(int));
  110.  
  111. if (vec == NULL)
  112. {
  113. printf("ERROR: not enough memory!\n");
  114. return EXIT_FAILURE;
  115. }
  116. for( i = 0; i < dim; i++){
  117. vec[i] = rand() % 10 + 1;
  118. }
  119.  
  120. for ( i = 0; i < dim; i++){
  121. printf("vec[%i] = %i\n",i,vec[i]);
  122. }
  123. int count = 0;
  124. for (i = 0; i< dim; i++){
  125.  
  126. if(vec[i] > 8){
  127. count++;
  128. }
  129. }
  130. printf("%i vectors are larger than 8",count);
  131. free(vec);
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement