Advertisement
dmilicev

task1_v2.c

Jun 3rd, 2020
161
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     task1_v2.c
  4.  
  5.     Task from Germin Chan
  6.     https://web.facebook.com/germin.chan
  7.  
  8.     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
  9.  
  10.  
  11.     You can find all my C programs at Dragan Milicev's pastebin:
  12.  
  13.     https://pastebin.com/u/dmilicev
  14.  
  15. */
  16.  
  17. #include<stdio.h>
  18. #include<stdlib.h>
  19.  
  20. int values_under_100(int *values, int size, int *under_100_array){
  21.     int i, j=0, sum=0;
  22.  
  23.     for(i=0;i<size;i++){                // for loop to get values under 100
  24.         if ( *(values+i) < 100 ) {
  25.             *(under_100_array + j) = *(values + i);
  26.             j++;
  27.             sum++;
  28.         }
  29.     }
  30.     return sum;
  31. }
  32.  
  33. int main(void){
  34.     int i, size;
  35.     int *values;
  36.  
  37.     printf("\n How many inputs do you want to enter? ");
  38.     scanf("%d", &size);
  39.  
  40.     values = malloc(size*sizeof(int));                  //dynamic allocation
  41.  
  42.     for(i=0;i<size;i++){
  43.         printf("\n Enter value: ");
  44.         scanf("%d", values + i );
  45.     }
  46.  
  47.     int *under_100_array = malloc(size*sizeof(int));    //dynamic allocation
  48.     int count = values_under_100(values, size, under_100_array);    //call the function
  49.  
  50.     printf("\n Values under 100 are: ");
  51.     for(i=0;i<count;i++){                               //for loop to print array
  52.         printf("%d ", *(under_100_array + i) );
  53.     }
  54.  
  55.     printf("\n\n There are %d values under 100.\n", count);
  56.  
  57.     return 0;
  58. }
Advertisement
RAW Paste Data Copied
Advertisement