Skip to content

MyUstaadG

Learn to Serve

  • Computer Science
    • Software
    • Computer Tips
    • Coding
      • Conditional Structures Exercises
      • Loops Exercises
      • Arrays Exercises
      • Python Exercises
    • Microsoft Office
      • MS Excel
      • MS Word
  • Videos
    • Funny Videos
    • Health and Fitness Videos
    • PUBG Videos
    • Technology Videos
  • News
  • Funny
  • Memes
  • Playwright Tutorials
  • Tutorials
  • Toggle search form

Write a program that initializes data into a one-dimensional array. Search value using binary search.

Posted on October 26, 2021 By myustaadg.com
Spread the love

Write a program that initializes data into one-dimensional array. The data must be in ascending order. It inputs a value from the user and searches this value in the array using binary search method.

So let’s Write Code:

Introduction to C++ Programming | Download and Install Dev C++ Compiler

Code:

Write a program that initializes data into one-dimensional array. Search value using binary search.

#include"iostream" 
using namespace std;
main()
{
int abc[10] = {1,3,9,15,78,87,95,103,124,352};
int loc, num, i, M, S = 0, E = 9;
loc = 0;
cout<<"Enter any value ? ";
cin>>num;
while(S<=E)
{
M = (S + E)/2; 
if(num == abc[M])
{
loc = M+1;
break;
}
else if(num < abc[M])
E = M - 1;
else
S = M + 1;
}
if(loc == 0)
cout<<"Value not found "<<endl;
else
cout<<"Value found at position = "<<loc<<endl;
}

Hope You will learn Something. Happy Coding.

Visit my YouTube Channel as Well๐Ÿ˜‡ https://www.youtube.com/watch?v=JuIHQ9cLSEw&list=PLbIhkHxfUIItTdcyCb34uRIrPJbXBndIl&index=4

Click on the below Links for more Programming Exercises:

Conditional Structure Exercises: ๐Ÿ‘‰https://myustaadg.com/category/programming-exercises/conditional-structures-exercises/

Array Exercises: ๐Ÿ‘‰https://myustaadg.com/category/programming-exercises/arrays-exercises/

Arrays Exercises, Programming Exercises Tags:array declaration in c++, Array Exercises, Array exercises in C++, array in Object oriented programming, array in OOP, array in programming, Arrays, arrays arrays in c++, arrays codes, Arrays exercises, Arrays in C++, Arrays in C++ Programming with examples, Arrays in Programming, Arrays with example, Binary Search, Binary Search Algorithm with C++ Code and algorithum, Binary Search in C++ Programming, C++ Arrays, c++ arrays declaration and initialization, C++ Arrays with examples, C++ One Dimensional Array, C++ Program for Binary Search, C++ Program Linear Search in Array, C++ Programs For Binary Search, Creating and Using Arrays, how do you initialize an array in c++, Implementing binary search of an array, Searching in C++: Sequential Searching and Binary Searching, Simple arrays code, Working With Arrays In C++

Post navigation

Previous Post: Write a program to enter data into one-dimensional array. Find out the maximum value and its location.
Next Post: Write a program that inputs temperatures for seven days of the week into a one-dimensional array โ€œtempโ€.

More Related Articles

Write a program that stores the string in a variable and then displays the contents of the variable on the screen in python | Python Exercises Programming Exercises
Write a program that inputs data into a table and displays it on the screen in tabular form. Arrays Exercises
Write a program that inputs values into a one-dimensional array of ten elements. Draws a bar chart or histogram. Arrays Exercises
Write a program that initializes the values in the one-dimensional array and computes the sum of even values. Arrays Exercises
Write a program using two arrays and enter values in 1st array. The program should copy the values of the 1st array to 2nd array. Arrays Exercises
Write a program to initialize values in two tables A and B. Multiply matrix A and B and store results into table C | Multiplication of matrices. Arrays Exercises

Copyright © 2022 MyUstaadG.

Powered by PressBook Blog WordPress theme