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

  1. #16
    Registered User
    Join Date
    Oct 2017
    Posts
    27
    Hey dude i tried to swap the alphabet to with strcpy and with a temp string(buffer) but the programm crushed always

  2. #17
    Registered User
    Join Date
    Oct 2017
    Posts
    27
    How can i change it the same time i change the INT ARRAY. I cant do it with a temp value.Also not with strcpy

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Isa-Elsino View Post
    Hey dude i tried to swap the alphabet to with strcpy and with a temp string(buffer) but the programm crushed always
    It's not a string swap. Try this:
    Code:
    char t;
    t = alpha[j];
    alpha[j] = alpha[j+1];
    alpha[j+1] = t;

  4. #19
    Registered User
    Join Date
    Oct 2017
    Posts
    27
    Omg thanks a lot dude. I m pretty sure that i tried this but anyway thanks a lot.it worked

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Glad to help you progress

  6. #21
    Banned
    Join Date
    Aug 2017
    Posts
    861
    who did this one in here? which can be taken and built upon to get the count of each letter used in a sentence then use that value held within an array to get the letter most used.
    in your comments it is stating one one value is needed. not a bubble sort to get them in order
    //Print out the character (singular, not plural or ordered) with the biggest frequency ( most times occurred )
    just replace the vowels with the entire alphabet use an array inside that switch to keep count of each letter, though that would make for a long switch.
    for the most part you need one array for each letter, you need another array to match that amount. they the array that is holding the letters, that element number needs to be the same as the matched_array element number to hold the value of occurred times for each letter. then in a loop it will spit out the letter and amount of times it occurred in the sentence. then somewhere in there you need to have it look at the amount of times each letter occurred then whence you find the greatest one then hold element number that matches the alphabet array, then match them up, alphabet[ element number ] = d matched_array [ same element number ]= 30.




    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    #define MAX_CHAR 256
    
    int get_vowels(char s[], int *a, int *e, int *i, int *o, int *u)
    {
        int vowels [5] = {'a','e','i','o','u'};
         int A = 0, E =0, I=0,O=0,U=0;
        int len, count = 0;
        int x = 0, b = 0;
        len = strlen(s);
      while (x < len)
      {
        
        b = 0;
        while(b < 5)
        {
              
            if( tolower( s[x]) == vowels[b])
            {             
                switch (tolower(s[x]))
                {
                    case 'a':
                        A++;
                        *a = A;
                        break;
                    case 'e':
                        E++;
                        *e = E;
                        break;
                    case 'i':
                        I++;
                        *i = I;
                        break;
                    case 'o':
                        O++;
                        *o = O;
                        break;
                    case 'u':
                        U++;
                        *u = U;
                        break;
                    default:
                            
                        break;
                    }
                    count++;
            }
            b++;
         }
        x++;
        }
        return count;
        
    }
     
     
    int main()
    {
        char sentence[MAX_CHAR];
        int a = 0,e=0,i=0,o=0,u=0;
        int count;
      
        printf("Enter a sentence \n");
        fgets(sentence, MAX_CHAR, stdin);
        count = get_vowels(sentence, &a, &e, &i, &o, &u);
        printf("a: %d e: %d i: %d o: %d u: %d\n", a,e,i,o,u);
        printf("Count : %d\n", count);
     
      return 0;
    }
    it is just an idea .. do not have to be attacked because I dropped completed code in here too.

  7. #22
    Banned
    Join Date
    Aug 2017
    Posts
    861
    done thanks to Isa-Elsino and his most excellent idea of using
    Code:
    hold_postion[0] = j;
    Code:
    userx@slackwhere:~/bin
    $ ./greatest_times_a_letter_is_counted
    Enter a sentence 
    hhhhhh ttttttt uuuuuuuuu eeeeeeeee
    letter is a was used 0 times
    letter is b was used 0 times
    letter is c was used 0 times
    letter is d was used 0 times
    letter is e was used 9 times
    letter is f was used 0 times
    letter is g was used 0 times
    letter is h was used 6 times
    letter is i was used 0 times
    letter is j was used 0 times
    letter is k was used 0 times
    letter is l was used 0 times
    letter is m was used 0 times
    letter is n was used 0 times
    letter is o was used 0 times
    letter is p was used 0 times
    letter is q was used 0 times
    letter is r was used 0 times
    letter is s was used 0 times
    letter is t was used 7 times
    letter is u was used 9 times
    letter is v was used 0 times
    letter is w was used 0 times
    letter is x was used 0 times
    letter is y was used 0 times
    letter is z was used 0 times
    letter e is most times counted, being  9 times
    the only draw back is if two or more letters are used the same amount of times. which requires more data types and stuff to track that.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    #define MAX_CHAR 256
    
    void get_vowels(char s[]) //, int *a, int *e, int *i, int *o, int *u)
    {
        //int vowels [5] = {'a','e','i','o','u'};
        int alpha[26] = {'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'};
        int alpha_count [ 26 ] = { 0 };
        int hold_postion [ 1 ] = { 0 }; // only needs to be one element it is only keeping one value, 
     // so is an array here really needed  for what you are using it for?
        
        int max = 0; // you where stuck on sorting that array which is not needed so this is what you need here
        
        
     //    int A = 0, E =0, I=0,O=0,U=0;
        int len; //, count = 0;
        int x = 0, b = 0;
        len = strlen(s);
        
    // your set up for your nested while loops was almost correct.
    // so I just used this to show you what you're doing wrong. 
       
      while (x < len)
      {
        
        b = 0;
        while(b < 26)
        {
              
            if( tolower( s[x]) == alpha[b])
            {             
                alpha_count[b]++;
            }
            b++;
         }
        x++;
        }
         
         for ( int i = 0 ; i < 26; i++)
            printf("letter is %c was used %d times\n",alpha[i], alpha_count[i] );
      
    // now you will have the total amount of letters max count for each letter in your
    // alpha_count array, that the element match up to your alpha array
    
    //now all you have to do is find the max ( greatest value out of all 26 elements)
    
        for (int j = 0; j < 26; j++)
            if ( alpha_count [ j ] > max )
                {
                   // two lines of code here needed to update
                   // two separate  things 
                  // the printf will give you a clue to one of them 
                  // because you were already doing that one anyhow
                  /// enjoy 
                }
            printf("letter %c is most times counted, being  %d times\n", alpha[hold_postion[0] ], alpha_count[hold_postion[0] ]);
                    
        
    }
     
     
    
    int main (void)
    {
    char sentence[MAX_CHAR];
        
        printf("Enter a sentence \n");
        fgets(sentence, MAX_CHAR, stdin);
        get_vowels(sentence); //, &a, &e, &i, &o, &u);
         
    
    return 0;
    }
    it looked like you were missing a bunch of your code because I could not get it running , so I just used this as your while loops were almost right, how you were getting your input was not present so I had to improvise and do as stated modify existing code .. reuse of code is a 21st century thing ya know
    Last edited by userxbw; 11-05-2017 at 04:55 PM.

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