dmilicev

array_of_integers.c

Feb 4th, 2022 (edited)
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 KB | None | 0 0
  1. /*
  2.  
  3.     array_of_integers.c
  4.  
  5.     Task from:
  6.     https://www.facebook.com/photo/?fbid=279404964292467&set=gm.1550830281966942
  7.  
  8.  
  9.     You can find all my C programs at Dragan Milicev's pastebin:
  10.  
  11.     https://pastebin.com/u/dmilicev
  12.  
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <math.h>
  17.  
  18. // Function to print array, n is number of elements in array arr[]
  19. // text[] describes the shown array arr[]
  20. void ShowArray(char text[],int arr[],int n)
  21. {
  22.     int i;
  23.  
  24.     printf("\n n = %d ", n);
  25.     printf("%s",text);
  26.     for(i=0;i<n;i++)
  27.         printf("%5d", arr[i]);
  28.  
  29.     printf("\n");
  30. }
  31.  
  32. // Function to swap two integers, with pointers
  33. void swap(int *xp, int *yp)
  34. {
  35.     int temp = *xp;
  36.     *xp = *yp;
  37.     *yp = temp;
  38. }
  39.  
  40. // function bubbleSort to sort the array in descending order
  41. void bubbleSortDescending(int arr[], int n)
  42. {
  43.     int i, j;
  44.  
  45.     for (i = 0; i < n-1; i++)
  46.         for (j = 0; j < n-i-1; j++)     // Last i elements are already in place
  47.             if (arr[j] < arr[j+1])      // arr[j] > arr[j+1] for ascending order
  48.                 swap(&arr[j], &arr[j+1]);
  49. }
  50.  
  51. void removeEvenIntegers(int arr[], int *n)
  52. {
  53.     int i,k=0;
  54.  
  55.     for(i=0;i<*n;i++)
  56.         if(arr[i]%2==0)
  57.             ;                   // do nothing
  58.         else
  59.             arr[k++]=arr[i];
  60.  
  61.     *n=k;
  62. }
  63.  
  64. // reverse array of integers
  65. void reverse_array( int arr[], int n )
  66. {
  67.     int i, j, mem_int;          // n is number of integers in arr[]
  68.  
  69.     // reverse array
  70.     for( i=0,j=n-1; i<n/2; i++,j-- )
  71.     {
  72.         mem_int    = arr[i];
  73.         arr[i]   = arr[j];
  74.         arr[j] = mem_int;
  75.     }
  76. }
  77.  
  78. // Returns 1 if num is prime, otherwise returns 0.
  79. int is_prime(int num){
  80.     int i;
  81.     float sq = sqrt(num);
  82.     if( num < 2 )
  83.         return 0;
  84.     for(i=2; i<=sq; i++)    // it is enough to go to the root of the number !
  85.         if(num % i == 0)    // if the number is divisible by i,
  86.             return 0;       // it is not prime number
  87.     return 1;
  88. }
  89.  
  90. int removeNotPrimeIntegers(int arr[], int *n)
  91. {
  92.     int i,k=0;
  93.  
  94.     for(i=0;i<*n;i++)
  95.         if( is_prime(arr[i]) )
  96.             arr[k++]=arr[i];
  97.         else
  98.             ;               // do nothing
  99.  
  100.     *n=k;
  101. }
  102.  
  103. // Swap the last number of the arr with the first number of arr.
  104. void swap_last_and_first_number_of_array(int arr[], int n)
  105. {
  106.     int temp;
  107.  
  108.     temp=arr[0];
  109.     arr[0]=arr[n-1];
  110.     arr[n-1]=temp;
  111. }
  112.  
  113.  
  114. int main(void){
  115.     int arr[]={5,34,7,56,32,1,0,12,8,76,78,23,2,56,28,56,23,65,13,34,45,3,76,25,15,32,76,34,30,29};
  116.     int i, n=sizeof(arr)/sizeof(int);
  117.  
  118. //  n=30;
  119. //  printf("\n Enter %d integers for array \n", n);
  120. //  for(i=0;i<n;i++)
  121. //      scanf("%d", &arr[i]);
  122. //  n=i;
  123.     ShowArray("\n Array is :\n", arr, n);
  124.  
  125.     bubbleSortDescending(arr,n);
  126.     ShowArray("\n Descending sorted array is :\n", arr, n);
  127.  
  128.     removeEvenIntegers(arr,&n);
  129.     ShowArray("\n Array without even integers is : \n", arr, n);
  130.  
  131.     reverse_array(arr,n);
  132.     ShowArray("\n Reversed array is : \n", arr, n);
  133.  
  134.     removeNotPrimeIntegers(arr,&n);
  135.     ShowArray("\n Array without not prime integers is : \n", arr, n);
  136.  
  137.     swap_last_and_first_number_of_array(arr,n);
  138.     ShowArray("\n Array after swapping first and last number is : \n", arr, n);
  139.  
  140.     printf("\n\n");
  141.     return 0;
  142. } // main()
  143.  
Add Comment
Please, Sign In to add comment