Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,172 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,920 people online right now. Registration is fast and FREE... Join Now!




Array difficulties.

 
Reply to this topicStart new topic

Array difficulties.

Jerichozero
13 Dec, 2007 - 05:09 PM
Post #1

New D.I.C Head
*

Joined: 13 Dec, 2007
Posts: 4


My Contributions
Hey, guys. I'm trying to write a program that takes a phrase input by the user, counts the number of times each letter shows up in the phrase, then displays this number to the screen beside the corresponding letter. If a letter does not show up in the phrase, then it will have no count on the screen (No zero's for letters not input, in other words.). This is what I have so far.

CODE
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

void sort(char x[], int n);
const int alphabet = 26;

int main(){
    char limit = '.', sentence[255];
    int length, letters, counters[alphabet] = {0};
    cout << "Please type in a sentence composed of all lowercase letters, \n";
    cout << "and include a period at the end: ";
     char next;
     int index = 0;
     cin.get(next);
     for (int i = 0; next != limit; i++) {
         cin.get(next);
         letters++;
         sentence[i] = next; }            
    
     for(int i = 0; sentence[i] > 0; i++) {
             counters[sentence[i]-'a']++; }
            
             sort(sentence, letters);        
            
     for(int j = 0; j < alphabet; j++) {
             cout << counters[j] << setw(4) << sentence[j] << endl; }
            
    system("pause");
    return 0;
}

void sort(char x[], int n) {
   for (int pass = 1; pass <= n; pass++) {  
       for (int i = 0; i < n-pass; i++)
       if (x[i] > x[i+1]) {
               int temp = x[i];
                x[i] = x[i+1];
                x[i+1] = temp; }}}


Unfortunately, while it seems to be counting the letters (And they show up in the left column where they should, were the corresponding letters in alphabetical orders on the right), I can't get the program to assign a letter to only one array position if the letter in question is repeated in the user's phrase. I apologize if I'm wording this oddly, but if anyone could point me in the right direction, I'd be very thankful.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Array Difficulties.
13 Dec, 2007 - 05:52 PM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
I don't quite understand what the sorting is about. In that last alphabet display, outputting sentence makes little sense. I think maybe you were aiming for this:
CODE

for(int i=0; i<alphabet; i++) {
    if (counters[i]>0) {
        cout << (char)(i+'a') << setw(4) << counters[i] << endl;
    }
}


Hope this helps.

PS. you win the most funky block style I've seen in a very long time. }}}}} blink.gif

User is offlineProfile CardPM
+Quote Post

Jerichozero
RE: Array Difficulties.
13 Dec, 2007 - 06:02 PM
Post #3

New D.I.C Head
*

Joined: 13 Dec, 2007
Posts: 4


My Contributions
QUOTE(baavgai @ 13 Dec, 2007 - 06:52 PM) *

I don't quite understand what the sorting is about. In that last alphabet display, outputting sentence makes little sense. I think maybe you were aiming for this:
CODE

for(int i=0; i<alphabet; i++) {
    if (counters[i]>0) {
        cout << (char)(i+'a') << setw(4) << counters[i] << endl;
    }
}


Hope this helps.

PS. you win the most funky block style I've seen in a very long time. }}}}} blink.gif



That did it. Thanks a bunch for the help, I appreciate it.

(Yeah, that's the blocking I like to call "Here's a bracket I forgot so you can let me compile you stupid program.")
User is offlineProfile CardPM
+Quote Post

Jerichozero
RE: Array Difficulties.
13 Dec, 2007 - 07:50 PM
Post #4

New D.I.C Head
*

Joined: 13 Dec, 2007
Posts: 4


My Contributions
QUOTE(Jerichozero @ 13 Dec, 2007 - 07:02 PM) *

QUOTE(baavgai @ 13 Dec, 2007 - 06:52 PM) *

I don't quite understand what the sorting is about. In that last alphabet display, outputting sentence makes little sense. I think maybe you were aiming for this:
CODE

for(int i=0; i<alphabet; i++) {
    if (counters[i]>0) {
        cout << (char)(i+'a') << setw(4) << counters[i] << endl;
    }
}


Hope this helps.

PS. you win the most funky block style I've seen in a very long time. }}}}} blink.gif



That did it. Thanks a bunch for the help, I appreciate it.

(Yeah, that's the blocking I like to call "Here's a bracket I forgot so you can let me compile you stupid program.")


Agh, perhaps I spoke too soon. I'd forgotten that I need the output to display the first most frequent letter first, followed by the second, and so on. The sort function I have in there doesn't seem to be doing this for me in the current setup, however. Any suggestions on what needs to be changed? (I've also found a strange anomaly that I can't quite figure out. If the user inputs only three letters, then every time it will return the accurate letter count for the three entered, and then an 'x' counted once! It doesn't do this if the user inputs more than or less than three.)
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Array Difficulties.
13 Dec, 2007 - 08:12 PM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(Jerichozero @ 13 Dec, 2007 - 10:50 PM) *

Any suggestions on what needs to be changed?


This is a bit of a challenge. You essentially need to sort a list of items with two values, (letter, count). You can create an array of struct, load it up with your hits, and then sort that.

Another attack, unorthodox but fairly straight forward. You can find the position of the maximum ( or minimum) value, print that, zero out that position, and repeat. You keep going until you get back a count of zero and you're done.

Good luck.

User is offlineProfile CardPM
+Quote Post

Jerichozero
RE: Array Difficulties.
14 Dec, 2007 - 09:02 AM
Post #6

New D.I.C Head
*

Joined: 13 Dec, 2007
Posts: 4


My Contributions
QUOTE(baavgai @ 13 Dec, 2007 - 09:12 PM) *

QUOTE(Jerichozero @ 13 Dec, 2007 - 10:50 PM) *

Any suggestions on what needs to be changed?


This is a bit of a challenge. You essentially need to sort a list of items with two values, (letter, count). You can create an array of struct, load it up with your hits, and then sort that.

Another attack, unorthodox but fairly straight forward. You can find the position of the maximum ( or minimum) value, print that, zero out that position, and repeat. You keep going until you get back a count of zero and you're done.

Good luck.


Could I do something similar to the following to the sort code I have, in that case? (Unfortunately, we aren't yet allowed to use structs in the class.)

CODE

void sort(int a[], char x[], int number){
     int temp, i, j;
     char temp2;
     for ( i = 0; i <= number; i++ )
   {
      for ( j = i; j <= number; j++ )
      {
         if ( a[j] < a[i] )
         {
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            temp2 = x[i];
            x[i] = x[j];
            x[j] = temp2;
         }
      }
   }
}


This post has been edited by Jerichozero: 14 Dec, 2007 - 09:05 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:41AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month