Add

Arrays program in language 13 useful program

 1.Program to print the reverse of an Array.

 simple program to reverse an array.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int c, d, n, a[100], b[100];

    printf("\n\nEnter number of elements in array :");

    scanf("%d", &n);

    printf("\n\nEnter %d elements\n", n);

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

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

    /*

        temporarily storing elements into array b

        starting from end of array a

    */

    for(c = n-1, d = 0; c >= 0; c--, d++)

        b[d] = a[c];

    /*

        copying reversed array into original.

        Here we are modifying original array to reverse it.

    */

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

        a[c] = b[c];

    printf("\n\n Resultant array is: ");

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

        printf("%d", a[c]);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

2. Program to insert an element in an Array.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int array[100], position, c, n, value;

    printf("\n\nEnter number of elements in array:");

    scanf("%d", &n);

    printf("\n\nEnter %d elements\n", n);

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

        scanf("%d", &array[c]);

    printf("\n\nEnter the location where you want to insert new element:  ");

    scanf("%d", &position);

    printf("\n\nEnter the value to insert: ");

    scanf("%d", &value);

    // shifting the elements from (position to n) to right

    for(c = n-1; c >= position-1; c--)

        array[c+1] = array[c];

    array[position - 1] = value;    // inserting the given value

    printf("\n\nResultant array is: ");

    /*

        the array size gets increased by 1

        after insertion of the element

    */

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

        printf("%d  ", array[c]);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

In the above program we take an array as user input and then ask the user for a new number that they wish to add to the original array, and the position where they want to add the new number.

The we shift the existing numbers from the index position to the end of the array one position to the right, therby vacating a space for the new element. And then we add the new number at the user specified position index.

Program to Delete an Element from Array in C.

3.Program to delete an element from array, where the position of element to be deleted is given by user.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int array[100], position, c, n;

    printf("\n\nEnter number of elements in array:");

    scanf("%d", &n);

    printf("\n\nEnter %d elements\n", n);

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

        scanf("%d", &array[c]);

    printf("\n\nEnter the location where you want to delete element from:  ");

    scanf("%d", &position);

    if(position >= n+1)

        printf("\n\nDeletion not possible\n\n");

    else

        // updating the locations with next elements

        for(c = position-1; c < n-1; c++)

        array[c] = array[c+1];

    printf("\n\nResultant array is: ");

    /*

        the array size gets reduced by 1

        after deletion of the element

    */

    for(c = 0; c < n-1; c++)

        printf("%d  ", array[c]);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

3.Program to Delete an element from array based on value.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int array[10], element, c, n, pos;

    /*

        initialization as garbage value is

        stored by default in c variables

    */

    int found = 0;

    printf("\n\nEnter number of elements in array:");

    scanf("%d", &n);

    printf("\n\nEnter %d elements\n", n);

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

        scanf("%d", &array[c]);

    printf("\n\nThe input array is: ");

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

    printf("%d", array[c]);

    printf("\n\nEnter the element to be deleted: ");

    scanf("%d", &element);

    // check the element to be deleted is in array or not

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

    {

        if(array[c] == element)

        {

            found = 1;

            pos = c;

            break;  // terminate the loop

        }

    }

    if(found == 1) // the element to be deleted exists in the array

    {

        for(c = pos; c < n-1; c++)

            array[c] = array[c+1];

    }

    else

        printf("\n\nElement %d is not found in the array\n\n", element);

 

    printf("\n\nResultant array is: ");

    /*

        the array size gets reduced by 1

        after deletion of the element

    */

    for(c = 0; c < n-1; c++)

        printf("%d  ",array[c]);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

4. Program to find Largest and Smallest Element in an Array.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int a[50], size, i, big, small;

    printf("\nEnter the size of the array: ");

    scanf("%d", &size);

    printf("\n\nEnter the %d elements of the array: \n\n", size);

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

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

    big = a[0]; // initializing

    /*

        from 2nd element to the last element

        find the bigger element than big and

        update the value of big

    */

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

    {

        if(big < a[i])   // if larger value is encountered

        {

            big = a[i]; // update the value of big

        }

    }

    printf("\n\nThe largest element is: %d", big);

    small = a[0];   // initializing

    /*

        from 2nd element to the last element

        find the smaller element than small and

        update the value of small

    */

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

    {

        if(small>a[i])   // if smaller value is encountered

        {

            small = a[i];   // update the value of small

        }

    }

    printf("\n\nThe smallest element is: %d", small);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

5. Program to find Sum of N input Numbers using Array.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int n, sum = 0, c, array[100];

    printf("Enter the number of integers you want to add: ");

    scanf("%d", &n);

    printf("\n\nEnter %d integers \n\n", n);

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

    {

        scanf("%d", &array[c]);

        sum += array[c];    // same as sum = sum + array[c]

    }

    printf("\n\nSum = %d\n\n", sum);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

6. Program to Sort Array elements.

#include<stdio.h>

#include<conio.h>

void sorting(int *x, int y);

void main()

{

    int a[20], i, c, n;

    clrscr();

    printf("Enter number of elements you want to sort: ");

    scanf("%d", &n);

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

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

    sorting(a, n);

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

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

    getch();

}

void sorting(int *x, int y)

{

    int i, j, temp;

    for(i = 1; i <= y-1; i++)

    {

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

        {

            if(*(x+j) > *(x+j+1))

            {

                temp = *(x+j);

                *(x+j) = *(x+j+1);

                *(x+j+1) = temp;

            }

        }

    }

}

7. Program to remove Duplicate Element in an Array.

we will learn how to remove a duplicate element from an array. Before moving forward with the program, if you are not familiar with what is an Array please read this article:




Remove duplicates from the sorted array:

we are implementing the program to remove a duplicate element from a sorted array. We will create a temporary array and copy elements from one array to another only in case when there is no next matching element.

 

Note: This program will work only for sorted array so while providing an input make sure that the given array is in a sorted array, otherwise it will give unexpected outputs.

#include <stdio.h>

int remove_duplicate(int arr[], int n)

{

  if (n == 0 || n == 1)

    return n;

  int temp[n];

  int j = 0;

  int i;

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

    if (arr[i] != arr[i + 1])

      temp[j++] = arr[i];

  temp[j++] = arr[n - 1];

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

    arr[i] = temp[i];

  return j;

}

int main()

{

  int n;

  scanf("%d", &n);

  int arr[n];

  int i;

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

  {

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

  }

  printf("\nArray Before Removing Duplicates: ");

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

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

  n = remove_duplicate(arr, n);

  printf("\nArray After Removing Duplicates: ");

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

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

  return 0;

}

8. Program to check whether a two dimensional array is a Sparse Matrix.

A Sparse Matrix is a matrix(two-dimensional array) in which number of 0's is greater than the number of non-zero elements.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int n, m, c, d, matrix[10][10];

    int counter = 0;

    printf("\nEnter the number of rows and columns of the matrix \n\n");

    scanf("%d%d",&m,&n);

    printf("\nEnter the %d elements of the matrix \n\n", m*n);

    for(c = 0; c < m; c++)   // to iterate the rows

    {

        for(d = 0; d < n; d++)   // to iterate the columns

        {

            scanf("%d", &matrix[c][d]);

            if(matrix[c][d] == 0)

            counter++;  // same as counter=counter +1

        }

    }

    // printing the matrix

    printf("\n\nThe entered matrix is: \n\n");

    for(c = 0; c < m; c++)   // to iterate the rows

    {

        for(d = 0; d < n; d++)   // to iterate the columns

        {

            printf("%d\t", matrix[c][d]);

        }

    printf("\n"); // to take the control to the next row

    }

    // checking if the matrix is sparse or not

    if(counter > (m*n)/2)

        printf("\n\nThe entered matrix is a sparse matrix\n\n");

    else

        printf("\n\nThe entered matrix is not a sparse matrix\n\n");

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

9. Program to check whether given Square Matrix is symmetric or not.

A Square Matrix is said to be symmetric if it is equal to it's transpose.

Transpose of a matrix is achieved by exchanging indices of rows and columns.

Transpose is only defined for a square matrix.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int c, d, a[10][10], b[10][10], n, temp;

    printf("\nEnter the dimension of the matrix: \n\n");

    scanf("%d", &n);

    printf("\nEnter the %d elements of the matrix: \n\n",n*n);

    for(c = 0; c < n; c++) // to iterate the rows

        for(d = 0; d < n; d++) // to iterate the columns

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

    // finding transpose of a matrix and storing it in b[][]

    for(c = 0; c < n; c++) // to iterate the rows

        for(d = 0; d < n; d++) //to iterate the columns

            b[d][c] = a[c][d];

    // printing the original matrix

    printf("\n\nThe original matrix is: \n\n");

    for(c = 0; c < n; c++)   // to iterate the rows

    {

        for(d = 0; d < n; d++)   // to iterate the columns

        {

            printf("%d\t", a[c][d]);

        }

    printf("\n");

    }

    // printing the transpose of the entered matrix

    printf("\n\nThe Transpose matrix is: \n\n");

    for(c = 0; c < n; c++) // to iterate the rows

    {

        for(d = 0; d < n; d++)   // to iterate the columns

        {

            printf("%d\t", b[c][d]);

        }

        printf("\n");

    }

    // checking if the original matrix is same as its transpose

    for(c = 0; c < n; c++)   // to iterate the rows

    {

        for(d = 0; d < n; d++)   // to iterate the columns

        {

            /*

                even if they differ by a single element,

                the matrix is not symmetric

            */

            if(a[c][d] != b[c][d])

            {

                printf("\n\nMatrix is not Symmetric\n\n");

                exit(0);    // a system defined method to terminate the program

            }

        }

    }

 

    /*

        if the program is not terminated yet,

        it means the matrix is symmetric

    */

    printf("\n\nMatrix is Symmetric\n\n");

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

10. Program to find Deteminant of 2x2 Matrix.

Please note that, when we say a 2x2 matrix, we mean an array of 2x2.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

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

    long determinant;

    printf("\n\nEnter the 4 elements of the array\n");

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

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

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

    printf("\n\nThe entered matrix is: \n\n");

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

    {

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

        {

            printf("%d\t", a[i][j]);   // to print the complete row

        }

        printf("\n"); // to move to the next row

    }

    // finding the determinant of a 2x2 matrix

    determinant = a[0][0]*a[1][1] - a[1][0]*a[0][1];

    printf("\n\nDterminant of 2x2 matrix is : %d - %d =  %d", a[0][0]*a[1][1], a[1][0]*a[0][1], determinant);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

11. Program to find Normal and Trace of a Square Matrix.

Normal and Trace are only defined for a square matrix.

Square Matrix: Matrix in which, the number of rows = number of columns.

Normal: Square root of the sum of the squares of each element of the matrix.

Trace: Sum of the diagonal elements of a matrix.

Diagonal Element: An element having same indices for row and column.

#include<stdio.h>

/*

    to use the sqrt method to find

    the square root of a number we include

    math.h header file

*/

#include<math.h> 

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int i, j, n, aj[10][10], sum = 0, sum1 = 0, a = 0, normal;

    printf("\nEnter the number of rows (columns) of the matrix: \n\n");

    scanf("%d", &n);

    printf("\nEnter the %d elements of the first matrix: \n\n", n*n);

    for(i = 0; i < n; i++)   // to iterate the rows

    {

        for(j = 0; j < n; j++)   // to iterate the columns

        {

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

            a = aj[i][j]*aj[i][j];  // finding square of each element

            sum1 += a;  // same as sum1 = sum1 + a

        }

    }

    normal = sqrt((double)sum1);    // typecasting to double value

    printf("\n\nThe normal of the given matrix is: %d", normal);

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

    {

        sum = sum + aj[i][i];   // sum of the diagonal elements

    }

    printf("\n\nThe Trace of the given matrix is: %d", sum);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

12. Program to perform addition and subtraction of Matrices.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];

    printf("\nEnter the number of rows and columns of the first matrix \n\n");

    scanf("%d%d", &m, &n);

    printf("\nEnter the %d elements of the first matrix \n\n", m*n);

    for(c = 0; c < m; c++)   // to iterate the rows

        for(d = 0; d < n; d++)   // to iterate the columns

            scanf("%d", &first[c][d]);

    printf("\nEnter the %d elements of the second matrix \n\n", m*n);

    for(c = 0; c < m; c++)   // to iterate the rows

        for(d = 0; d < n; d++)   // to iterate the columns

            scanf("%d", &second[c][d]);

    /*

        printing the first matrix

    */

    printf("\n\nThe first matrix is: \n\n");

    for(c = 0; c < m; c++)   // to iterate the rows

    {

        for(d = 0; d < n; d++)   // to iterate the columns

        {

            printf("%d\t", first[c][d]);

        }

    printf("\n");

    }

    /*

        printing the second matrix

    */

    printf("\n\nThe second matrix is: \n\n");

    for(c = 0; c < m; c++)   // to iterate the rows

    {

        for(d = 0; d < n; d++)   // to iterate the columns

        {

            printf("%d\t", second[c][d]);

        }

    printf("\n");

    }

    /*

        finding the SUM of the two matrices

        and storing in another matrix sum of the same size

    */

    for(c = 0; c < m; c++)

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

            sum[c][d] = first[c][d] + second[c][d];

    // printing the elements of the sum matrix

    printf("\n\nThe sum of the two entered matrices is: \n\n");

    for(c = 0; c < m; c++)

    {

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

        {

            printf("%d\t", sum[c][d]);

        }

        printf("\n");

    }

    /*

        finding the DIFFERENCE of the two matrices

        and storing in another matrix difference of the same size

    */

    for(c = 0; c < m; c++)

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

            diff[c][d] = first[c][d] - second[c][d];

    // printing the elements of the diff matrix

    printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");

    for(c = 0; c < m; c++)

    {

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

        {

            printf("%d\t", diff[c][d]);

        }

        printf("\n");

    }

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

13. Program for Matrix Multiplication.

Two matrices with a given order can be multiplied only when number of columns of first matrix is equal to the number of rows of the second matrix.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int n, m, c, d, p, q, k, first[10][10], second[10][10], pro[10][10],sum = 0;

    printf("\nEnter the number of rows and columns of the first matrix: \n\n");

    scanf("%d%d", &m, &n);

    printf("\nEnter the %d elements of the first matrix: \n\n", m*n);

    for(c = 0; c < m; c++)   // to iterate the rows

        for(d = 0; d < n; d++)   // to iterate the columns

            scanf("%d", &first[c][d]);

    printf("\nEnter the number of rows and columns of the first matrix: \n\n");

    scanf("%d%d", &p, &q);

    if(n != p)

        printf("Matrices with the given order cannot be multiplied with each other.\n\n");

    else    // matrices can be multiplied

    {

        printf("\nEnter the %d elements of the second matrix: \n\n",m*n);

        for(c = 0; c < p; c++)   // to iterate the rows

            for(d = 0; d < q; d++)   // to iterate the columns

                scanf("%d", &second[c][d]);

        // printing the first matrix

        printf("\n\nThe first matrix is: \n\n");

        for(c = 0; c < m; c++)   // to iterate the rows

        {

            for(d = 0; d < n; d++)   // to iterate the columns

            {

                printf("%d\t", first[c][d]);

            }

            printf("\n");

        }

        // printing the second matrix

        printf("\n\nThe second matrix is: \n\n");

        for(c = 0; c < p; c++)   // to iterate the rows

        {

            for(d = 0; d < q; d++)   // to iterate the columns

            {

                printf("%d\t", second[c][d]);

            }

            printf("\n");

        }

        for(c = 0; c < m; c++)   // to iterate the rows

        {

            for(d = 0; d < q; d++)   // to iterate the columns

            {

                for(k = 0; k < p; k++)

                {

                    sum = sum + first[c][k]*second[k][d];

                }

            pro[c][d] = sum;    // resultant element of pro after multiplication

            sum = 0;    // to find the next element from scratch

            }

        }

        // printing the elements of the product matrix

        printf("\n\nThe multiplication of the two entered matrices is: \n\n");

        for(c = 0; c < m; c++)   // to iterate the rows

        {

            for(d = 0; d < q; d++)   // to iterate the columns

            {

                printf("%d\t", pro[c][d]);

            }

            printf("\n"); // to take the control to the next row

        }

    }

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.