Thread: 2 Different types of array and I wan them to sort together.Do I need a struct?HELP

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    27

    2 Different types of array and I wan them to sort together.Do I need a struct?HELP

    2 Different types of array and I wan them to sort together.Do I need a struct OR is an other way for doing it?HELP

  2. #2
    Registered User
    Join Date
    Feb 2014
    Posts
    30
    What types are the arrays?

    There's most likely another way.

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    27
    Well the one is INT ARRAY and the other is a char array

    Frequency of the character and the characters and i dont want to lose the connection of those two

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    30
    In your first post you ask how you can sort them. In your last post you ask how you can count the frequency of the letters.

    Does the int array contain integers that represent human readable chracters?

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    27
    I have already done the frequency of the characters .Yes it contains integers and the other array is the alphabet.I just want to do a bubble sort to THE INT ARRAY and change the CHAR ARRAY(ALPHABET) accordingly

  6. #6
    Registered User
    Join Date
    Feb 2014
    Posts
    30
    I would make a struct then.

    Code:
    struct _alphabet
    {
        int frequency;
        char letter;
    };
    Then you can just bubble sort based on the frequency without the relation losing meaning.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    I would just use
    Code:
    int frequency[128] = { 0 };
    There is a slot for every ASCII character, so you can do:
    frequency['A']++;
    except 'A' could also be a variable.

  8. #8
    Registered User
    Join Date
    Feb 2014
    Posts
    30
    Quote Originally Posted by whiteflags View Post
    I would just use
    Code:
    int frequency[128] = { 0 };
    There is a slot for every ASCII character, so you can do:
    frequency['A']++;
    except 'A' could also be a variable.
    I was thinking he also wants to have the results sorted, which stops you from using the above method, doesn't it? Because if you sort the frequency of your example, the relation to letters would be lost.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    That's a fair point. I guess it depends on what sorted means; if it should be ordered by increasing frequency, sorting the frequency array would lose the information. Otherwise, ASCII is sorted alphabetically.

  10. #10
    Registered User
    Join Date
    Oct 2017
    Posts
    27
    caN U show me an example I m stuck right now

  11. #11
    Registered User
    Join Date
    Feb 2014
    Posts
    30
    What have you tried?

    Instead of a hand out, what code do you have so far?

  12. #12
    Registered User
    Join Date
    Oct 2017
    Posts
    27
    Code:
     //Frequency of the characters
    
    
        int freq[26], holdposition[26];//array holding the frequency of the characters
    
    
        for(i=0; i<26; i++)
        {
            freq[i] = 0;
            holdposition[i] = 0;
        }
    
    
        char alpha[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        char str[WIDTH];
        j=0;
        int k;
    
    
        while( j < numwords){
            strcpy(str, wordpc[j]);
            //printf("%s", str);
            i=0;
            while (str[i] != '\0'){
                for( k=0; k<26; k++ ){
                    if (str[i] == alpha[k] ){
                        freq[k]++;
                    }
                }
                i++;
            }
            j++;
        }
    
    
       for (i = 0; i < 26; i++)
       {
          /** Printing only those characters
              whose count is at least 1 */
    
    
          if (freq[i] != 0)
             printf("\n%c occurs %d times in the entered string.\n", alpha[i],freq[i]);
       }
    
    
       //Print out the character with the biggest frequency
    
    
       /** Bubble Sort*/
       int swap;
       k=0;
       for (i = 0 ; i < ( 26 - 1 ); i++)
      {
        for (j = 0 ; j < 26 - i - 1; j++)
        {
          if (freq[j] < freq[j+1]){
            holdposition[k]= j;
            k++;
            swap = freq[j];
            freq[j] = freq[j+1];
            freq[j+1] = swap;
            //holdposition related to the alphabet
          }
          else{
              holdposition[k]= j;
              k++;
          }
        }
      }
    
    
      for ( i = 0 ; i < 26 ; i++ )
        printf("%d %c\n", freq[i], alpha[holdposition[i]]);
    So because unfortunately i havent study a lot of structures ,i couldnt do it so i tried this way which is tottaly wrong, anyway maybe you can show me some sites so i can do this .

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    One way to fix this would be, in the sort, where you swap one frequency element with another, to also swap one alpha element with another. Because they start off parallel to each other, if you always swap both arrays when you want to swap one, they will stay parallel. Also, you won't need holdposition[] if you do it this way.
    Last edited by whiteflags; 11-05-2017 at 12:54 PM.

  14. #14
    Registered User
    Join Date
    Feb 2014
    Posts
    30
    Quote Originally Posted by whiteflags View Post
    One way to fix this would be, in the sort, where you swap one frequency element with another, to also swap one alpha element with another. Because they start off parallel to each other, if you always swap both arrays when you want to swap one, they will stay parallel. Also, you won't need holdposition[] if you do it this way.
    I don't know why this idea escaped me.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    It only works because bubble sort is a stable sort.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use qsort() to sort array of pointers to struct
    By VIgnotam in forum C Programming
    Replies: 4
    Last Post: 04-04-2012, 06:42 PM
  2. Replies: 1
    Last Post: 07-30-2011, 12:49 PM
  3. Array of Struct sort
    By fukki in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2010, 06:08 AM
  4. Replies: 1
    Last Post: 01-26-2010, 09:02 AM
  5. Finding Struct Types
    By randomalias in forum C Programming
    Replies: 3
    Last Post: 05-17-2006, 08:18 PM

Tags for this Thread