Array Exercise 1

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


Exercise 1:

Write a program (WAP) that will take n integer numbers into an array, and then print all the integers into reverse order (from the last valid index to index 0).

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

Solution:


   #include<stdio.h>
   int main(){
      int n, i;
      scanf("%d", &n);       
      int arr[n];
      for(i=0; i<n; i++) {
        scanf("%d", &arr[i]);
      }
    
      printf("Reversed array: ");
      for(i=n-1; i>=0; i--) {
        printf("%d ", arr[i]);
      }
    return 0;
   }


Output:


    5
    1 2 3 4 5
    5 4 3 2 1    



Exercise 2:

Write a program (WAP) that WAP that will take n integer numbers into an array, and then sum up all the integers in that array.

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

Solution:


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

     printf("%d\n", sum);
     return 0;
   }


Output:


    5
    1 2 3 4 5
    15



Exercise 3:

Write a program (WAP) that will take n integer numbers into an array, and then sum up all the even integers in that array.

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

Solution:


    #include<stdio.h>
    int main(){
        int n, i, sum_even=0;
        scanf("%d", &n);
        int arr[n];
        for(i=0; i<n; i++) {
            scanf("%d", &arr[i]);
            if(arr[i] % 2 == 0) {
                sum_even += arr[i];
            }
        }
        
        printf("%d\n", sum_even);
        return 0;
       }


Output:


    5
    1 2 3 4 5
    6



Exercise 4:

Write a program (WAP) that will take n integer numbers into an array, and then sum up all the even indexed integers in that array.

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

Solution:


    int main() {
        int n, i, sum = 0;
        scanf("%d", &n);
        int arr[n];
        for (i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
            if (i % 2 == 0) { // check if index is even
                sum += arr[i];
            }
        }
        printf("%d\n", sum);
        return 0;
    }


Output:


    5
    1 2 3 4 5
    9



Exercise 5:

Write a program (WAP) that will take n integer numbers into an array, and then reverse all the integers within that array. Finally print them all from 0 index to last valid index.

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

Solution:


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


Output:


    5
    1 2 3 42 2
    2 42 3 2 1  



Exercise 6:

Write a program (WAP) that will take n integer numbers into an array, and then find the maximum -minimum among them with its index position.

Sample Input Sample Output
5
1 2 3 4 5
Max: 5, Index: 4
Min: 1, Index: 0
6
2 8 3 9 0 1
Max: 9, Index: 3
Min: 0, Index: 4

Solution:


    #include<stdio.h>
    int main(){
    int n, arr[100], max, min, max_index, min_index, i;
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    max = arr[0];
    min = arr[0];
    max_index = 0;
    min_index = 0;
    for (i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
            max_index = i;
        }
        if (arr[i] < min) {
            min = arr[i];
            min_index = i;
        }
    }
    printf("Max: %d, Index: %d\n", max, max_index);
    printf("Min: %d, Index: %d\n", min, min_index);
    return 0;
    }


Output:


    5
    1 2 3 4 5
    Max: 5, Index: 4
    Min: 1, Index: 0



Exercise 7:

Write a program (WAP) that will take n alphabets into an array, and then count number of vowels in that array.

Sample Input Sample Output
7
AKIOUEH
Count: 5
29
UNITEDINTERNATIONALUNIVERSITY
Count: 13

Solution:


    #include<stdio.h>
    int main(){
    int n, i, count = 0;
    scanf("%d", &n);
    char arr[100];
    for(i = 0; i < n; i++) {
        scanf(" %c", &arr[i]);// space is for separate the each char
    }

    for(i = 0; i < n; i++) {
        if(arr[i] == 'a' || arr[i] == 'e' || arr[i] == 'i' || arr[i] == 'o' || arr[i] == 'u' ||
            arr[i] == 'A' || arr[i] == 'E' || arr[i] == 'I' || arr[i] == 'O' || arr[i] == 'U') {
            count++;
        }
    }

    printf("Count: %d", count);

    return 0;
    }


Output:


    29
    UNITEDINTERNATIONALUNIVERSITY
    Count: 13



Exercise 8:

Write a program (WAP) that will take n integers into an array, and then search a number into that array. If found then print its index. If not found then print “NOT FOUND”.

Sample Input Sample Output
8
7 8 1 3 2 6 4 3
3
FOUND at index position: 3, 7
8
7 8 1 3 2 6 4 3
5
NOT FOUND

Solution:


    #include<stdio.h>
    int main(){
    int n, i, num, found = 0,k=0;
    scanf("%d", &n);
    int arr[n],result[n];
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    scanf("%d", &num);
    for (i = 0; i < n; i++) {
        if (arr[i] == num) {
            result[k++]= i;
            found = 1;
        }
    }
    
    if (found == 0) {
        printf("NOT FOUND\n");
    }
    else{
        printf("FOUND at index position: ");
        for(int i = 0; i < k; i++ ){
            if(i==k-1){ //for printing commas
                printf("%d",result[i]);
            }
            else{
                printf("%d, ",result[i]);  
            }
        }
    }
    
    return 0;
    }


Output:


    8
    7 8 1 3 2 6 4 3
    3
    FOUND at index position: 3, 7



Exercise 9:

Write a program (WAP) that will take n integers into an array A, and then copy all numbers in reverse order from array A to another array B. Finally show all elements of both array A and B.

Sample Input Sample Output
8
7 8 1 3 2 6 4 3
3
Array A : 7 8 1 3 2 6 4 3
Array B : 3 4 6 2 3 1 8 7
3
3 2 1
Array A : 3 2 1
Array B : 1 2 3

Solution:


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


    printf("\nArray A: ");
    for (i = 0; i < n; i++) {
        printf("%d ", A[i]);
    }
    printf("\nArray B: ");
    for (i = 0; i < n; i++) {
        printf("%d ", B[i]);
    }

    return 0;
    }


Output:


    3
    3 2 1
    Array A: 3 2 1 
    Array B: 1 2 3



Exercise 10:

Write a program (WAP) that will first take n integers into an array A and then m integers into array B. Now swap all elements between array A and B. Finally show all elements of both array A and B.

Sample Input Sample Output
8
7 8 1 3 2 6 4 3
3
3 2 1
Array A : 3 2 1
Array B : 7 8 1 3 2 6 4 3

Solution:


    #include<stdio.h>
    int main() {
    int n, m, i, temp;
    // take input for array A
    scanf("%d", &n);
    int A[n];
    for(i=0; i<n; i++) {
        scanf("%d", &A[i]);
    }
    // take input for array B
    scanf("%d", &m);
    int B[m];
    for(i=0; i<m; i++) {
        scanf("%d", &B[i]);
    }
    
    // swap elements between array A and B
    for(i=0; i<n && i<m; i++) {
        temp = A[i];
        A[i] = B[i];
        B[i] = temp;
    }
    
    // display elements of array A and B
    printf("Array A: ");
    for(i=0; i<n; i++) {
        printf("%d ", A[i]);
    }
    printf("\n");
    
    printf("Array B: ");
    for(i=0; i<m; i++) {
        printf("%d ", B[i]);
    }
    return 0;
    }


Output:


    8
    7 8 1 3 2 6 4 3
    3
    3 2 1
    8
    Array A: 3 2 1 3 2 6 4 3 
    Array B: 7 8 1

Exercise 11:

Write a program (WAP) WAP that will take n positive integers into an array A. Now find all the integers that are divisible by 3 and replace them by -1 in array A. Finally show all elements of array A.

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

Solution:


  #include<stdio.h>
  int main() {
    int n, i;
    scanf("%d", &n);
    int a[n];
    for(i=0; i<n; i++) {
        scanf("%d", &a[i]);
    }
    // Replacing elements divisible by 3 with -1
    for(i=0; i<n; i++) {
        if(a[i]%3 == 0) {
            a[i] = -1;
        }
    }
    // Printing array elements
    for(i=0; i<n; i++) {
        printf("%d ", a[i]);
    }
  return 0;
  }


Output:


    8
    7 8 1 3 2 6 4 3
    7 8 1 -1 2 -1 4 -1