Please help ~ fix Codings

This is a discussion on Please help ~ fix Codings within the C++ Programming forums, part of the General Programming Boards category; Could ppl please fix my 2 codings to "template version". I spend so much time understanding template, but still hard ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    Please help ~ fix Codings

    Could ppl please fix my 2 codings to "template version". I spend so much time understanding template, but still hard for me!

    Please help~ thanks to all

    Code:
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    const int ARRAY_SIZE = 10;
    
    
    void search(const int a[], int lowEnd, int highEnd,
                               int key, bool& found, int& location);
    
    int main( )
    {
        int a[ARRAY_SIZE];
        const int finalIndex = ARRAY_SIZE - 1;
    
        int i;
        for (i = 0; i < ARRAY_SIZE; i++)
            a[i] = 3*i;
        cout << "Array conatins:\n";
        for (i = 0; i < ARRAY_SIZE; i++)
            cout << a[i] << " ";
        cout << endl;
    
        int key, location;
        bool found;
        cout << "Enter number to be located: ";
        cin >> key;
        search(a, 0, finalIndex, key, found, location);
    
        if (found)
            cout << key << " is in index location "
                 << location << endl;
        else
            cout << key << " is not in the array." << endl;
    
        return 0;
    }
    
    void search(const int a[], int lowEnd, int highEnd,
                               int key, bool& found, int& location)
    {
        int first = lowEnd;
        int last = highEnd;
        int mid;
    
        found = false;//so far
        while ( (first <= last) && !(found) )
        {
            mid = (first + last)/2;
            if (key == a[mid])
            {
                found = true;
                location = mid;
            }
            else if (key < a[mid])
            {
                last = mid - 1;
            }
            else if (key > a[mid])
            {
                first = mid + 1;
            }
        }
    }
    Code:
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    const int ARRAY_SIZE = 10;
    
    
    void search(const int a[], int first, int last,
                        int key, bool& found, int& location);
    
    int main( )
    {
        int a[ARRAY_SIZE];
        const int finalIndex = ARRAY_SIZE - 1;
    
        int i;
        for (i = 0; i < ARRAY_SIZE; i++)
            a[i] = 3*i;
        cout << "Array conatins:\n";
        for (i = 0; i < ARRAY_SIZE; i++)
            cout << a[i] << " ";
        cout << endl;
    
        int key, location;
        bool found;
        cout << "Enter number to be located: ";
        cin >> key;
        search(a, 0, finalIndex, key, found, location);
    
        if (found)
            cout << key << " is in index location "
                 << location << endl;
        else
            cout << key << " is not in the array." << endl;
    
        return 0;
    }
    
    void search(const int a[], int first, int last,
                              int key, bool& found, int& location)
    {
        int mid;
        if (first > last)
        {
            found = false;
        }
        else
        {
            mid = (first + last)/2;
    
            if (key == a[mid])
            {
                found = true;
                location = mid;
            }
            else if (key < a[mid])
            {
                search(a, first, mid - 1, key, found, location);
            }
            else if (key > a[mid])
            {
                search(a, mid + 1, last, key, found, location);
            }
        }
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    1. You didn't tell us what's wrong with your code.
    2. You never even used templates in your code.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    RE

    I had no problem with the coding in Recursive.

    I want to make my codings into template version, but I don't know how to do.

    So I am asking if ppl could change my recursive version to template version.

    Thanks

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    did you read the faq entry on templates? http://faq.cprogramming.com/cgi-bin/...&id=1044780608

    the only thing you really have to do is to add the template statement before each function and replace each occurrence of say 'int' with whatever you name your template type.

    make sure that whatever type you use during implementation is supported. that is, dont use a custom class you made and try and compare them with the template using operators like '>' '==', etc, without having overriding those operators to suit your class. (ie using the string class should be fine, as the operators are overridden and appropriate.)

    if this isnt clear, then tell us exactly what you do and dont understand or what isnt working when you try to create your templated functions.

    hope it helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. compiling my code, i can't fix error
    By howru in forum C Programming
    Replies: 2
    Last Post: 04-22-2008, 04:38 AM
  3. C++ code need help to fix
    By McReal in forum C++ Programming
    Replies: 9
    Last Post: 05-12-2007, 03:48 PM
  4. Help me fix my mess!!!!
    By Starr in forum C++ Programming
    Replies: 35
    Last Post: 02-01-2006, 03:40 PM
  5. please fix this programme
    By carsonng2000 in forum C++ Programming
    Replies: 10
    Last Post: 05-17-2005, 12:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21