How do you swap possisions when using an array?

I notice that its harder to compare strings in the array. Unlike numbers in an array, you can easily swap positions. For example,

int array[10];
int temp;

for(int x=0;x<=10,x++)
{if (array[x]<array[x+1])
{temp=array[x];
array[x]=array[x+1];
array[x+a]=temp;
}
}

Its easy. But if its an array of words, how do you compare it to the next index and how do you swap positions?

Thankz.

Comments

  • please use the [ code ] [ /code ] (without spaces) tags.

    your indenting is really bad. you could use mine.
    your solution is easy: use std::string instead of int:

    [code]
    std::string array[10];
    std::string temp;

    for(int x=0;x<=10,x++){
    if(array[x]<array[x+1]){
    temp=array[x];
    array[x]=array[x+1];
    array[x+1]=temp; //i guess you +a here was an error
    }
    }
    [/code]

    next thing: dont do the swap yourself:

    [code]
    std::string array[10];

    for(int x=0;x<=10,x++){
    if(array[x]<array[x+1]){
    std::swap(array[x],array[x+1]);
    }
    }
    [/code]

    and again: why write again what was written for you:

    [code]
    std::string array[10];

    std::sort(array,array+sizeof(array)/sizeof(array[0]));
    [/code]


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion