Howdy, Stranger!

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

Categories

How do you swap possisions when using an array?

ryln22ryln22 Member Posts: 15
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

  • PeterTheMasterPeterTheMaster Member Posts: 636
    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]


  • Shawn CarterShawn Carter Member Posts: 0

    ____ // http://forcoder.org // free video tutorials and ebooks about \ Visual Basic PHP Visual Basic .NET MATLAB Go Delphi Python Objective-C Perl C# PL/SQL Swift JavaScript R C Java C++ Ruby Scratch Assembly Julia ML Lua Crystal Awk VBScript SAS Scheme Scala Rust Alice Fortran Bash Erlang Clojure Apex D Kotlin ABAP Hack F# Lisp Ada Prolog LabVIEW Transact-SQL COBOL Dart FoxPro Logo \ _

Sign In or Register to comment.