Sorting arrays

This is a discussion on Sorting arrays within the C++ Programming forums, part of the General Programming Boards category; Hello, I need help. How would I go about doing this: Getting character string from user - store into an ...

  1. #1
    Unregistered
    Guest

    Question Sorting arrays

    Hello, I need help. How would I go about doing this:
    Getting character string from user - store into an array..user then inputs order that the character string to be printed out to screen. Any ideas?
    For example:
    user puts in: e l l o H
    user then puts in: 2 3 4 5 1
    Outputs to screen: Hello

    > i think it requires two arrays and a recursive sort dont know to go about it an example would be great<

    thanks a lot

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    This would be a crude answer I know. but you could;
    Read the string into an array 'mytext[]', then read the users number input as a string, tokenise it with spaces or comma's.
    2,3,1,5,4. use atoi, to turn them into ints.
    Giving you
    a=2
    b=3
    c=1
    d=5
    e=4
    etc.
    Then just print from your array with something like
    cout << mytext[a] << mytext[b] etc

    I imagine this would work, but I'm sure there's something far more dynamic that could be done (I'm just another newb, and I don't write good answers when I'm away from my compiler)
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    8

    oleHl - Hello

    This should solve the problem of a word of any length.
    #include <iostream>
    #include <stdlib.h>
    using namespace std;

    void main() {
    char word[10];
    int numbers[10];
    int i, length, a=1, b;

    cout << "Enter a word: "; // oleHl - which gives "Hello"
    cin >> word;

    // Let's get the number of letters in our word:
    length = strlen(word); // 5 letter
    //Now, enter the order of letters- 5,4,2,1,3

    cout << "Enter the order of " << length << " letters: ";

    for(i=0; i<length; i++)
    cin >> numbers[i];

    for(b=0; b< length; b++)
    for(i=0; i<length; i++)
    {
    if(numbers[i] == length-(length-a)) {
    cout << word[i];
    a++; }
    else
    continue;
    }
    cout << endl;
    }

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    8

    oleHl - Hello

    This should solve the problem of a word of any length.
    #include <iostream>
    #include <stdlib.h>
    using namespace std;

    void main() {
    char word[10];
    int numbers[10];
    int i, length, a=1, b;

    cout << "Enter a word: "; // oleHl - which gives "Hello"
    cin >> word;

    // Let's get the number of letters in our word:
    length = strlen(word); // 5 letter
    //Now, enter the order of letters- 5,4,2,1,3

    cout << "Enter the order of " << length << " letters: ";

    // we take each number at a time after hitting the RETURN key
    for(i=0; i<length; i++)
    cin >> numbers[i];

    for(b=0; b< length; b++)
    for(i=0; i<length; i++)
    {
    if(numbers[i] == length-(length-a)) {
    cout << word[i];
    a++; }
    else
    continue;
    }
    cout << endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Sorting Arrays
    By DaniiChris in forum C Programming
    Replies: 11
    Last Post: 08-03-2008, 08:23 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM

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