Help with some C++ assignment. Easy for experts

Discussion in 'Programming' started by z80039, Apr 27, 2010.

  1. #1
    Hi ,
    I have an Assignment and I am not being able to do it by myself.

    1) Write a program that obtains the execution time of Bubble sort, Selection sort, Insertion sort, and Quick sort for input size of 500,000, 1,000,000, 1,500,000, 2,000,000, 2,500,000, and 3,000,000. Print the results in a table.


    2) Recursive Binary search , Write and implement a recursive version of the binary search algorithm and write a program to test it.
    I have written this but I don`t know what the error is :
    #include <iostream>
    using namespace std;
    int binarySearch(const int list[], int length, const int & item);
    int i=0;
    int main()
    {
    int arr[10],size=10; 
    int x;
    cout<<"Please enter the desired number"<<endl;
    cin>>x;
    cout<<"Please enter a sorted list of 10 numbers"<<endl;
    while(i<10)
    {
    cin>>arr[i];
    i++;
    }
    if(binarySearch(arr,size,x)==-1)
    cout<<"Number is not in the list"<<endl;
    else
    cout<<"The index of the desired number is "<<binarySearch(arr,size,x)<<endl;
    return 0;
    }
     
     
    int binarySearch(const int list[],int length,const int & item)
    {
    int first=0,last=length-1;
    int mid;
    bool found=false;
    while (first<=last&&!found)
    {
    mid =(first+last)/2;
    if (list[mid]==item)
    found=true;
    else if (list[mid]>item)
    last=mid-1;
    else
    first=mid+1;
    }
    if (found)
    return mid;
    else if(first>last)
    {
    return -1;
    }
    else
    return binarySearch(list,length,item);
    }
    Code (markup):
    Thank you for any help provided.
     
    z80039, Apr 27, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,846
    Likes Received:
    4,542
    Best Answers:
    123
    Trophy Points:
    665
    #2
    The point of assignments is to learn and become expert yourself.
     
    sarahk, Apr 28, 2010 IP
  3. codytaylor

    codytaylor Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I could do that fairly easily for you but I have to agree with sarahk. Why take the class and not learn the language?
     
    codytaylor, Apr 30, 2010 IP
  4. Dark3n

    Dark3n Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Looks complicated, i will try to figure it out.
     
    Dark3n, Apr 30, 2010 IP
  5. Polizel

    Polizel Peon

    Messages:
    93
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hey you have a missing .h extension at the very top of your program <iostream.h>
     
    Polizel, May 22, 2010 IP
  6. YesMoreTraffic

    YesMoreTraffic Well-Known Member

    Messages:
    278
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #6
    It doesn't matter now - .h issue
    the new namespace std standard doesn't need a .h extension to iostream and other header files.

    And like others have pointed out, its better if you do your assignment your self.
    Study, research and then write the code your self.

    A google search often helps :
    I came up with :
    http://www.daniweb.com/forums/thread19231.html
     
    YesMoreTraffic, May 23, 2010 IP