Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 118,871 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,781 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



function template using array need help

 
Reply to this topicStart new topic

function template using array need help, making a function template

shadowman187
post 3 May, 2008 - 06:28 PM
Post #1


New D.I.C Head

*
Joined: 1 May, 2008
Posts: 3

hopefully i put this in the right place i was not sure if it was in visual basics or in the c++ catergory. but if this is the wrong place let me know so i can start to post in the right place.okay im working on a sorting array. that has some numbers and correctly arranges them in order using an int array. i use a selection sort and a swap function to do this. my problem is i now have to make a function template called selection sort that does the same thing. i have read online about templates but still find them confusing. even though the idea of them seem easy. how do i make it into a function template selectionsort?this is my code:
CODE
#include<iostream>
#include<iomanip>
using namespace std;

void selectionSort(int*const,const int);
void swap(int*const,int*const);

void selectionSort(int*const array, const int size){
int smallest;
    //loop over size-1 elements
    for(int i=0;i<size-1;i++){
        smallest=i;
        //loop to find index of smallest element
    for(int index=i+1;index<size;index++)
        if(array[index]<array[smallest])
            smallest=index;
    swap(&array[i],&array[smallest]);
    }
}
int main( ){
    
    const int arraySize=10;
    int a[arraySize]={2,6,4,8,10,12,89,68,45,37};

    cout<<"Data items in orignal order\n";

    for(int i=0;i<arraySize;i++)
        cout<<setw(4)<<a[i];
    selectionSort(a,arraySize);

    cout<<"\nData items in ascending order\n";

    for (int j=0;j<arraySize;j++)
        cout<<setw(4)<<a[j];
    cout<<endl;
    
    return 0;
}

void swap(int*const element1Ptr,int*const element2Ptr){
    int hold=*element1Ptr;
    *element1Ptr=*element2Ptr;
    *element2Ptr=hold;
}


This post has been edited by shadowman187: 3 May, 2008 - 06:43 PM
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 3 May, 2008 - 06:42 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 4,669



Thanked 125 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Wow, you are in the completely wrong forum. hehe smile.gif
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 3 May, 2008 - 06:42 PM
Post #3


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,918



Thanked 83 times

Dream Kudos: 8100

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI, Boo.Net

My Contributions


This is posted in the wrong forum, this should be in the C & C++ Forum. Moved smile.gif
User is offlineProfile CardPM

Go to the top of the page

cooter1341
post 7 May, 2008 - 06:26 PM
Post #4


New D.I.C Head

*
Joined: 23 Apr, 2008
Posts: 6

Hey,
I am not very good at this either, but I was able to take your existing code and modify it enough to work. I was able to use some of the stuff off of another Array I had done before and place it in yours and I was able to make it compile. Take a look at it, I sure hope this helps you out, I am pretty much a beginner at this, but I have been messing around with the Arrays for a couple of my last projects. Well good luck and let us know if this works for you. smile.gif

CODE

#include<iostream>
#include<iomanip>
using namespace std;

void selectionSort(int*const,const int);
void swap(int*const,int*const);

void selectionSort(int*const array, const int size){
int smallest;
    //loop over size-1 elements
    for(int i=0;i<size-1;i++){
        smallest=i;
        //loop to find index of smallest element
    for(int index=i+1;index<size;index++)
        if(array[index]<array[smallest])
            smallest=index;
    swap(&array[i],&array[smallest]);
    }
}

#define SIZE 12
int main()
{
int b, c, d, e, f, g, h, j, k, l, m, p;

cout << "We will enter 12 positive numbers : ";

cout << "Enter the first number : ";
    cin >> b;
cout << "Enter the second number : ";
    cin >> c;
cout << "Enter the third number : ";
    cin >> d;
cout << "Enter the fourth number : ";
    cin >> e;
cout << "Enter the fifth number : ";
    cin >> f;
cout << "Enter the sixth number : ";
    cin >> g;
cout << "Enter the seventh number : ";
    cin >> h;
cout << "Enter the eighth number : ";
    cin >> j;
cout << "Enter the ninth number : ";
    cin >> k;
cout << "Enter the tenth number : ";
    cin >> l;
cout << "Enter the eleventh number : ";
    cin >> m;
cout << "Enter the twelvth number : ";
    cin >> p;

       int a[SIZE] = {b, c, d, e, f, g, h, j, k, l, m, p};

    cout<<"Numbers in the order you entered them\n";

    for(int i=0;i<SIZE;i++)
        cout<<setw(4)<<a[i];
    selectionSort(a,SIZE);

    cout<<"\nNumbers in ascending order\n";

    for (int j=0;j<SIZE;j++)
        cout<<setw(4)<<a[j];
    cout<<endl;
    
    return 0;
}

void swap(int*const element1Ptr,int*const element2Ptr){
    int hold=*element1Ptr;
    *element1Ptr=*element2Ptr;
    *element2Ptr=hold;
}


User is offlineProfile CardPM

Go to the top of the page

shadowman187
post 7 May, 2008 - 06:31 PM
Post #5


New D.I.C Head

*
Joined: 1 May, 2008
Posts: 3

well the main thing i want to do is make a Template
like this:
template<typename T>
and implement it into my selection sort. im having trouble making it into a template.
User is offlineProfile CardPM

Go to the top of the page

woodjom
post 19 Jul, 2008 - 07:41 PM
Post #6


New D.I.C Head

*
Joined: 8 May, 2008
Posts: 43



Thanked 1 times
My Contributions


QUOTE(shadowman187 @ 7 May, 2008 - 08:31 PM) *

well the main thing i want to do is make a Template
like this:
template<typename T>
and implement it into my selection sort. im having trouble making it into a template.


hey shadow....in case you are still having problems here is a suggestion.

CODE

typedef void SPELL_FUN    args( ( int sn, int level, CHAR_DATA *ch, void *vo,    int target ) );


this basically creates a template of the function input and associates it with the name SPELL_FUN

then when you want to use the function declare a variable with the name SPELL_FUN and it will automatically assume the value typedef'd above

as such in usage:
CODE

...
typedef void SPELL_FUN    args( ( int sn, int level, CHAR_DATA *ch, void *vo,    int target ) );  //declaration
...
SPELL_FUN *    spell_fun; //usage
...


would translate to:
CODE

...
void spell_fun (int sn, int level, CHAR_DATA *ch, void *vo, int target)
..


you may ask your self why not just do the bottom one instead of the top one. Think of as a template and cuts the code typing down and still makes the same reference.

as well it allows you to declare functions just like variables and of course with less typing.

let me know if this helps or hurts smile.gif
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 20 Jul, 2008 - 10:43 AM
Post #7


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,763



Thanked 37 times

Dream Kudos: 525
My Contributions


How to make a function template. Well, lets start with the swap since that is a classic and can be found within C++'s algorithm header (well I could not find it but it should be there somewhere).

Generally you will see this looking something like this:
cpp
template <class T>
void swap(T &first, T &second) {
T temp;
temp = first;
first = second;
second = temp;
}


So converting you current function over into template functions is not all that hard, you more or less will prefix them with "template <class T>" and then replace "int" with "T"... There may be a little more to it than that, but that is the basics.




User is offlineProfile CardPM

Go to the top of the page

woodjom
post 20 Jul, 2008 - 11:39 AM
Post #8


New D.I.C Head

*
Joined: 8 May, 2008
Posts: 43



Thanked 1 times
My Contributions


oops thought this was my posting in the vb.net thread smile.gif

Hey Nick,

Yeah i got that for CPP but i am looking for an equivalent feature in VB.net

This post has been edited by woodjom: 20 Jul, 2008 - 11:41 AM
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 20 Jul, 2008 - 11:50 AM
Post #9


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,763



Thanked 37 times

Dream Kudos: 525
My Contributions


I am betting that PsychoCoder beats me to answer this. And this should be a question should be in the VB.net forum (I am not a .net programmer).

However I believe that what you would be interested in is "Generics" in VB.net

This is not the same as templates (templates are MUCH cooler than generics).

In good old VB (non.net) you would use late binding to simulate the generic. You can use the same technique in VB.net using the Object datatype. However I *think* that VB.net has the ability to use Generics more formally than late binding.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/13/08 02:08AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month