Advertisement
Guest User

Q8v2.c

a guest
Nov 20th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int * getEvenNumbers (int * array, int size_array) {
  6. int count1, *array2, count2, n_even;
  7. n_even = getSize(array, size_array);
  8. array2 = (int *) malloc(sizeof(int) * n_even);
  9. count2 = 0;
  10. for(count1 = 0; count1 < size_array; count1++) {
  11. if(array[count1]%2 == 0) {
  12. array2[count2] = array[count1];
  13. count2++;
  14. }
  15. }
  16.  
  17. return array2;
  18. }
  19.  
  20. int getSize (int * array, int size_array) {
  21. int count1, n_even = 0;
  22. for(count1 = 0; count1 < size_array; count1++) {
  23. if(array[count1]%2 == 0) {
  24. n_even++;
  25. }
  26. }
  27.  
  28. return n_even;
  29. }
  30.  
  31. int main() {
  32. int terms, *point, *arr, count1, size_even_array;
  33.  
  34. printf("\nHow many random terms would you like? \nPlease enter a positive integer: \n");
  35. scanf("%d", &terms);
  36.  
  37. arr = (int*) malloc (sizeof(int) * terms);
  38. srand ((unsigned int) time (0));
  39.  
  40. printf("\nThe numbers of the set are: \n");
  41. for(count1 = 0; count1 < terms; count1++) {
  42. arr[count1] = rand();
  43. printf("%d \t", arr[count1]);
  44. }
  45.  
  46. printf("\n\nThe even numbers of the set are: \n");
  47. point = getEvenNumbers(arr, terms);
  48. size_even_array = getSize(arr, terms);
  49. for(count1 = 0; count1 < size_even_array; count1++) {
  50. printf("%d \t", point[count1]);
  51. }
  52.  
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement