Homework Help Question & Answers

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 number to find :";
cin>>search;
first = 0;
last = n-1;
start = clock();
middle = (first+last)/2;
while (first <= last)
{
count++;
if(arr[middle] < search)
first = middle + 1;
else if(arr[middle] == search)
{
break;
}
else
last = middle - 1;
count_in++;
middle = (first + last)/2;
}
if(first > last)
cout<<"Not found! ";
tot = count_in+ count;
cout<<"Outer Operations = "<<count<<endl;
cout<<"i loop Operations = "<<count_in<<endl;
cout<<"Total Number of ops = "<<tot<<endl;
//time
// Calculate duration
duration = (float)(end - start)/ CLOCKS_PER_SEC;
cout<<"Execution time ="<<duration<<endl;
end = clock();
return 0;
}

0 0
Next > < Previous
ReportAnswer #1

Run time

Above graph is log n graph

For given n algorithm take log n time

So time complexity of algorithm is O(log n)

Know the answer?
Add Answer of:
graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <alg...
Your Answer: Your Name: What's your source?
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
  • #include<iostream> #include<string> #include<fstream> using namespace std; string mergeOneLine(int id,string last_name, string first_name, string phone_number, float quizzes[14],...

    #include<iostream> #include<string> #include<fstream> using namespace std; string mergeOneLine(int id,string last_name, string first_name, string phone_number, float quizzes[14], float homeworks[10], float labs[7], float tests[3], float project, float discussion, float teamWork, float extra, float sumScore = 0.0, float percentage = 0.0, char letterGrade = 'X') { //Merges all assignments and information into one string and returns it string oneLine; oneLine += to_string(id) + "," + last_name + "," + first_name + "," + phone_number + "-"; for (int i = 0; i <...

  • c++ help with pointers in place of arrays

    x.øi5 working program (well not working anymore) using arrays to add a record, delete etc. I need to now create the program only change the class files not the mainone and change the arrays into pointers. I started on it, but I'm not very comfortable with the use of pointers, Thanks.main files#include <iostream>#include "StudentDatabase.h"#include <string>#include <iomanip>using namespace std;int main(){StudentDatabase newDB;int choice;void displayMenu();string userNameDesired;int pos;displayMenu();do{cin >> choice;if (choice == 1) {string n;string ident;double gp;cout << "Please enter name of student: ";cin...

  • c++ PLEASE HELP its rather easy if you know what your doing.

    x.øi5class header//File car.h#ifndef STUDENTDATABASE_H#define STDUENTDATABASE_H#include <string>#include <iostream>using namespace std;// Student Database declarationclass StudentDatabase{private:int numberOfRecords;struct StudentRec{ string name;string ID;double GPA;};public:StudentRec *Record;int getnumRecords(){return numberOfRecords;}StudentDatabase();//string getRecord()//{//string userName = Record->name;//return userName;//}void addRecord(string, string, double);void removeRecord(string, string, double);void showAll();int getOneName(StudentRec[], int, string);StudentRec getStudentRecord(){StudentRec StudentRecords[100];return StudentRecords[100];}void sortArray (StudentRec[], int);};#endifclass implementation #include <string>#include "StudentDatabase.h"#include <iostream>#include <iomanip>using namespace std;StudentDatabase::StudentDatabase(){Record = new StudentRec[100];numberOfRecords = 0;}void StudentDatabase::addRecord(string n, string I, double G){StudentRec newstudent;newstudent.name = n;newstudent.ID = I;newstudent.GPA = G;Record[numberOfRecords] = newstudent;numberOfRecords++;}void StudentDatabase::showAll(){StudentRec AllStudents;for (int count = 0; count < numberOfRecords;...

  • Implement merge sort and merge #include <iostream> using namespace std; void * merge(int arr[], int start1, int end1, int start2, int end2){ int * combined = new int[end2-start1 + 1];         ...

    Implement merge sort and merge #include <iostream> using namespace std; void * merge(int arr[], int start1, int end1, int start2, int end2){ int * combined = new int[end2-start1 + 1];             } void mergeSort(int arr[], int start, int end){ //base case: down to 1 item, do nothing //recursive case: //MergeSort(left) //MergeSort(right) //Merge(left, right) int m = (end - start) / 2; if(start==end){ } else { mergeSort(arr, start, m); mergeSort(arr, m+1, end); merge(arr, start, m, m+1, end); } } void fill(int arr[],...

  • Need help with Arrays (C++)

    I need help changing my current program so that the user does notenter in how many number he/she will use and just starts to enterin the numbers. The program shouldhave an array which can hold upto 50 numbers (without the user specifying this in the beginning).*Note: this only happens if the user decides to enter in thenumbersfrom the keyboard, not from the data file.* Here is mycode:#include<iostream>#include<fstream>#include<iomanip>using namespace std;int arr_2d[50][2],count;void convert_2d(int *arr,int num){int min=0,i,j,temp;extern int count;count =0;for (i = 0;...

  • I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for.....

    I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for..while..do while,loops,pointer,address,continue,return,break. Create a C++ program which contain a function to ask user to enter user ID and password. The passwordmust contain at least 6 alphanumeric characters, 1 of the symbols [email protected],#,$,%,^,&,* and 1 capital letter.The maximum allowable password is 20. Save the information. Test the program by entering the User ID and password. The...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • Use this code to create multiple functions. #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() {...

    Use this code to create multiple functions. #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { cout << fixed << showpoint << setprecision(2); ofstream outFile; outFile.open("Feras's.txt"); outFile << "..Skinny Feras's Restaurant ..\n\n" << endl; int choise=10, quantity; float paid, SubTotal=0, Tax = .10, Total, RM, more; while(choise!=0) { system("cls"); cout << "\t**Welcome To Skinny Alsaif Restaurant Lol**" << endl; cout << "\nWhat would you like to have?" << endl; cout << "1. Burger." << endl; cout << "2. Pizza." <<...

  • I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using...

    I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using namespace std; int NumOfEmployees(); int TotDaysAbsent(int); double AverageAbsent(int, int); int main() {         cout << endl << "Calculate the average number of days a company's employees are absent." << endl << endl;      int numOfEmployees = NumOfEmployees();         TotDaysAbsent(numOfEmployees);    return 0; } int NumOfEmployees() {    int numOfEmployees = 0;     cout << "Please enter the number of employees in the company: ";         cin >> numOfEmployees;     while(numOfEmployees <= 0)     {            ...

Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
Share Your Knowledge

Post an Article
Post an Answer
Post a Question with Answer

Self-promotion: Authors have the chance of a link back to their own personal blogs or social media profile pages.