ARAYS

An array is a collection of similar data elements stored at contiguous memory locations. It is the simplest data structure where each data element can be accessed directly by only using its index number. For instance, if we want to store the marks scored by a student in 5 subjects, then there’s no need to define individual variables for each subject. Rather, we can define an array which will store the data elements at contiguous memory locations

array marks[5] defines the marks scored by a student in 5 different subjects where each subject marks are located at a particular location in the array

Example 1:Program to take 5 values from the user and store them in an array

#include<stdio.h>
        
int main() {
    int values[5];
            
    printf("Enter 5 integers: ");
    
    // taking input and storing it in an array
    for(int i = 0; i < 5; ++i) {
    scanf("%d", &values[i]);
    }
            
    printf("Displaying integers: ");
            
    // printing elements of an array
    for(int i = 0; i < 5; ++i) {
    printf("%d\n", values[i]);
    }
    return 0;
}
       

Example 2:Program to find the average of n numbers using arrays

#include<stdio.h>
        
int main() {
                
    int marks[10], i, n;
    float sum = 0, average;
                
    printf("Enter number of elements: ");
    scanf("%d", &n);
                
    for(i=0; i<n; ++i) {
    printf("Enter number%d: ",i+1);
    scanf("%d", &marks[i]);
                          
    // add int entered by the user to the sum variable
    sum += marks[i];
    }
                
    average = sum / n;
    printf("Average = %f", average);
            
    return 0;
    }
}                         

Example 3:C program to find maximum and minimum element in array


#include<stdio.h>
void main()
{
    int arr1[100];
    int i, mx, mn, n;
    printf("Input the number of elements:");
    scanf("%d",&n);
       
    printf("Input %d  :\n",n);
    for(i=0;i<n;i++)
    {
    printf("element - %d : ",i);
    scanf("%d",&arr1[i]);
    }
    mx = arr1[0];
    mn = arr1[0];
    
    for(i=1; i<n; i++)
    {
    if(arr1[i]>mx)
    {
    mx = arr1[i];
    }
    
    
    if(arr1[i]<mn)
    {
    mn = arr1[i];
    }
    }
    printf("Maximum element is : %d\n", mx);
    printf("Minimum element is : %d\n\n", mn);
}
    

Example 4:Write a C program to count even and odd elements in an array

#include<stdio.h>
int main()
{
    int Size, i, a[10];
    int Even_Count = 0, Odd_Count = 0;
    printf("\n Please Enter the Size of an Array :  ");
    scanf("%d", &Size);
             
    printf("\nPlease Enter the Array Elements\n");
    for(i = 0; i < Size; i++)
    {
    scanf("%d", &a[i]);
    }
            
    for(i = 0; i < Size; i ++)
    {
    if(a[i] % 2 == 0)
    {
    Even_Count++;
    }
    else
    {
    Odd_Count++;
    }
    }
              
    printf("\nEven Numbers= %d ", Even_Count);
    printf("\nOdd Numbers= %d ", Odd_Count);
    return 0;
}
            

Example 5:Write a C Program to Calculate Addition of All Elements in Array

#include<stdio.h>

int main() {
    int i, arr[50], sum, num;
            
    printf("\nEnter no of elements :");
    scanf("%d", &num);
            
    //Reading values into Array
    printf("\nEnter the values :");
    for (i = 0; i < num; i++)
    scanf("%d", &arr[i]);
            
    //Computation of total
    sum = 0;
    for (i = 0; i < num; i++)
    sum = sum + arr[i];
    
    //Printing of all elements of array
    for (i = 0; i < num; i++)
    printf("\na[%d]=%d", i, arr[i]);
            
    //Printing of total
    printf("\nSum=%d", sum);
            
    return (0);
}
                      

Two Dimensional Array in C

The two-dimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure

It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required.

Example 6:Simple Two dimensional(2D) Array Example

#include<stdio.h>
int main(){
/* 2D array declaration*/
    int disp[2][3];
    /*Counter variables for the loop*/
    int i, j;
    for(i=0; i<2; i++) {
    for(j=0;j<3;j++) {
    printf("Enter value for disp[%d][%d]:", i, j);
    scanf("%d", &disp[i][j]);
    }
    }
    //Displaying array elements
    printf("Two Dimensional array elements:\n");
    for(i=0; i<2; i++) {
    for(j=0;j<3;j++) {
    printf("%d ", disp[i][j]);
    if(j==2){
    printf("\n");
    }
    }
    }
    return 0;
}
      

Example 7:Matrix multiplication in C

#include<stdio.h>  
#include<stdlib.h>
int main(){
    int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;  
    system("cls");
    printf("enter the number of row=");  
    scanf("%d",&r);  
    printf("enter the number of column=");  
    scanf("%d",&c);  
    printf("enter the first matrix element=\n");  
    for(i=0;i<r;i++)  
    {  
    for(j=0;j<c;j++)  
    {  
    scanf("%d",&a[i][j]);  
    }  
    }  
    printf("enter the second matrix element=\n");  
    for(i=0;i<r;i++)  
    {  
    for(j=0;j<c;j++)  
    {  
    scanf("%d",&b[i][j]);  
    }  
    }  
              
    printf("multiply of the matrix=\n");  
    for(i=0;i<r;i++)  
    {  
    for(j=0;j<c;j++)  
    {  
    mul[i][j]=0;  
    for(k=0;k<c;k++)  
    {  
    mul[i][j]+=a[i][k]*b[k][j];  
    }  
    }  
    }  
    //for printing result  
    for(i=0;i<r;i++)  
    {  
    for(j=0;j<c;j++)  
    {  
    printf("%d\t",mul[i][j]);  
    }  
    printf("\n");  
    }  
    return 0;
}
             
      

Example 8:TRANSPOSE OF MATRIX:

#include <stdio.h>
    int main() {
    int a[10][10], transpose[10][10], r, c;
    printf("Enter rows and columns: ");
    scanf("%d %d", &r, &c);
            
    // asssigning elements to the matrix
    printf("\nEnter matrix elements:\n");
    for (int i = 0; i < r; ++i)
    for (int j = 0; j < c; ++j) 
    printf("Enter element a%d%d: ", i + 1, j + 1);
    scanf("%d", &a[i][j]);
    }
        
    // printing the matrix a[][]
    printf("\nEntered matrix: \n");
    for (int i = 0; i < r; ++i)
    for (int j = 0; j < c; ++j) {
    printf("%d  ", a[i][j]);
    if (j == c - 1)
    printf("\n");
    }
            
    // computing the transpose
    for (int i = 0; i < r; ++i)
    for (int j = 0; j < c; ++j) {
    transpose[j][i] = a[i][j];
    }
            
    // printing the transpose
    printf("\nTranspose of the matrix:\n");
    for (int i = 0; i < c; ++i)
    for (int j = 0; j < r; ++j) {
    printf("%d  ", transpose[i][j]);
    if (j == r - 1)
    printf("\n");
    }
    return 0;
} 
    

Example 9:C Program for Bubble Sort to Sort Elements in An Order

#include<stdio.h>
int main()
{
    int array[100], n, i, j, swap; 
    printf("Enter number of elements:");
    scanf("%d", &n); 
    printf("Enter %d Numbers:", n); 
    for(i = 0; i < n; i++)
    scanf("%d", &array[i]); 
    for(i = 0 ; i < n - 1; i++)
    {
    for(j = 0 ; j < n-i-1; j++)
    {
    if(array[j] > array[j+1]) 
    {
    swap=array[j];
    array[j]=array[j+1];
    array[j+1]=swap;
    }
    }
    } 
    printf("Sorted Array:"); 
    for(i = 0; i < n; i++)
    printf("%d ", array[i]);
    return 0;
}
            

Example 10:Insertion Sort Program in C

#include<stdio.h>
int main(){
            
             
    int i, j, count, temp, number[25];
    
    printf("Total Elements: ");
    scanf("%d",&count);
    
    printf("Enter %d elements: ", count);
    //store input numbers in array
    for(i=0;i<count;i++)
    scanf("%d",&number[i]);
            
    // Implementation of insertion sort
    for(i=1;i<count;i++){
    temp=number[i];
    j=i-1;
    while((temp<number[j])&&(j>=0)){
    number[j+1]=number[j];
    j=j-1;
    }
    number[j+1]=temp;
    }
            
    printf("Order of Sorted elements: ");
    for(i=0;i<count;i++)
    printf(" %d",number[i]);
            
    return 0;
}
           

Example 11:Selection Sort Program in C

#include<stdio.h>
int main(){
              
    int i, j, count, temp, number[25];
            
    printf("Total Enter?: ");
    scanf("%d",&count);
             
    printf("Enter %d elements: ", count);
    // elements stored in array
    for(i=0;i<count;i++)
    scanf("%d",&number[i]);
              
    // Logic of selection sort algorithm
    for(i=0;i<count;i++){
    for(j=i+1;j<count;j++){
    if(number[i]>number[j]){
    temp=number[i];
    number[i]=number[j];
    number[j]=temp;
    }
    }
    }
             
    printf("Sorted elements: ");
    for(i=0;i<count;i++)
    printf(" %d",number[i]);
             
    return 0;
}