Question

Please help with my C++ homework! 1. The following function is supposed to perform binary search....

Please help with my C++ homework!

1.

The following function is supposed to perform binary search. It has no errors and will execute correctly.

int binarySearch(int array[], int size, int value)
{
   int first = 0,             // First array element
       last = size - 1,       // Last array element
       middle,                // Mid point of search
       position = -1;         // Position of search value
   bool found = false;        // Flag

   middle = (first + last) / 2;     

   while (!found && first <= last)
   {
    

if (array[middle] == value)      
      {
         found = true;
         position = middle;
      }
      else if (array[middle] > value)  
         last = middle - 1;
      else
         first = middle + 1;           
   }
   return position;
}

True/False

13.

If you attempt to open a non-existing file for output

a.It is automatically created if it’s a text file.

b.It is automatically created for both text and binary file.

c.It is automatically created if it’s a binary file.

4.It is automatically created bt only if the file is a text fileThe open fails.

19. If fobj is a object of type fstream, then the following statement

fobj.seekp(32L, ios::beg);

a.sets the write position to the 32rd byte (byte 31) from the beginning of the file

b.You cannot tell from the information given

c.It works differently with different operating systems

d.sets the write position to the 33rd byte (byte 32) from the beginning of the file

20.

If fobj is an object of type fstream, then the following statement

fobj.seekp(120L, ios::curr)

a.sets the write pos to the 121st byte (byte 120) from the current position

b.You cannot tell from the information given

c.It works differently with different operating systems

d.sets the write pos to the 119st byte (byte 118) from the current position

21.

Consider the following code:

int main()

{

      fstream fout ("data.dat", ios::out|ios::binary);

    char c = 'A';

    while (c < 'K')

    {

            fout.write(&c, sizeof(c));

            c++;

    }

    fout.close();

    return 0;

}

After this program is executed, the file “data.dat” will contain the following:

a.Abcdefghij

b.AAAAAAAAAA

c. Cccccccccc

d.ABCDEFGHIJ

22.

Consider the code bellow. The file “letters.dat” contains ABCDEFGHIJ

int main()

{

      fstream fin("letters.dat", ios::in|ios::binary);

      char a;

      fin.get(a);

    cout << a;

      fin.seekg(3, ios::beg);

      fin.get(a);

      cout << a ;

      fin.seekg(-4, ios::end);

     fin.get(a);

     cout << a;

     seekg(2, ios::cur);

     fin.get(a);

     cout << a << endl;

     return 0;

    }

What will be the output?

a.ACEJ

b.Aaaa

c.DEFG

d.ABCD

e.ADGJ

0 0
Add a comment Improve this question Transcribed image text
Answer #1
12.
False

13.
b.It is automatically created for both text and binary file.

19.
a.sets the write position to the 32rd byte (byte 31) from the beginning of the file

20.
a.sets the write pos to the 121st byte (byte 120) from the current position

21.
d.ABCDEFGHIJ

22.
e.ADGJ


Add a comment
Know the answer?
Add Answer to:
Please help with my C++ homework! 1. The following function is supposed to perform binary search....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code in C++: Please help me fix the error in function: void write_account(); //function to write...

    Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • Write a C++ program that uses a structure to store the following inventory information in a...

    Write a C++ program that uses a structure to store the following inventory information in a file: ⦁   Item description, Quantity on hand, Wholesale cost, Retail cost, and Date added to inventory. ⦁   Use a char array for item description and date. ⦁   The program should have a menu that allows the user to perform the following tasks: i.   Add a new record at the end of the file. ii.   Display any record in the file. iii.   Change any record...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <alg...

    graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <algorithm> using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes //Execution Begins Here! int main(int argc, char** argv) { int n, i, arr[50], search, first, last, middle,count=0,count_in,tot; clock_t start, end; float duration; cout<<"Enter total number of elements :"; cin>>n; cout<<"Enter numbers"; for (i=0; i<n;i++) cin>>arr[i]; cout<<"Enter a...

  • Posted this a few hours ago. Can anyone help with this? IN C++ Perform the following:...

    Posted this a few hours ago. Can anyone help with this? IN C++ Perform the following: Read 10 bowlers with 3 scores each into 1 string array and 1 numeric array(2 dimension double array) Sort the data by individual bowlers averages, Don't forget to sort their names also. Calculate the average across and store in your existing array. Calculate the average down and store in your existing array. Print out the contents of both arrays. This will print the averages...

  • In c++ please How do I get my printVector function to actually print the vector out?...

    In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){    ifile.open(filename.c_str(), ios::in);    if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); }    } void...

  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

  • Write in C++: Please create a shopping list using the STL list container, then follow the...

    Write in C++: Please create a shopping list using the STL list container, then follow the following instructions. Create an empty list. Append the items, "eggs," "milk," "sugar," "chocolate," and "flour" to the list. Print the list. Remove the first element from the list. Print the list. Insert the item, "coffee" at the beginning of the list. Print the list. Find the item, "sugar" and replace it with "honey." Print the list. Insert the item, "baking powder" before "milk" in...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT