Thursday, 28 February 2019

Displaying elements of an array in reverse order

 Problem : Displaying the elements of an array in reverse order
  Input :
          Array[5]={10,20,30,40,50};
  Output:
          50,40,30,20,10
/*
C program to display elements in reverse order: program will not change the order in memory.it will simply display in reverse order
*/
#include<stdio.h>
int main(){
    int array[10];
    int i;

    //Entering numbers into  array
    for(i=0; i<10; i++){
        printf("Enter a number\n");
        scanf("%d",&array[i]);
    }
   
    //Reading and Displaying numbers from  array in their original order
    printf("\nArray elements in their original order:\n");
    for(i=0;i<10;i++){
        printf("%d\t",array[i]);
    }

    printf("\nArray elements in reverse order:\n");
    //Reading and Dislaying numbers from  array in reverse order
    for(i=9;i>=0;i--){
        printf("%d\t",array[i]);
    }
    printf("\n");
    return 0;
}

Output:

Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Wednesday, 27 February 2019

Finding number of elements in an array

Problem: Given an array of integers ,find the number of elements in it.
Examples:
       Input : array[]={76,90,87,98,56,77,91}
       Output :
          No of Elements :7
         
//C program to find the number of elements in an array
#include<stdio.h>
    int main(){
       int array[]={76,90,87,98,56,77,91};
       int arraySize=0;

      /*key here is sizeof operator which return the size in byte: sizeof(array) 
       determines the size of array in bytes and sizeof(array[0]) determines of first 
       element in byte.*/

      //finding the size(no of elements) of array
       arraySize = sizeof(array)/ sizeof(array[0]);

       printf("Number of Elements in the array are %d.\n",arraySize);
  return 0;
}
Output:













Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Tuesday, 26 February 2019

C program to find minimum and maximum elements in an array

Problem : Given an array of integers ,find the smallest element in it.
Examples:
       Input : array[]={76,90,87,98,56,77,91}
       Output :
          Maximum : 98
          Minimum : 56
Solution:
   Minimum Element:
  1. We assume that first element of the array is the minimum one.
  2. Then iterate the array and compare each element with the assumed minimum element.
  3. If current element is minimum than assumed minimum element, we update the assumed minimum element to current element. This process continues till all elements are checked.

Maximum Element:
  1. We assume that first element of the array is the maximum one.
  2. Then iterate the array and compare each element with the assumed maximum element.
  3. If current element is larger than assumed largest element, we update the assumed maximum element to current element. This process continues till all elements are checked.


// C program to find the minimum and maximum elements in an array
#include<stdio.h>
    int main(){
       int array[]={76,90,87,98,56,77,91};
       int max,min,arraySize=0,i;
      //finding the size(no of elements) of array
       arraySize = sizeof(array)/ sizeof(int);
       max=array[0];  //assume that first element is the maximum one
       min=array[0];  //assume that first element is the minimum one
       for (i = 1; i < arraySize; i++){             
         if (array[i]>max){
            max=i;
         }
        if (array[i]<min){
            min=i;
         }            
       }
       printf("Maximum Element is %d at position %d.\n",array[max],max);
       printf("Minimum Element is %d at position %d.\n",array[min],min);
 return 0;
}
Output:

Please comment if you find anything incorrect, or you want to improve the topic discussed above. 

C program to find smallest element in an array

Problem : Given an array of integers ,find the smallest element in it.
Examples:
       Input : array[]={12,34,65,98,15,34}
       Output : 12
       Input : array[]={22,30,95,90,15,54}
       Output : 15
Solution:
  1. We assume that first element of the array is the smallest one.
  2. Then iterate the array and compare each element with the assumed smallest element.
  3. If current element is smaller than assumed smallest element, we update the assumed smallest element to current element. This process continues till all elements are checked.

// C program to find the smallest element in an array
#include<stdio.h>
int main(){
      int array[]={76,90,87,98,56,77,91};
       int min,arraySize=0;
      
       //function declaration
       int minElement(int array[],int n);
      
      //finding the size(no of elements) of array
       arraySize = sizeof(array)/ sizeof(int);
      
       //function calling
       min = minElement (array,arraySize);

       printf("Smallest Element is %d.\n", min);
            return 0;
}

//function definition
    int minElement(int array[],int n){
       int min,i;
       min =array[0];  //assume that first element is the smallest one
       for (i = 1; i < n; i++){                 
         if (array[i]< min){
            min=array[i];
         }          
       }
     return min;
      }
Output:
. 







                                                                                                                                                                                                

Please comment if you find anything incorrect, or you want to improve the topic discussed above. 

C program to find the largest element in an array

Problem : Given an array of integers ,find the largest element in it.
Examples:
       Input : array[]={12,34,65,98,15,34}
       Output : 98
       Input : array[]={22,30,95,90,12,54}
       Output : 95
Solution:
  1. We assume that first element of the array is the largest one.
  2. Then iterate the array and compare each element with the assumed largest element.
  3. If current element is larger than assumed largest element, we update the assumed largest element to current element. This process continues till all elements are checked.

// C program to find the largest element in an array
#include<stdio.h>
int main(){
      int array[]={76,90,87,98,56,77,91};
       int max,arraySize=0;
      
       //function declaration
       int maxElement(int array[],int n);
      
        //finding the size(no of elements) of array
       arraySize = sizeof(array)/ sizeof(int);
      
       //function calling
       max=maxElement(array,arraySize);

       printf("Maximum Element is %d.\n",max);
            return 0;
}

//function definition
      int maxElement(int array[],int n){
            int max,i;
            max=array[0];  //assume that first element is the largest one
       for (i = 1; i < n; i++){                 
         if (array[i]>max){
            max=array[i];
         }          
       }
               return max;
      }
Output:












Please comment if you find anything incorrect, or you want to improve the topic discussed above. 

Iterative Binary Search in C using function

Binary search algorithm is one of the most efficient searching algorithm. The precondition of binary search algorithm is that the set of elements e.g array must be ordered (sorted) in particular order.
Binary search begins by finding the middle element of the set and comparing it with the key value. If key value is smaller than middle element, it means all elements from middle element to last element of the set are greater than key value, so they must not be searched and should be discarded. So now the new set should comprise of elements from first to element just before middle element. If key value is greater than middle element, it means all elements from first element to middle element of the set are smaller than key value, so they must not be searched and should be discarded. So now new set should comprise of elements from first element after middle element to last element. If key value is equal to middle element, then index of middle element is returned.
//Iterative binary search in C using function
#include <stdio.h>
int main()
{
    int array[] = {9,10,12,15,32,50};
    int key = 17,arraySize=0,i,result=-1;
    //function declaration
    int binarySearch(int array[],int n,int value);
    //finding the size(no of elements) of array
    arraySize = sizeof(array)/ sizeof(int);
    //function calling
       result=binarySearch(array,arraySize,key);
      
       if(result!=-1)
         printf("Element %d found at position %d.\n",key,result+1);
       else
         printf("Element %d does not exist.\n",key);

    return 0;
}

//function definition
      int binarySearch(int array[],int n,int value){
            int index=-1,i,first,last,mid;
           
    first=0;
    last=n-1;
       while(first<=last)
    {
        mid=(first+last)/2;//index of middle element
        if(value<array[mid])
        {
            last=mid-1;
        }
        else if(value>array[mid])
            first=mid+1;
        else{
            index=mid;
            break;
        }
Output: key=10;
Output: key=32;

Please comment if you find anything incorrect, or you want to improve the topic discussed above. 

Monday, 25 February 2019

Iterative Binary Search in C without using function

Binary search algorithm is one of the most efficient searching algorithm. The precondition of binary search algorithm is that the set of elements e.g array must be ordered (sorted) in particular order.

Binary search begins by finding the middle element of the set and comparing it with the key value. If key value is smaller than middle element, it means all elements from middle element to last element of the set are greater than key value, so they must not be searched and should be discarded. So now the new set should comprise of elements from first to element just before middle element. If key value is greater than middle element, it means all elements from first element to middle element of the set are smaller than key value, so they must not be searched and should be discarded. So now new set should comprise of elements from first element after middle element to last element. If key value is equal to middle element, then index of middle element is returned.

//Iterative binary search in C without using function
#include <stdio.h>
int main()
{
    int array[] = {9,10,12,15,32,50};
    int key = 17,arraySize=0,i,index=-1,first,last,mid;

    //finding the size(no of elements) of array
    arraySize = sizeof(array)/ sizeof(int);

    first=0;
    last=arraySize-1;
    while(first<=last)
    {
        mid=(first+last)/2; //index of middle element
        if(key<array[mid])
        {
            last=mid-1;
        }
        else if(key>array[mid])
            first=mid+1;
        else{
            index=mid;
            break;
        }
           
    }

    if(index!=-1)
        printf("Element %d found at position %d.\n",key,index+1);
    else
        printf("Element %d not exist in given set of elements.\n",key);

    return 0;
}
Output: key=15;
Output: key=10;
Output: key=17;

Please comment if you find anything incorrect, or you want to improve the topic discussed above. 

Thursday, 21 February 2019

Linear Search using function in C

Linear Search

  1. Traverse given array starting from first element.
  2. Compare element at current index with given key.
  3. If key matches with an element, return the index.
  4. If key doesn’t match with any of elements, return -1.
//C program for linear search using function
#include <stdio.h>
     int main(void)
      {
       int array[] = { 10, 15, 9, 12, 50,32 };
       int key = 90,arraySize=0,i,result;
       //function declaration
       int linearSearch(int array[],int n,int value);
      //finding the size(no of elements) of array
       arraySize = sizeof(array)/ sizeof(int);
       //function calling
       result=linearSearch(array,arraySize,key);
       if(result!=-1)
         printf("Element %d found at position %d.\n",key,result+1);
       else
         printf("Element %d does not exist.\n",key);
               return 0;
      }   
      //function definition
      int linearSearch(int array[],int n,int value){
      int index=-1,i;
       for (i = 0; i < n; i++){                 
         if (array[i] == value){
            index=i;  //key found at index i
            break;
         }          
       }
               return index;

      }



Please comment if you find anything incorrect, or you want to improve the topic discussed above.