Performs a binary search instead of a linear search.

Assignment Help C/C++ Programming
Reference no: EM13165976

Modify the following program so it performs a binary search instead of a linear search. Use the selection sort algorithm to sort the array before the binary search is performed.

#include <iostream>
using namespace std;

// Function prototype
bool searchList(const long [], int, long);

// Constant for array size
const int SIZE = 10;

int main()
{
    // Array of numbers that are always played
    long ticket[SIZE] = {13579,26791,26792,33445,55555,
62483,77777,79422,85647,93121};
                         
    // This variable will hold the winning number.
    long winningNum;

    // Get this week's winning number.
    cout << "\nPlease enter this week's 5-digit "
        << "winning lottery number: ";
    cin >> winningNum;

    // Search for the winning number.
    if (searchList(ticket, SIZE, winningNum))
    {
        // If searchList returned true, then
        // the player has a winning ticket.
        cout << "You have a winning ticket.\n";
    }
    else
    {
        cout << "You did not win this week." << endl;
    }

    return 0;
}

// The searchList function determines if the number to
// search for is in the set of stored numbers using a
// linear search.

bool searchList(const long list[], int numElems, long value)
{
    bool found = false;

    for (int count = 0; count < numElems && !found; count++)
    {
        if (list[count] == value)
            found = true;
    }

    return found;
}

Reference no: EM13165976

Questions Cloud

State significant difference between bacteria and eukaryotes : There are significant differences between Bacteria and Eukaryotes in the manner in which genetic information becomes functional gene product
What is the predicted freezing point of solution : A skyscraper in Pittsburg, built in the early 1970's, is supported by water filled columns. Potassium carbonate was added to the water to precent freezing during cold weather. If the solution is 40.0% K2CO3 by mass, what is the predicted freezing ..
Culture and society has influenced eating disorders : Discuscribe the way culture and society has influenced eating disorders. Discuss why eating disorders are more prevalent in western civilization than in other cultures. Use specific examples to support your answer.
Describe an example of a base : Describe an example of a base and a derived class that shows base-class functions that are overridden in the derived class. Provide the syntax, showing the class definitions of both classes.
Performs a binary search instead of a linear search. : Modify the following program so it performs a binary search instead of a linear search. Use the selection sort algorithm to sort the array before the binary search is performed.
The calcium ion create a procedure for preparing solution : For the calcium ion create a procedure for preparing a 0.100 M solution from the solid, Anyhydrous Calcium Chloride, CaCl2. Make sure to include amounts and equipment you will use for the preparation.
Marketing and technology : Assess all of the issues discussed in Chapter 7 to determine the extent to which you believe the U.S. consumer is being manipulated by marketing and technology, as well as what steps should be taken (if any) to address possible manipulation. Provide ..
What is the vapor pressure of the stored mixture : The solvent for an organic reaction is prepared by mixing 60.0 mL of acetone with 45.0 mL of ethyl acetate. This mixture is stored at 25.0C. The vapor pressure and the densities for the two pure components at 25.0C. What is the vapor pressure of t..
Find out the amount of iron chloride hexahrdrate : The solution of iron (III) chloride used in this experiment is 0.02M. Determine the amount of iron (III) chloride hexahrdrate

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Write c program that prompts for variable number of integers

Write a C program that prompts for a variable number of integers, adds them up, averages them, and prints out the average.

  Create class integerset

Create class IntegerSet for which each object can hold integers in the range 0 through 100. Represent the set internally as a vector of bool values. Element a[i] is true if integer i is in the set. Element a[j] is false if integer j is not in the set..

  Two-dimensional array of double to hold the sales

Suppose you have a two-dimensional array of double to hold the sales amounts for all your salespeople three years. Assume you employ 50 salespeople and this array already holds valid data.

  Struct definition to represent the data of a person''s bank

Define a struct definition to represent the data of a person's bank account. There will be one string for the name, and two doubles for balance and interest rate. Declare two variables of this new type in the main function. Modify the values of each ..

  Write a function that takes an integer array

1. Write a function that takes an integer array and the array's size as parameters. The function will check if the array is sorted. If it is, it will return a 1. If it is not sorted it will return 0. The function should not  sort the array.

  Squares, triangles, circles and ellipses

write a program which will answer a pair of questions for squares, triangles, circles and ellipses. A square is defined by its lower left corner ( S L L x , S L L y ), and the length of one side  S W .

  Create a customer profile class.

a. Create a CustomerProfile class. Each CustomerProfile contains a last name, phone number, and bit fields indicating whether the customer:

  The main program should create an ifstream

For decryption, the main program should create an ifstream for the file to be decrypted. It should use the getline method of the ifstream to read lines from the file, call the encryption / decryption function with the line to be decrypted, and dis..

  To reverse the order of elements on a stack

Write a program in C++ to  reverse the order of elements on a stack S using two additional stacks using one additional stack

  Create an employee class

Create an employee class, including two data: an employee number (type int ) and the employee's compensation (in dollars; type float). The member data should comprise an int for storing the employee number and a float for storing the employee's co..

  Compares the number of possible sequences

A c++ program that compares the number of possible sequences of the length L that can be generated uinder the following assumptions. Given an alphabet of size N=9. The program must output a formatted list comparing the number of possible sequences..

  Wave steepness is the ratio of wave height

Wave steepness is the ratio of wave height (WH) to wave length(WL) and is an indicator of wave stability. When wave steepness exceeds a 1/7 ratio; the wave becomes unstable and begins to break. Assume a data file exits with the following header

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd