Array Exercise 2

On this page, you will get some extra array exercises and solutions.


Exercise 12:

Write a program (WAP) that will take n integers into an array A. Now sort them in ascending order within that array. Finally show all elements of array A.

Sample Input Sample Output
8
7 8 1 3 2 6 4 3
1 2 3 3 4 6 7 8
3
3 2 1
1 2 3

Solution:


   #include<stdio.h>
   int main(){
    int n, i, j, temp;
    scanf("%d", &n);
    int arr[n];
    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    
    // Sorting the array in ascending order using bubble sort
    for(i = 0; i < n - 1; i++) {
        for(j = 0; j < n - i - 1; j++) {
            if(arr[j] > arr[j + 1]) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    for(i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
 
    return 0;
   }


Output:


    3
    3 2 1
    1 2 3    



Exercise 13:

Write a program (WAP) that will take n integers into an array A. Now remove all duplicates numbers from that array. Finally print all elements from that array.

Sample Input Sample Output
8
2 8 1 3 2 6 4 3
2 8 1 3 6 4
3
3 3 3
3
4
6 7 8 9
6 7 8 9

Solution:


   #include<stdio.h>
   int main(){
    int n, i, j, k;
    scanf("%d", &n);
    int arr[n];
    
    for (i = 0; i < n; i++){
        scanf("%d", &arr[i]);
    }
    for (i = 0; i < n; i++){
        for (j = i+1; j < n; j++){
            if (arr[i] == arr[j]){
                for (k = j; k < n-1; k++){
                  arr[k] = arr[k+1];  
                }
                n--;
                j--;
            }
        }
    }

    for (i = 0; i < n; i++){
        printf("%d ", arr[i]);
    }
     return 0;
   }


Output:


    3
    3 3 3
    3 



Exercise 14:

Write a program (WAP) that will take n integers into an array A and m positive integers into array B. Now find the intersection (set operation) of array A and B.

Sample Input Sample Output
8
7 8 1 5 2 6 4 3
6
1 3 6 0 9 2
1 2 6 3
3
1 2 3
2
4 5
Empty set

Solution:


    #include<stdio.h>
    int main() {
    int sizeA, sizeB, i, j, k;
    int A[100], B[100], intersect[100];
    scanf("%d", &sizeA);
    for (i = 0; i < sizeA; i++) {
        scanf("%d", &A[i]);
    }

    // remove duplicates from array A
    for (i = 0; i < sizeA; i++) {
        for (j = i+1; j < sizeA;) {
            if (A[j] == A[i]) {
                for (k = j; k < sizeA; k++) {
                    A[k] = A[k+1];
                }
                sizeA--;
            } else {
                j++;
            }
        }
    }
    scanf("%d", &sizeB);
    for (i = 0; i < sizeB; i++) {
        scanf("%d", &B[i]);
    }

    // remove duplicates from array B
    for (i = 0; i < sizeB; i++) {
        for (j = i+1; j < sizeB;) {
            if (B[j] == B[i]) {
                for (k = j; k < sizeB; k++) {
                    B[k] = B[k+1];
                }
                sizeB--;
            } else {
                j++;
            }
        }
    }

    // find intersection of arrays A and B
    int count = 0;
    for (i = 0; i < sizeA; i++) {
        for (j = 0; j < sizeB; j++) {
            if (A[i] == B[j]) {
                intersect[count] = A[i];
                count++;
                break;
            }
        }
    }

    if (count == 0) {
        printf("Empty set\n");
    } else {
        for (i = 0; i < count; i++) {
            printf("%d ", intersect[i]);
        }
        printf("\n");
    }
    return 0;
    }


Output:


    8
    7 8 1 5 2 6 4 3
    6
    1 3 6 0 9 2
    1 2 6 3



Exercise 15:

Write a program (WAP) that will take n integers into an array A and m positive integers into array B. Now find the union (set operation) of array A and B.

Sample Input Sample Output
8
7 8 1 5 2 6 4 3
6
1 3 6 0 9 2
7 8 1 5 2 6 4 3 0 9
3
1 2 3
2
4 5
1 2 3 4 5

Solution:


    #include<stdio.h>
    int main() {
      int n, m, i, j;
      scanf("%d", &n);
      int A[n];
      for(i=0; i<n; i++) {
          scanf("%d", &A[i]);
      }
      scanf("%d", &m);
      int B[m];
      for(i=0; i<m; i++) {
          scanf("%d", &B[i]);
      }
  
      // Remove duplicates from array A
      for(i=0; i<n; i++) {
          for(j=i+1; j<n;) {
              if(A[j] == A[i]) {
                  n--;
                  A[j] = A[n];
              }
              else {
                  j++;
              }
          }
      }
  
      // Remove duplicates from array B
      for(i=0; i<m; i++) {
          for(j=i+1; j<m;) {
              if(B[j] == B[i]) {
                  m--;
                  B[j] = B[m];
              }
              else {
                  j++;
              }
          }
      }
  
      // Find union of arrays A and B
      int C[n+m];
      int k = 0;
      for(i=0; i<n; i++) {
          C[k] = A[i];
          k++;
      }
      for(i=0; i<m; i++) {
          int isDuplicate = 0;
          for(j=0; j<n; j++) {
              if(B[i] == A[j]) {
                  isDuplicate = 1;
                  break;
              }
          }
          if(!isDuplicate) {
              C[k] = B[i];
              k++;
          }
      }
  
      
      for(i=0; i<k; i++) {
          printf("%d ", C[i]);
      }
    return 0;
    }


Output:


  3
  1 2 3
  2
  4 5
  1 2 3 4 5  



Exercise 16:

Write a program (WAP) that will take n integers into an array A and m positive integers into array B. Now find the difference (set operation) of array A and B or (A-B).

Sample Input Sample Output
8
7 8 1 5 2 6 4 3
6
7 8 5 4
5 4 3 2 1
3
1 2 3
2
4 5
1 2 3

Solution:


    #include<stdio.h>
    int main(){
    int n, m, i, j, k, flag;
    int A[100], B[100], C[100];
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        scanf("%d", &A[i]);
    }
    scanf("%d", &m);

    for (i = 0; i < m; i++) {
        scanf("%d", &B[i]);
    }

    k = 0;
    for (i = 0; i < n; i++) {
        flag = 0;
        for (j = 0; j < m; j++) {
            if (A[i] == B[j]) {
                flag = 1;
                break;
            }
        }
        if (!flag) {
            C[k] = A[i];
            k++;
        }
    }

    for (i = 0; i < k; i++) {
        printf("%d ", C[i]);
    }
     return 0;
    }


Output:


  3
  1 2 3
  2
  4 5
  1 2 3