Thread: class Fraction Operator

  1. #16
    Registered User
    Join Date
    Jul 2011
    Posts
    9
    Thank you everybody, I got it to work. finally.

  2. #17
    Registered User
    Join Date
    Jul 2011
    Posts
    9

    Question. by Peter.

    Hello, I have a question guys, I dont know why my teacher asked me this , but i want to make sure is right, would you be able to help me. ? thank you , I really appreciated.

    heres my code
    Code:
    const int numLetter = 26;
     
    struct letterFrequency
       {
        int freq;
        char letter;
       };
     
    int indexOfLargest(letterFrequency freqArray[], int startingPoint);
     
    void countLetters(letterFrequency freqArray[]);
    void initFreqArray(letterFrequency freqArray[]);    // arrays.
    void printFreqTable(letterFrequency freqArray[]);
    void sortFreqArray(letterFrequency freqArray[]);
     
     
     
    int main()
      {
        letterFrequency freqArray[numLetter];
     
        initFreqArray(freqArray);
        countLetters(freqArray);
        sortFreqArray(freqArray);
        printFreqTable(freqArray);
     
     
     
     
      }
     
    void initFreqArray(letterFrequency freqArray[])
     
        {
          for (int count = 0; count < numLetter; count++)
            {
               freqArray[count].freq = 0;
               freqArray[count].letter = 'a' + count;
            }
        }
     
    void countLetters(letterFrequency freqArray[])
       {
          char ch;
     
          cout << "Enter a sequence of characters (end with a period '.'): ";
          cin.get(ch);
     
    while (ch != '.')
       {
            if (isalpha(ch))
              {
                freqArray[tolower(ch) - 'a'].freq++;
              }
                cin.get(ch);
              }
       }
     
    void sortFreqArray(letterFrequency freqArray[])
      {
        for (int count = 0; count < numLetter - 1; count++)
           {
                 swap(freqArray[indexOfLargest(freqArray, count)],
                 freqArray[count]);
           }
      }
     
    int indexOfLargest(letterFrequency freqArray[], int startingPoint)
      {
        int indexOfLargest = startingPoint;
           for (int count = startingPoint + 1; count < numLetter; count++)
              {
                if (freqArray[count].freq > freqArray[indexOfLargest].freq)
                   {
                      indexOfLargest = count;
                   }
              }
        return indexOfLargest;
      }
     
    void printFreqTable(letterFrequency freqArray[])
    {
        int count = 0;
     
        cout << "Letter:" << setw(4) << "" << "Number of Occurrences" << endl;
     
    while (freqArray[count].freq != 0 && count < numLetter)
      {
        cout << setw(4) << freqArray[count].letter << setw(16)
             << freqArray[count].freq << endl;
                count++;
      }
     
    system ("PAUSE");
     
    }
    and here is the question :
    
    
    (1) Could you explain what is happening in the source code below if someone types cAt in The hat. xyz
     
    while (ch != '.')
    {
    if (isalpha(ch))
    {
    freqArray[tolower(ch) - 'a'].freq++;
    }
    cin.get(ch);
    }
    }
    Please be specific and let me know what is occurring in the freqArray struct.

    and

    Could you explain why you used the following condition in the while loop?

    while (freqArray[count].freq != 0 && count < numLetter)

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. So be the computer. You've got the code. Run through it line by line; when you need input you know what the user will type in.

    2. Why did you use that condition in your while loop?

    3. What does this have to do with fractions?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 03-26-2008, 09:01 AM
  2. Replies: 7
    Last Post: 11-10-2007, 05:17 AM
  3. Help with methods of a fraction class
    By NebulousMenace in forum C++ Programming
    Replies: 10
    Last Post: 04-02-2003, 12:36 AM
  4. Help with Fraction class
    By cheeisme123 in forum C++ Programming
    Replies: 0
    Last Post: 06-04-2002, 07:48 AM