fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int findElementsInRange(int *arr, int size, int a, int b)
  5. {
  6. int result = 0;
  7. for(register int y =0;y<size;y++)
  8. {
  9. if(arr[y] >= a && arr[y] <= b)
  10. {
  11. result++;
  12. }
  13. }
  14.  
  15. return result;
  16. }
  17. void populateArray(int *arr, int size)
  18. {
  19. for(register int x = 0;x<size;x++)
  20. {
  21. arr[x] = rand() % 10;
  22. }
  23. }
  24. void printArray(int *arr, int size)
  25. {
  26. for(register int x = 0;x<size;x++)
  27. {
  28. printf("%d ",arr[x]);
  29. }
  30. puts("");
  31. }
  32. int main(void)
  33. {
  34. int size = 0;
  35. char tempBuff[257];
  36. memset(tempBuff,0,sizeof(tempBuff));
  37. printf("Enter the number of elements in the array: ");
  38. fgets(tempBuff,257,stdin);
  39. sscanf(tempBuff,"%d",&size);
  40. int *arr = calloc(size,sizeof(int));
  41. populateArray(arr,size);
  42. printArray(arr,size);
  43. int a = 1;
  44. int b = 5;
  45. int res = findElementsInRange(arr,size,a,b);
  46. if(res)
  47. {
  48. printf("Total number of elements in the array in the specified range is %d\n",res);
  49. }
  50. else
  51. {
  52. printf("No elements in the specified range found\n");
  53. }
  54. //code here
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 2248KB
stdin
10
stdout
Enter the number of elements in the array: 3 6 7 5 3 5 6 2 9 1 
Total number of elements in the array in the specified range is 6