Close
Close

Practice questions on Array

Level 1

1.
Print all element of an array using recusion after passing it into a function.

2.
What infromation does the name of an array store?

3.
What is the error in the following program:
#include <stdio.h>
int main(){
  int t[ ][ ] = {
    5,6,
    10,11
  } ;
}

4.
Make a 3D array.

Level 2

1.
Take 10 integer inputs from user and store them in an array and print them on screen.
#include <stdio.h>
int main()
{
  int a[10],num,i;
  for(i=0;i<10;i++){
      printf("Enter a number\n");
      scanf("%d",&a[i]);
  }
  printf("Numbers are:\n");
  for(i=0;i<10;i++){
      printf("%d\n",a[i]);
  }
  return 0;
}

2.
Take 10 integer inputs from user and store them in an array. Again ask user to give a number. Now, tell user whether that number is present in array or not.

3.
Take 20 integer inputs from user and print the following:
number of positive numbers
number of negative numbers
number of odd numbers
number of even numbers
number of 0.
#include <stdio.h>
int main()
{
  int z[20],num,i;
  int pos = 0;
  int neg = 0;
  int odd = 0;
  int even = 0;
  int zero = 0;
  for(i=0;i<20;i++)
  {
    printf("Enter a number\n");
    scanf("%d",&z[i]);
    if(z[i]>0){
      pos++;
    }
    else if(z[i]<0){
      neg++;
    }
    else{
      zero++;
    }
    if(z[i]%2==0){
      even++;
    }
    else{
      odd++;
    }
  }
  printf("Positive : %d\nNegative : %d\nZero : %d\nodd : %d\neven : %d\n",pos,neg,zero,odd,even);
  return 0;
}

4.
Take 10 integer inputs from user and store them in an array. Now, copy all the elements in another array but in reverse order.
#include <stdio.h>
int main()
{
  int a[10],b[10],i;
  for(i=0;i<10;i++)
  {
    printf("Enter a number\n");
    scanf("%d",&a[i]);
  }
  int j = 0;
  for(i=9;i>=0;i--)
  {
    b[i] = a[j];
    j++;
  }
  for(i=0;i<10;i++)
  {
    printf("%d\n",b[i]);
  }
  return 0;
}
									

5.
Write a program to find the sum of all elements of an array.

6.
Write a program to find the product of all elements of an array.
#include <stdio.h>
int main()
{
  int a[] = {1,3,4,6,7,2,5,23,4,54};
  int i,product = 1;
  for(i=0;i<10;i++)
  {
    product = product*a[i];
  }
  printf("%d\n",product);
  return 0;
}

7.
Take an array of 10 integers and print all its elements using pointer.

8.
Initialize and print all elements of a 2D array.

9.
Find largest and smallest elements of an array.
#include <stdio.h>
int main()
{
  int a[] = {23,6,328,34,12,234,9,23,4,54};
  int largest = a[0];
  int smallest = a[0];
  int i;

  for(i=0;i<10;i++)
  {
    if(a[i]>largest)
      largest = a[i];
    if(a[i]<smallest)
      smallest = a[i];
  }
  printf("Largest is %d and smallest is %d\n",largest,smallest);
  return 0;
}
									

10.
Write a program to print sum, average of all numbers, smallest and largest element of an array.

11.
Write a program to check if elements of an array are same or not if read from front or back. E.g.-
2 3 15 15 3 2
#include <stdio.h>
int main()
{
  int a[] = {2,3,15,15,3,2};
  int read = 1;
  int i,j = 5;
  for(i =0;i<6/2;i++)
  {
    if(a[i]!=a[j])
    {
      read = 0;
      break;
    }
    else
      j--;
  }
  printf("%d\n",read);
  return 0;
}

12.
Take an array of 10 elements. Split it into middle and store the elements in two dfferent arrays. E.g.-
INITIAL array :
58 24 13 15 63 9 8 81 1 78

After spliting :
58 24 13 15 63
9 8 81 1 78

Level 3

1.

Take an array of length n where all the numbers are nonnegative and unique. Find the element in the array possessing the highest value. Split the element into two parts where first part contains the next highest value in the array and second part hold the required additive entity to get the highest value. Print the array where the highest value get splitted into those two parts.
Sample input: 4 8 6 3 2
Sample output: 4 6 2 6 3 2

#include <stdio.h>
int main()
{
  int a[] = {4,8,6,3,2};
  int b[6],i;
  int highest = a[0];
  int second_highest = a[0];
  int j = 0;
  for(i=0;i<5;i++)
  {
    if(a[i]>highest)
    {
      highest = a[i];
      j = i;
    }
  }
  for(i = 0;i<5;i++)
  {
    if(a[i]<highest && a[i]>second_highest)
    {
      second_highest = a[i];
    }
  }

  for(i = 0;i<j;i++)
  {
    b[i] = a[i];
  }
  b[j] = second_highest;
  b[j+1] = highest-second_highest;
  for(i = j+2;i<6;i++)
  {
    b[i] = a[i-1];
  }
  for(i = 0;i<6;i++)
  {
    printf("%d\n",b[i]);
  }
  return 0;
}

2.

Write a program to shift every element of an array to circularly right. E.g.-
INPUT : 1 2 3 4 5
OUTPUT : 5 1 2 3 4

#include <stdio.h>
int main()
{
  int a[] = { 1,2,3,4,5 };
  int t = a[4],i;
  for(i = 4; i>=1; i--)
  {
      a[i]=a[i-1];
  }
  a[0]=t;
  for(i = 0; i<=4; i++)
  {
     printf("%d\n",a[i]);
  }
  return 0;
}

3.

Initialize a 2D array of 3*3 matrix. E.g.-
1 2 3
4 5 6
7 8 9

Check if the matrix is symmetric or not.

a[i][j]=a[j][i]

4.

Pass a 2D array to function and access all its elements.


5.

Sorting refers to arranging data in a particular format. Sort an array of integers in ascending order. One of the algorithm is selection sort. Use below explanation of selection sort to do this.
INITIAL ARRAY :
2 3 1 45 15
First iteration : Compare every element after first element with first element and if it is larger then swap. In first iteration, 2 is larger than 1. So, swap it.
1 3 2 45 15
Second iteration : Compare every element after second element with second element and if it is larger then swap. In second iteration, 3 is larger than 2. So, swap it.
1 2 3 45 15
Third iteration : Nothing will swap as 3 is smaller than every element after it.
1 2 3 45 15
Fourth iteration : Compare every element after fourth element with fourth element and if it is larger then swap. In fourth iteration, 45 is larger than 15. So, swap it.
1 2 3 15 45

#include <stdio.h>
int main()
{
  int a[] = { 2, 3, 1, 45, 15};
  int t,i,j;
  for(i = 0;i<4;i++){
    for(j = i+1;j<5;j++){
      if(a[i]>a[j]){
        t = a[i];
        a[i] = a[j];
        a[j] = t;
      }
    }
  }
  for(i = 0;i<5;i++){
    printf("%d\n",a[i]);
  }
  return 0;
}

6.

Write a program to add and multiply two 3x3 matrices. You can use 2D array to create a matrix.


Ask Yours
Post Yours