Chapter:

Arrays-

1. Write a C program to find determinant of n*n matrix.


#include
int main(){
    float  matrix[10][10], ratio, det;
    int i, j, k, n;
    printf("Enter order of matrix: ");
    fflush(stdout);
    scanf("%d", &n);
    printf("Enter the matrix: \n");
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
        	fflush(stdout);
            scanf("%f", &matrix[i][j]);
        }
    }
    /* Conversion of matrix to upper triangular */
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(j>i){
                ratio = matrix[j][i]/matrix[i][i];
                for(k = 0; k < n; k++){
                    matrix[j][k] -= ratio * matrix[i][k];
                }
            }
        }
    }
    det = 1; //storage for determinant
    for(i = 0; i < n; i++)
        det *= matrix[i][i];
    printf("The determinant of matrix is: %.2f\n\n", det);
    return 0;
}

Show More

2. Write a program in C to find maximum and minimum element in an array.


#include 

void main()
{
      int arr1[100];
      int i, mx, mn, n;
      printf("\n\nFind maximum and minimum element in an array :\n");
                                            printf("--------------------------------------------------\n");	
       printf("Input the number of elements to be stored in the array :");
       fflush(stdout);
       scanf("%d",&n);
       printf("Input %d elements in the array :\n",n);
       for(i=0;imx)
       	{
              mx = arr1[i];
           }
          if(arr1[i] 

Show More

3. Write a program in C to sort elements of an array in descending order.


#include 
void main()
{
    int arr1[100];
    int n, i, j, tmp;
    printf("\n\nsort elements of array in descending order :\n");
    printf("Input the size of array : ");
    fflush(stdout);
    scanf("%d", &n);
    printf("Input %d elements in the array :\n",n);
    for(i=0;iShow More

4. Write a program in C for multiplication of two square Matrices.


#include 
void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int c2);
void multiplyMatrices(int first[][10], int second[][10], int multResult[][10], int r1, int c1, int r2, int c2);
void display(int mult[][10], int r1, int c2);
int main() 
{
    int first[10][10], second[10][10], mult[10][10], r1, c1, r2, c2;
    printf("Enter rows and column for the first matrix: ");
    fflush(stdout);
    scanf("%d %d", &r1, &c1);
    printf("Enter rows and column for the second matrix: ");
      fflush(stdout);
    scanf("%d %d", &r2, &c2);
    // Taking input until columns of the first matrix is equal to the rows of the second matrix
    while (c1 != r2)
     {
        printf("Error! Enter rows and columns again.\n");
        printf("Enter rows and columns for the first matrix: ");
          fflush(stdout);
        scanf("%d%d", &r1, &c1);
        printf("Enter rows and columns for the second matrix: ");
          fflush(stdout);
        scanf("%d%d", &r2, &c2);
    }
    // Function to take matrices data
    enterData(first, second, r1, c1, r2, c2);
    // Function to multiply two matrices.
    multiplyMatrices(first, second, mult, r1, c1, r2, c2);
    // Function to display resultant matrix after multiplication.
    display(mult, r1, c2);
    return 0;
}
void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int c2) 
{
    printf("\nEnter elements of matrix 1:\n");
    for (int i = 0; i < r1; ++i)
     {
        for (int j = 0; j < c1; ++j)
         {
            printf("Enter a%d%d: ", i + 1, j + 1);
            scanf("%d", &first[i][j]);
        }
    }
    printf("\nEnter elements of matrix 2:\n");
    for (int i = 0; i < r2; ++i)
     {
        for (int j = 0; j < c2; ++j)
         {
            printf("Enter b%d%d: ", i + 1, j + 1);
            scanf("%d", &second[i][j]);
        }
    }
}
void multiplyMatrices(int first[][10], int second[][10], int mult[][10], int r1, int c1, int r2, int c2)
 {
    // Initializing elements of matrix mult to 0.
    for (int i = 0; i < r1; ++i) 
    {
        for (int j = 0; j < c2; ++j)
         {
            mult[i][j] = 0;
        }
    }
    // Multiplying first and second matrices and storing in mult.
    for (int i = 0; i < r1; ++i)
     {
        for (int j = 0; j < c2; ++j)
         {
            for (int k = 0; k < c1; ++k) 
            {
                mult[i][j] += first[i][k] * second[k][j];
            }
        }
    }
}
void display(int mult[][10], int r1, int c2)
 {
    printf("\nOutput Matrix:\n");
    for (int i = 0; i < r1; ++i) 
    {
        for (int j = 0; j < c2; ++j)
         {
            printf("%d  ", mult[i][j]);
            if (j == c2 - 1)
                printf("\n");
        }
    }
}
Show More

5. Write a program in C to insert New value in array.


#include 
void main()
{
   int arr1[100],i,n,p,x;
   printf("\n\nInsert New value in the unsorted array : \n ");
   printf("-----------------------------------------\n");   
   printf("Input the size of array : ");
   fflush(stdout);
   scanf("%d", &n);
    /* Stored values into the array*/
   printf("Input %d elements in the array in ascending order:\n",n);
    for(i=0;i=p;i--)
   {
      arr1[i]= arr1[i-1];
   }
   /* insert value at given position */
      arr1[p-1]=x;
   printf("\n\nAfter Insert the element the new list is :\n");
   for(i=0;i<=n;i++)
      printf("% 5d",arr1[i]);
	  printf("\n\n");
}
Insert New value in the unsorted array :                                                                      
 -----------------------------------------                                                                    
Input the size of array : 4                                                                                   
Input 4 elements in the array in ascending order:                                                             
element - 0 : 1                                                                                               
element - 1 : 8                                                                                               
element - 2 : 7                                                                                               
element - 3 : 10                                                                                              
Input the value to be inserted : 5                                                                            
Input the Position, where the value to be inserted :2                                                         
The current list of the array :                                                                               
    1    8    7   10                                                                                          
                                                                                                              
After Insert the element the new list is :                                                                    
    1    5    8    7   10  
Show More

6. Write a C Program to Add Two Matrices Using Multi-dimensional Arrays.


#include 
int main() 
{
    int r, c, a[100][100], b[100][100], sum[100][100], i, j;
    printf("Enter the number of rows (between 1 and 100): ");
    fflush(stdout);
    scanf("%d", &r);
    printf("Enter the number of columns (between 1 and 100): ");
    fflush(stdout);
    scanf("%d", &c);
    printf("\nEnter elements of 1st matrix:\n");
    for (i = 0; i < r; ++i)
    {
        for (j = 0; j < c; ++j) 
        {
            printf("Enter element a%d%d: ", i + 1, j + 1);
            fflush(stdout);
            scanf("%d", &a[i][j]);
        }
    }
    printf("Enter elements of 2nd matrix:\n");
    for (i = 0; i < r; ++i)
    {
        for (j = 0; j < c; ++j)
         {
            printf("Enter element a%d%d: ", i + 1, j + 1);
            fflush(stdout);
            scanf("%d", &b[i][j]);
        }
    }
    // adding two matrices
    for (i = 0; i < r; ++i)
    {
        for (j = 0; j < c; ++j) 
        {
            sum[i][j] = a[i][j] + b[i][j];
        }
    }
    // printing the result
    printf("\nSum of two matrices: \n");
    for (i = 0; i < r; ++i)
    {
        for (j = 0; j < c; ++j) 
        {
            printf("%d   ", sum[i][j]);
            if (j == c - 1) {
                printf("\n\n");
            }
        }
    }
    return 0;
}
Show More

7. Write a program in C to delete an element at desired position from an array.


#include 
void main()
{
  int arr1[50],i,pos,n;
  printf("\n\nDelete an element at desired position from an array :\n");
     printf("---------------------------------------------------------\n");  
  
  printf("Input the size of array : ");
  fflush(stdout);
  scanf("%d", &n);
    /* Stored values into the array*/
   printf("Input %d elements in the array in ascending order:\n",n);
   for(i=0;i
Output:
Delete an element at desired position from an array :                                                         
---------------------------------------------------------                                                     
Input the size of array : 5                                                                                   
Input 5 elements in the array in ascending order:                                                             
element - 0 : 1                                                                                               
element - 1 : 2                                                                                               
element - 2 : 3                                                                                               
element - 3 : 4                                                                                               
element - 4 : 5                                                                                               
                                                                                                              
Input the position where to delete: 3                                                                         
                                                                                                              
The new list is :   1  2  4  5 
Show More

8. Write a C program to find transpose of a matrix.


#include 
int main()
 {
    int a[10][10], transpose[10][10], r, c, i, j;
    printf("Enter rows and columns: ");
    fflush(stdout);
    scanf("%d %d", &r, &c);
    // Assigning elements to the matrix
    printf("\nEnter matrix elements:\n");
    for (i = 0; i < r; ++i)
    {
        for (j = 0; j < c; ++j)
         {
            printf("Enter element a%d%d: ", i + 1, j + 1);
            fflush(stdout);
            scanf("%d", &a[i][j]);
        }
    }
    // Displaying the matrix a[][]
    printf("\nEntered matrix: \n");
    for (i = 0; i < r; ++i)
    {
        for (j = 0; j < c; ++j)
         {
            printf("%d  ", a[i][j]);
            if (j == c - 1)
            {
                printf("\n");
            }
        }
    }
    // Finding the transpose of matrix a
    for (i = 0; i < r; ++i)
    {
        for (j = 0; j < c; ++j) 
        {
            transpose[j][i] = a[i][j];
        }
    }
    // Displaying the transpose of matrix a
    printf("\nTranspose of the matrix:\n");
    for (i = 0; i < c; ++i)
    {
        for (j = 0; j < r; ++j)
         {
            printf("%d  ", transpose[i][j]);
            if (j == r - 1)
            {
                printf("\n");
            }
        }
    }
    return 0;
}
Show More

9. Write a program in C to sort elements of an array in ascending order.

#include 

void main()
{
    int arr1[100];
    int n, i, j, tmp;
    printf("\n\nsort elements of array in ascending order :\n ");
    printf("Input the size of array : ");
    fflush(stdout);
    scanf("%d", &n);
       printf("Input %d elements in the array :\n",n);
       for(i=0;iShow More

10. Write a program in C to separate odd and even integers in separate arrays.


#include 
void main()
 {
       int arr1[10], arr2[10], arr3[10];
       int i,j=0,k=0,n;
       printf("\n\nSeparate odd and even integers in separate arrays:\n");
       printf("Input the number of elements to be stored in the array :");
       fflush(stdout);
       scanf("%d",&n);
       printf("Input %d elements in the array :\n",n);
       for(i=0;i


Show More

All Chapters

View all Chapter and number of question available From each chapter from C-programming-