C Programs – (Arrays)

1. Assign a 1-D array with random values

#include <stdio.h>
#include <stdlib.h>

int main()
{
        int arr[20];
        int i;
        int size;

        printf("assigning array values using rand()\n");
        size = 20;
        for (i = 0; i < size; i++) {
                /* random values from 0 to 100 */
                arr[i] = rand() % 100;
        }

        printf("The elements in array are:\n");
        for (i = 0; i < size; i++)
                printf("%d ", arr[i]);
        printf("\n");
}

output

assigning array values using rand()
The elements in array are:
83 86 77 15 93 35 86 92 49 21 62 27 90 59 63 26 40 26 72 36 

2. Remove prime numbers in an array

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>

bool is_prime(int val)
{
        int i;

        if (val < 2)
                return false;

        for(i = 2; i <= val/2; i++) {
                if (val % i == 0)
                        return false;
        }
        return true;
}

int remove_prime_numbers(int *ptr, int size)
{
        int i;
        int cnt = 0;

        for (i = 0; i < size; i++) {
                if (is_prime(ptr[i])) {
                        cnt++;
                        memcpy(ptr + i, ptr + i + 1, (size - i - 1)*sizeof(int));
                        i--;
                        size--;
                }
        }
        return cnt;
}

int main()
{
        int arr[20];
        int i;
        int size;
        int count;

        printf("assigning array values using rand()\n");
        size = 20;
        srand(time(NULL));
        for (i = 0; i < size; i++) {
                /* random values from 0 to 100 */
                arr[i] = rand() % 100;
        }

        printf("The elements in array are:\n");
        for (i = 0; i < size; i++)
                printf("%d ", arr[i]);
        printf("\n");

        count = remove_prime_numbers(arr, size);

        printf("Array after removing prime numbers\n");
        for (i = 0; i < (size - count); i++ )
                printf("%d ", arr[i]);
        printf("\n");
}

ouput

assigning array values using rand()
The elements in array are:
3 42 5 2 71 67 71 22 81 0 5 75 75 70 68 72 29 35 66 81 
Array after removing prime numbers
42 22 81 0 75 75 70 68 72 35 66 81 

3. Find the largest and smallest element in an Array

input array: 41 41 88 2 72 74 80 81 40 53 50 90 39 32 71 16 40 64 44 2
the largest number is 90
the smallest number is 2

/* find second largest and second smallest number */
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main()
{
        int i;
        int arr[20];
        int size;
        int max = 0, min;

        printf("assign 20 array elements\n");
        size = 20;
        srand(time(NULL));
        for (i = 0; i < size; i++) {
                /* random values from 0 to 100 */
                arr[i] = rand() % 100;
                printf("%d ", arr[i]);
        }
        printf("\n");

        min = arr[0];
        for (i = 0; i < size; i++) {
                if (arr[i] > max)
                        max = arr[i];
                if (arr[i] < min)
                        min = arr[i];
        }
        printf("the largest number is %d\n", max);
        printf("the smallest number is %d\n", min);
}

output

99 84 15 35 57 31 34 37 12 43 97 10 31 5 87 36 13 85 61 32 
the largest number is 99
the smallest number is 5

4. Find the Second largest and second smallest element in an Array

sample input: 63 21 44 93 57 15 30 18 5 9 44 80 1 58 38 60 68 15 49 17
output:
the second largest number is 80
The second smallest element is 5

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
        int i;
        int arr[20];
        int l1 = 0, l2 = 0; // first largest and second largest
        int s1 = 0, s2 = 0; // first smallest and second smallest
        int sflag = 1;
        int size;

        printf("assigning array values using rand()\n");
        size = 20; 
        srand(time(NULL));
        for (i = 0; i < size; i++) {
                /* random values from 0 to 100 */
                arr[i] = rand() % 100;
                printf("%d ", arr[i]);
        }
        printf("\n");

        s1 = arr[0];
        for(i = 0; i < size; i++) {
                if (arr[i] > l2 && arr[i] > l1) {
                        l2 = l1;
                        l1 = arr[i];
                } else if (arr[i] > l2 && arr[i] < l1) {
                        l2 = arr[i];
                }
                if (sflag) {
                        if (arr[i] > s1) {
                                s2 = arr[i];
                                sflag = 0;

                        } else if (arr[i] < s1) {
                                s2 = s1;
                                s1 = arr[i];
                                sflag = 0;
                        }
                } else {
                        if (arr[i] < s2 && arr[i] < s1) {
                                s2 = s1;
                                s1 = arr[i];
                        } else if (arr[i] < s2 && arr[i] > s1) {
                                s2 = arr[i];
                        }
                }
        }
        printf("the second largest number is %d\n", l2);
        printf("The second smallest element is %d\n", s2);
}     

output

assigning array values using rand()
80 8 55 66 59 71 89 56 86 3 56 99 80 90 18 0 55 70 95 11 
the second largest number is 95
The second smallest element is 3

5. Reverse the elements in an Array

81 50 79 51 96 82 25 82 93 63 81 72 23 30 89 13 22 77 44 48
Array after reversing
48 44 77 22 13 89 30 23 72 81 63 93 82 25 82 96 51 79 50 81

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
        int i;
        int arr[20];
        int size;
        int tmp;

        printf("assigning array values using rand()\n");
        size = 20;
        srand(time(NULL));
        for (i = 0; i < size; i++) {
                /* random values from 0 to 100 */
                arr[i] = rand() % 100;
                printf("%d ", arr[i]);
        }
        printf("\n");

        for (i = 0; i < size/2; i++) {
                tmp = arr[i];
                arr[i] = arr[size - 1 - i];
                arr[size - 1 - i] = tmp;
        }
        printf("Array after reversing\n");
        for (i = 0; i < size; i++)
                printf("%d ", arr[i]);
        printf("\n");
}

output

assigning array values using rand()
37 19 13 56 14 47 9 19 3 29 28 20 52 60 44 47 42 66 82 73
Array after reversing
73 82 66 42 47 44 60 52 20 28 29 3 19 9 47 14 56 13 19 37

6. Find the duplicate elements in an array and remove them.

input array
2 3 3 4 5 5 67 55 55 7 8 4 5 6 7 88 99 00 09 67
Array after removing duplicates
2 3 4 5 67 55 7 8 6 88 99 0 9

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
int main()
{
        int i, j;
        int arr[20];
        int size;
        int tmp;

        printf("assign 20 array values\n");
        size = 20;
        for (i = 0; i < size; i++) {
                scanf("%d", &arr[i]);
        }
        printf("\n");

        for (i = 0; i < size - 1; i++) {
                for (j = i + 1; j < size; j++) {
                        if (arr[i] == arr[j]) {
                                memcpy(&arr[j], &arr[j + 1],
                                       sizeof(int) * (size - 1 - j));
                                j--;
                                size--;
                        }
                }
        }

        printf("Array after removing duplicates\n");
        for (i = 0; i < size; i++)
                printf("%d ", arr[i]);
        printf("\n");
}

input

assign 20 array elements
4 5 6 44 5 6 3 4 5 6 66 7 88 9 67 45 33 44 78 87

output

Array after removing duplicates
4 5 6 44 3 66 7 88 9 67 45 33 78 87