Showing posts with label bsearch program in c. Show all posts
Showing posts with label bsearch program in c. Show all posts

Tuesday, November 24, 2020

, , ,

Use of Range Function In Python With Program

 python range function in python return the sequence of numbers between the start integer value to the stop integer.

Syntax : -

range(stop)
range(start, stop[, step])

Parameters For range() function in python:-

Range function takes the three arguments which is given below :-

  •  first one is a start point which define the starting point of range function.
  • second one is stop point integer which help to tell the end point for the sequence.
  • Step is a third one and it use to define the increment for the sequence of number.

Return value from range function in python: -

  •  range() function return the immutable sequence of number.
  • In this function sequence of number starts form 0 to Stop-1.
  • if the step argument is then it raise an Value Error.

How to use the range function in python with program

# empty range
print(list(range(0)))

# using range(stop)
print(list(range(10)))

# using range(start, stop)
print(list(range(1, 10)))

Output:

range function program in python
range function program in python


Saturday, October 3, 2020

,

bsearch function program in c programming language

 bsearch function program in c language

bsearch function in c used to perform the search in the array and array will be sorted by the ascending order.

 A pointer will be used to sorted the array and the base point to the initial element for the given array and the key pointed to the object that containing the value that going to be sorted with the comparison function.

Return value for the bsearch program:-

If search goes successful, bsearch() returns a pointer to an identical element of the array. If two or more elements are equal, the element pointed to that it is not  specified.

If
search goes unsuccessful finding the key, bsearch() returns NULL.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
 
#define NOSTRING 5
#define SIZE 26
 
int string_compare(void const *, void const *);
 
int main(void)
{
    int i;
    char strings[NOSTRING][SIZE];
    char search_str[SIZE];
    char *status;
 
    /* read strings from the user */
    printf("User, write in %d character strings...\n", NOSTRING);
    for (i= 0; i < NOSTRINGS; i++)
        fgets(strings[i], SIZE, stdin);
 
    printf("\n\n");
 
    /* display array of unsorted strings */
    printf("Before Sorting:\n");
    for (i = 0; i < NOSTRING; i++)
        fputs(strings[i], stdout);
 
    /* sort the array */
    qsort(strings, NOSTRING, SIZE, string_compare);
    printf("\n");
 
    /* array of sorted strings */
    printf("After Sorting:\n");
    for (i = 0; i < NOSTRING; i++)
        printf(strings[i], stdout);
 
    printf("\n\n");
 
    /* read the string to be searched for */
    printf("programer write about string you wish to search for...\n");
    fgets(search_str, SIZE, stdin);
    printf("\n");
 
    /* calling bsearch() to locate given string in the array */
    status = bsearch(search_str, strings, NOSTRINGS, SIZE, 
                       string_compare);
 
    /* verify if given value is found */
    assert(status != NULL);
    printf("your desired string is found!\n");
 
    return 0;
}
 
int string_compare(void const *str1, void const *str2)
{
    return strcmp((char *)str1, (char *)str2);
}

OUTPUT : -

bsearch function program in c programming language
 bsearch function program in c programming language