Header Ads

35 - WAP to Search an Element in an Array of Integer, Float, Double in C

This program helps to Search an Element in an Array of Integer, Float, Double in C

Program Output:

/* Program to Search an Element in an Array of Integer, Float, Double.*/

#include <stdio.h>
#include <string.h>

void swap(void *value1, void *value2, int size);
//Function to read types of the entry 
void read(void *array, int *length, int *choice)
{
 int idx;
 for (idx = 0; idx < *length; idx++)
 {
  switch (*choice)
  {
   case 1: scanf("%d", &(((int*)array)[idx]));
    break;
   case 2: scanf("%f", &(((float*)array)[idx]));
    break;
   case 3: scanf("%lf", &(((double*)array)[idx]));
    break;
  }
 }
}

//Function to sort the elements of array
void sort(void *array, int *length, int *choice)
{
 int idx, jdx;
 for(idx = 0; idx < *length; idx++)
 {
  for (jdx = idx + 1; jdx < *length; jdx++)
  {
   switch (*choice)
   {
    case 1: //Checking the numbers to swap
     if (*((int*)array + idx) > *((int*)array + jdx))
     {
      //Calling a function to swap
      swap((int*)array + idx, (int*)array + jdx, sizeof (*((int*)array + idx)));
     }
     break;
    case 2: 
     if (*((float*)array + idx) > *((float*)array + jdx))
     {
      swap((float*)array + idx, (float*)array + jdx, sizeof (*((float*)array + idx)));
     }
     break;
    case 3: 
     if (*((double*)array + idx) > *((double*)array + jdx))
     {
      swap((double*)array + idx, (double*)array + jdx, sizeof (*((double*)array + idx)));
     }
     break;
   }
  }
 }
}

//Function to swap
void swap(void *value1, void *value2, int size)
{
 char temp;
 int idx;
 //Loop to swap one byte at a time
 for (idx = 0; idx < size; idx++)
 {
  temp = *((char*)value1 + idx);
  *((char*)value1 + idx) = *((char*)value2 + idx);
  *((char*)value2 + idx) = temp;
 }
}

//Function to print the elements of array
void print(void *array, int *length, int *choice)
{
 int idx;
 for (idx = 0; idx < *length; idx++)
 {
  switch (*choice)
  {
   case 1: printf("%d ", ((int*)array)[idx]);
    break;
   case 2: printf("%.2f ", ((float*)array)[idx]);
    break;
   case 3: printf("%.4lf ", ((double*)array)[idx]);
    break;

  }
 }
 printf("\n");

}

//main function 
int main()
{
 //Declaring variables
 int choice, length, num, idx, count = 0;
 void *array = malloc(100);
 char ch;


 while (1)
 {
  printf("Enter Max No. of Elements in Array : ");
  scanf("%d", &length);
  printf("Select a type of Element\n\t1.Integer\n\t2.Float\n\t3.Double\n");
  printf("Your Choice : ");
  scanf("%d", &choice);

  switch (choice)
  {
   case 1://Case for integer
    printf("Enter '%d' Numbers in Array (with Spaces)\n", length);
    //Reading array elements
    read(array, &length, &choice);
    //Calling function to sort
    sort(array, &length, &choice);
    //Printing Sorted array
    printf("The sorted Array : ");
    print(array, &length, &choice);
    //find a number func
    printf ("Enter the Element to Search : ");
    scanf ("%d",&num);
    for (idx = 0; idx < length; idx++)
    {
     if (*((int *)array + idx) == num)
     {
      count++;
     }
    }
    if (count > 0)
    {
     printf ("Element Found!\n");
    }
    else
    {
     printf("Element Not Found!\n");
    }
    break;

   case 2://Case for float
    printf("Enter the Numbers : ");
    //Reading array elements
    read(array, &length, &choice);
    //Calling function to sort
    sort(array, &length, &choice);
    //Printing Sorted array
    printf("The sorted array : ");
    print(array, &length, &choice);
    //find a number func
    printf ("Enter the Element to Search :  ");
    scanf ("\n%d",&num);
    for (idx = 0; idx < length; idx++)
    {
     if (*((float *)array + idx) == num)
     {
      count++;
     }
    }
    if (count > 0)
    {
     printf ("Element Found!\n");
    }
    else
    {
    printf ("Element Not Found!\n");
    }
    break;

   case 3:
    printf("Enter the Numbers : ");
    //Reading array elements
    read(array, &length, &choice);
    //Calling function to sort
    sort(array, &length, &choice);
    //Printing Sorted array
    printf("The Sorted array : ");
    print(array, &length, &choice);
    //find a number func
    printf ("Enter the Element to be Searched : ");
    scanf ("%lf",&num);
    for (idx = 0; idx < length; idx++)
    {
     if (*((double *)array + idx) == num)
     {
      count++;
     }
    }
    if (count > 0)
    {
     printf ("Element Found!\n");
    }
    else
    {
    printf ("Element Not Found!\n");
    }
    break;
  }
  printf("To Continue press ['Y'/'n'] : \n");
  getchar();
  ch = getchar();
  if (!(ch == 'y' || ch == 'Y'))
  {
   break;
  }
 }
// cleanup memory
 free(array);
 return 0;
}

Program Output:

 ...

No comments