Problem Solving Examples of Array

Problem solving examples of array 

In this blog you will get problem solving examples based on array which are very important  for your logic building ,placements, internships and many more. If you done this all problems you will definitely be master in arrays. I am also a engineering student hence i am giving you this problems which helps me a lot in my placements internships and also it develops my logic . If want more such examples on different topics of c language so comment in comment box. Enjoy the code dear future developer.



 1*// WAP to read nos in an int array and find greatest no in that array

#include<stdio.h>

main()

{

    int i,arr[10],G;

    printf("Enter an array of size 10");

    G=arr[0];

    for(i=0;i<=9;i++)

    {

        scanf("%d",&arr[i]);

    if(arr[i]>G)

    G=arr[i];

    }

    printf("Greatest no is %d",G);


}

Output:

Enter an array of size 10: 54 56 12 10 25 36 78 96 46 10

Greatest no is 96

---------------------------------------------------------------------------------------------------------------

2*

//WAP to read 10 nos in an int array and find smallest no in that array

#include<stdio.h>

main()

{

    int i,arr[10],G;

    printf("Enter the array of size 10");

    for(i=0;i<=9;i++)

    scanf("%d",&arr[i]);

    G=arr[0];

    for(i=1;i<=9;i++)

   {

     if(arr[i]<G)

    G=arr[i];

   }

   printf("Smallest no is %d",G);

}


Enter the array of size 10 1 2 3 4 5 6 7 8 9 10

Smallest no is 1

---------------------------------------------------------------------------------------------------------------

3*// WAP to read 10 nos in an array and search for an element in that array

#include<stdio.h>

main()

{

    int i,s;

    int arr[10];

    printf("Enter an array of size 10\n"); // '\n' (new line)

    for(i=0;i<=9;i++)

    scanf("%d",&arr[i]);


    printf("enter the search element\n");

    scanf("%d",&s);


    for(i=0;i<=9;i++)

    if(arr[i]==s)

    {

    printf("found\n");

    break;

    }

    if(i==10)

    printf("Not found\n");

}


Output :

    Enter an array of size 10

1 2 3 4 5 6 7 8 9 10

enter the search element

5

found

---------------------------------------------------------------------------------------------------------------

4*// WAP to read 10 nos in an array and search for an element and also print its location

#include<stdio.h>

main()

{

    int i,s;

    int arr[10];

    printf("Enter an array of size 10\n");

    for(i=0;i<=9;i++)

    scanf("%d",&arr[i]);

    printf("enter a search element\n");

    scanf("%d",&s);

    for(i=0;i<=9;i++)

    if(arr[i]==s)

    {

    printf("found");

    printf("The location of search element is %d",i+1);

    break;

    }

    if(i==10)

        printf("Not found");

}

Output :

Enter an array of size 10:

1 2 5 3 8 9 45 62 99 100

enter a search element

4

Not found

---------------------------------------------------------------------------------------------------------------

5*//WAP to read 10 nos in array and sort that array using bubble sort.(Ascending order)

#include<stdio.h>

main()

{

    int arr[10],i,j,temp;

    printf("Enter an array of size 10\n");

    for(i=0;i<=9;i++)

    scanf("%d",&arr[i]);

    

    for(j=9;j>0;j--)

        for(i=0;i<=j;i++)

        if(arr[i]>arr[i+1])

        {

        temp=arr[i];          //Here swapping two no is done

        arr[i]=arr[i+1]; 

        arr[i+1]=temp;

        }

    printf("The sorted array is\n");

    for(i=0;i<=9;i++)   

    printf(" %d ",arr[i]);

}

Output:

Enter an array of size 10

10 9 8 7 5 6 4 3 1 2

The sorted array is

1 2 3 4 5 6 7 8 9 10

---------------------------------------------------------------------------------------------------------------

6*//Wap to read 10 nos in array and sort that array using bubble sort(descending order)

#include<stdio.h>

main()

{

int arr[10],i,j,a;

printf("Enter an array of size 10\n");

for(i=0;i<=9;i++)

scanf("%d",&arr[i]);

for(j=9;j>0;j--)

for(i=0;i<j;i++)

if(arr[i]<arr[i+1])

{

a=arr[i];

arr[i]=arr[i+1];

arr[i+1]=a;

}

printf("The sorted array is \n");

for(i=0;i<=9;i++)

printf(" %d",arr[i]);

}


Output:

 Enter an array of size 10:

0 1 2 5 99 120 300 88 99 50

The sorted array is

 300 120 99 99 88 50 5 2 1 0

--------------------------------------------------------------------------------------------------------------   

7*//WAP to read an array of n size and display it.

#include<stdio.h>

main()

{

    int n,i;

    printf("Enter an array of n size\n");

    scanf(" %d",&n);

    int arr[n];

    printf("Enter %d element\n");

    for(i=0;i<n;i++)

    scanf(" %d",&arr[i]);


    for(i=0;i<n;i++)

    printf(" %d",arr[i]);

}

Output:

Enter an array of n size

5

Enter 5 element

1 2 3 4 5 

1 2 3 4 5

-----------------------------------------------------------------------------------

8*// Wap to read and arraay of n size and find sum of all elements.

#include<stdio.h>

main()

{

    int i=0,s=0,n;

    printf("Enter a size of array\n");

    scanf("%d",&n);

    int arr[n];

    printf("Enter the %d elements\n");

    while(i<n)

    {

        scanf("%d",&arr[i]);

        s=s+arr[i];

        i++;

    }

    printf("The sum of elements is %d",s);

}

Output:

Enter a size of array

5

Enter the 5 elements

1 2 3 4 5

The sum of elements is 15

-----------------------------------------------------------------------------------

9*//wap to read an array of n size and count all negative no.

#include<stdio.h>

main()

{

    int n,i,c=0;

    printf("Enter a size of array\n");

    scanf("%d",&n);

    int arr[n];

    printf("Enter the %d elements of array\n");

    do

    {

        scanf("%d",&arr[i]);

        if(arr[i]<0)

        c=c+1;

        i++;

    }while(i<n);

    printf("The count of negative no is %d",c);

}    

Output:

Enter a size of array

5

Enter the elements of array

1 2 3 4 5

The count of negative no is 5

-----------------------------------------------------------------------------------

10*// Wap to read an array of n size and count all even as well as odd nos.

#include<stdio.h>

main()

{

    int n,i,e=0,o=0;

    printf("Enter the size of array\n");

    scanf("%d",&n);

    int arr[n];

    printf("Enter the %d elements of array\n");

    for(i=0;i<n;i++)

    {

    scanf("%d",&arr[i]);

    if(arr[i]%2==0)

    e=e+1;

    else

    o=o+1;

    }

    printf("There are %d even no and %d odd no",e,o);

}    

Output:

Enter the size of array

5

Enter the 5 elements of array

1 2 3 4 5

There are 2 even no and 3 odd no

---------------------------------------------------------------------------------------------------------------

11*//wap to read an array of size 3*4 and find sum of elements.Multidimensional array

#include<stdio.h>

main()

{

    int a[3][4];

    int i,j,s=0;

    printf("Enter an array of size 3*4\n");

    for(i=0;i<=2;i++)

    {

        for(j=0;j<=3;j++)

        {

            scanf("%d",&a[i][j]);

            s=s+a[i][j];

        }

    }

    printf("sum is %d\n",s);

}


Output:

Enter an array of size 3*4

1 2 3 4 

5 6 7 8

9 1 1 2

sum is 49

--------------------------------------------------------------------------------------------------------------

12*// wap to read an array of size 4*4 and display it.Multidimensional array

#include<stdio.h>

main()

{

    int a[4][4],i,j;

    printf("Enter an array of size 4*4\n");

    for(i=0;i<=3;i++)

    {

      for(j=0;j<=3;j++)

      scanf("%d",&a[i][j]);

    }

    printf("The matrix array is :\n");

    for(i=0;i<=3;i++)

    {

        for(j=0;j<=3;j++)

        printf(" %d ",a[i][j]);

        printf("\n");

    }

    return 0;

}

Output:

Enter an array of size 4*4

1 2 3 4

5 6 7 8

9 1 1 2

1 4 6 8

The matrix array is :

 1  2  3  4

 5  6  7  8

 9  1  1  2

 1  4  6  8

--------------------------------------------------------------------------------------------------------------


Comments