Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that performs the following tasks ( you can use/modify the demo

ID: 3671032 • Letter: W

Question

Write a program that performs the following tasks (you can use/modify the demo codes in class notes):

In the main function, declare a float array: test

Initialize array elements with these values:   0.0, 1.1, -2.2, 3.3, -4.4, 5.5, 6.6, 7.7, -8.8, 9.9

Sort the array using the qsort method.

Print out the elements of sorted array.

Ask the user to enter a float value.

Search the user’s value in the array using the binary search method.

If a match is found, print out both the index and value of the matching element.

You can use fabs() and <match.h> library to compare two float numbers for equality.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>


int intcmp(const void *v1, const void *v2);
int main()
{
       float test[]={0.0, 1.1, -2.2, 3.3, -4.4, 5.5, 6.6, 7.7, -8.8, 9.9};
      int count;
      float key;
      float *ptr;

      /* Enter some integers from the user. */


      /* Sort the testay into ascending order. */

      qsort(test, 11, sizeof(test[0]), intcmp);

      /* Display the sorted testay. */

      for (count = 0; count < 11; count++)
          printf(" test[%d] = %.2f.", count, test[count]);

      /* Enter a search key. */
printf("Enter the key to search..... ");
scanf("%f",&key);
      /* Perform the search. */

      ptr = (float *)bsearch(&key, test, 11, sizeof(test[0]),intcmp);

      if ( ptr != NULL )
          printf("%f found at test[%d].", key, (ptr - test));
      else
          printf("%f not found.", key);
      return(0);
}

int intcmp(const void *v1, const void *v2)
{
     return (*(int *)v1 - *(int *)v2);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote