C Programs on Arrays

C Program to Calculate Average Using Arrays

Code:

#include <stdio.h>
int main()
{
    int num, i;
    float number[100], total = 0.0, average;
    printf("Enter a number: ");
    scanf("%d", &num);
    while (num > 100 || num < 1)
    {
        printf("Please enter a number between (1 to 100).\n");
        printf("Enter a number: ");
        scanf("%d", &num);
    }
    for (i = 0; i < num; ++i)
    {
        printf("Element[%d]: ", i + 1);
        scanf("%f", &number[i]);
        total += number[i];
    }
    average = total / num;
    printf("Average value is %.2f\n", average);
    return 0;
}

Output:

Enter a number: 5
Element[1]: 1
Element[2]: 2
Element[3]: 3
Element[4]: 4
Element[5]: 5
Average value is 3.00

C Program to Find Largest Element in an Array

Code:

#include <stdio.h>
int main()
{
    int i, num;
    float array[100];
    printf("Enter elements between 1 to 100: ");
    scanf("%d", &num);
    for (i = 0; i < num; ++i)
    {
        printf("Enter value: ", i + 1);
        scanf("%f", &array[i]);
    }
    for (i = 1; i < num; ++i)
    {
        if (array[0] < array[i])
            array[0] = array[i];
    }

    printf("Largest element is %.2f\n", array[0]);

    return 0;
}

Output:

Enter elements between 1 to 100: 25
Enter value: 1
Enter value: 2
Enter value: 3
Enter value: 4
Enter value: 5
Enter value: 6
Enter value: 7
Enter value: 8
Enter value: 9
Enter value: 10
Enter value: 11
Enter value: 12
Enter value: 13
Enter value: 14
Enter value: 15
Enter value: 16
Enter value: 17
Enter value: 18
Enter value: 19
Enter value: 20
Enter value: 21
Enter value: 22
Enter value: 23 
Enter value: 24
Enter value: 25
Largest element is 25.00

C Program to Calculate Standard Deviation

Code:

#include <math.h>
#include <stdio.h>
float Cal(float value[]);
float Cal(float value[])
{
    float total = 0.0, meanvalue, StandardDeviation = 0.0;
    int i;
    for (i = 0; i < 5; ++i)
    {
        total += value[i];
    }
    meanvalue = total / 5;
    for (i = 0; i < 5; ++i)
        StandardDeviation += pow(value[i] - meanvalue, 2);
    return sqrt(StandardDeviation / 5);
}
int main()
{
    int i;
    float value[5];
    printf("Enter five elements: ");
    for (i = 0; i < 5; ++i)
        scanf("%f", &value[i]);
    printf("Standard Deviation of elements is %.6f\n", Cal(value));
    return 0;
}

Output:

Enter five elements: 10
20
30
40
50
Standard Deviation of elements is 14.142136

C Program to Add Two Matrices Using Multi-dimensional Arrays

Code:

#include<stdio.h>
int main()
{
    int i, j,r=2,c=2,A[r][c],B[r][c],C[r][c];
    printf("Enter elements of first Matrix A: \n");
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {
            scanf("%d", &A[i][j]);
        }        
    }
    printf("\nEnter elements of Matrix B: \n");
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {         
            scanf("%d", &B[i][j]);
        }                
    }
    printf("\nMatrix C:\n");
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {
            C[i][j]=A[i][j]+B[i][j];
            printf("%5d ",C[i][j]);            
        }        
        printf("\n");
    }       
    return 0;
}

Output:

Enter elements of first Matrix A: 
1
2
3
4

Enter elements of Matrix B: 
4
3
2
1

Matrix C:
    5     5 
    5     5

C Program to Multiply Two Matrices Using Multi-dimensional Arrays

Code:

#include<stdio.h>
int main()
{
    int i, j,r=2,c=2,A[r][c],B[r][c],C[r][c];
    printf("Enter elements of first Matrix A: \n");
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {
            scanf("%d", &A[i][j]);
        }        
    }
    printf("\nEnter elements of Matrix B: \n");
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {         
            scanf("%d", &B[i][j]);
        }                
    }
    printf("\nMatrix C:\n");
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {
            C[i][j]=A[i][j] * B[i][j];
            printf("%5d ",C[i][j]);            
        }        
        printf("\n");
    }       
    return 0;
}

Output:

Enter elements of first Matrix A: 
1
2
3
4

Enter elements of Matrix B: 
4
3
2
1

Matrix C:
    4     6 
    6     4

C Program to Find Transpose of a Matrix

Code:

#include<stdio.h>
int main()
{
  int i,j,a[3][3],b[3][3];
  printf("Enter the values below: \n");
    for ( i = 0; i < 3; i++)
    {
      for ( j = 0; j < 3; j++)
      {
        printf("a[%d][%d]: ",i,j);
        scanf("%d",&a[i][j]);
      }
    }
    printf("\nEntered matrix is: \n");
    for ( i = 0; i < 3; i++)
    {
      printf("\n");
      for ( j = 0; j < 3; j++)
      {
        printf("%d\t",a[i][j]);
      }
    }
    for ( i = 0; i < 3; i++)
    {
      for ( j = 0; j < 3; j++)
      {
        b[i][j]=a[j][i];
      } 
    }
    printf("\nTranspose of the matrix is: \n");
    for ( i = 0; i < 3; i++)
    {
      printf("\n");
      for ( j = 0; j < 3; j++)
      {
        printf("%d\t",b[i][j]);
      }
    }
    return 0;      
}

Output:

Enter the values below: 
a[0][0]: 1
a[0][1]: 2
a[0][2]: 3
a[1][0]: 4
a[1][1]: 5
a[1][2]: 6
a[2][0]: 7
a[2][1]: 8
a[2][2]: 9

Entered matrix is: 

1       2       3
4       5       6
7       8       9
Transpose of the matrix is: 

1       4       7
2       5       8
3       6       9