Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- array_of_integers.c
- Task from:
- https://www.facebook.com/photo/?fbid=279404964292467&set=gm.1550830281966942
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <math.h>
- // Function to print array, n is number of elements in array arr[]
- // text[] describes the shown array arr[]
- void ShowArray(char text[],int arr[],int n)
- {
- int i;
- printf("\n n = %d ", n);
- printf("%s",text);
- for(i=0;i<n;i++)
- printf("%5d", arr[i]);
- printf("\n");
- }
- // Function to swap two integers, with pointers
- void swap(int *xp, int *yp)
- {
- int temp = *xp;
- *xp = *yp;
- *yp = temp;
- }
- // function bubbleSort to sort the array in descending order
- void bubbleSortDescending(int arr[], int n)
- {
- int i, j;
- for (i = 0; i < n-1; i++)
- for (j = 0; j < n-i-1; j++) // Last i elements are already in place
- if (arr[j] < arr[j+1]) // arr[j] > arr[j+1] for ascending order
- swap(&arr[j], &arr[j+1]);
- }
- void removeEvenIntegers(int arr[], int *n)
- {
- int i,k=0;
- for(i=0;i<*n;i++)
- if(arr[i]%2==0)
- ; // do nothing
- else
- arr[k++]=arr[i];
- *n=k;
- }
- // reverse array of integers
- void reverse_array( int arr[], int n )
- {
- int i, j, mem_int; // n is number of integers in arr[]
- // reverse array
- for( i=0,j=n-1; i<n/2; i++,j-- )
- {
- mem_int = arr[i];
- arr[i] = arr[j];
- arr[j] = mem_int;
- }
- }
- // Returns 1 if num is prime, otherwise returns 0.
- int is_prime(int num){
- int i;
- float sq = sqrt(num);
- if( num < 2 )
- return 0;
- for(i=2; i<=sq; i++) // it is enough to go to the root of the number !
- if(num % i == 0) // if the number is divisible by i,
- return 0; // it is not prime number
- return 1;
- }
- int removeNotPrimeIntegers(int arr[], int *n)
- {
- int i,k=0;
- for(i=0;i<*n;i++)
- if( is_prime(arr[i]) )
- arr[k++]=arr[i];
- else
- ; // do nothing
- *n=k;
- }
- // Swap the last number of the arr with the first number of arr.
- void swap_last_and_first_number_of_array(int arr[], int n)
- {
- int temp;
- temp=arr[0];
- arr[0]=arr[n-1];
- arr[n-1]=temp;
- }
- int main(void){
- 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};
- int i, n=sizeof(arr)/sizeof(int);
- // n=30;
- // printf("\n Enter %d integers for array \n", n);
- // for(i=0;i<n;i++)
- // scanf("%d", &arr[i]);
- // n=i;
- ShowArray("\n Array is :\n", arr, n);
- bubbleSortDescending(arr,n);
- ShowArray("\n Descending sorted array is :\n", arr, n);
- removeEvenIntegers(arr,&n);
- ShowArray("\n Array without even integers is : \n", arr, n);
- reverse_array(arr,n);
- ShowArray("\n Reversed array is : \n", arr, n);
- removeNotPrimeIntegers(arr,&n);
- ShowArray("\n Array without not prime integers is : \n", arr, n);
- swap_last_and_first_number_of_array(arr,n);
- ShowArray("\n Array after swapping first and last number is : \n", arr, n);
- printf("\n\n");
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment