Advertisement
dmilicev

task1_v1.c

Jun 3rd, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. /*
  2.  
  3.     task1_v1.c
  4.  
  5.     Version with double pointer (which is unnecessary).
  6.  
  7.     Task from Germin Chan
  8.     https://web.facebook.com/germin.chan
  9.  
  10.     https://web.facebook.com/groups/543431102664524/?post_id=1174495746224720&comment_id=1174746769532951&reply_comment_id=1175294896144805&notif_id=1591153540531865&notif_t=group_comment&ref=notif
  11.  
  12.  
  13.     You can find all my C programs at Dragan Milicev's pastebin:
  14.  
  15.     https://pastebin.com/u/dmilicev
  16.  
  17. */
  18.  
  19. #include<stdio.h>
  20. #include<stdlib.h>
  21.  
  22. int values_under_100(int *values, int size, int **under_100_array){
  23.  
  24.     int i, j=0, sum=0;
  25.  
  26.     for(i=0;i<size;i++){                //for loop to get values under 100
  27.         if ( *(values+i) < 100 ) {
  28.             *(*under_100_array + j) = *(values + i);
  29.             j++;
  30.             sum++;
  31.         }
  32.     }
  33.     return sum;
  34. }
  35.  
  36. int main(void){
  37.     int i, size;
  38.     int *values;
  39.  
  40.     printf("\n How many inputs do you want to enter? ");
  41.     scanf("%d", &size);
  42.  
  43.     values = malloc(size*sizeof(int));                  //dynamic allocation
  44.  
  45.     for(i=0;i<size;i++){
  46.         printf("\n Enter value: ");
  47.         scanf("%d", values + i );
  48.     }
  49.  
  50.     int *under_100_array = malloc(size*sizeof(int));    //dynamic allocation
  51.     int count = values_under_100(values, size, &under_100_array);   //call the function
  52.  
  53.     printf("\n Values under 100 are: ");
  54.     for(i=0;i<count;i++){                               //for loop to print array
  55.         printf("%d ", *(under_100_array + i) );
  56.     }
  57.  
  58.     printf("\n\n There are %d values under 100.\n", count);
  59.  
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement