codechick
codechick

Reputation: 41

C++ compare two arrays and display duplicates - Code works now

At last, here is the code that works:

#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <Windows.h>

using namespace std;

bool checkBuffer( char buffArray[], int buffSize );
void parseBuffer( char alphaArray[], char buffArray[], int alphaSize, int buffSize );

int main() 
{
   char alphabetArray[]= "abcdefghijklmnopqrstuvwxyz";
   char buffer[11];

   cout << "Enter the string up to 10 letters." << endl;
   cin.get(buffer, strlen(buffer) );

   checkBuffer(buffer, sizeof(buffer) );
   parseBuffer( alphabetArray, buffer, sizeof(alphabetArray), sizeof(buffer) );

   system("Pause");
   return 0;
}
bool checkBuffer( char buffArray[], int buffSize )
  {
    if(buffArray, strlen(buffArray) == 0)
      {
        cout << "The buffer is empty.  The program will end in 3 seconds. "  << endl;
        Sleep(3000);
        exit(1);
      }
} 
void parseBuffer( char alphaArray[], char buffArray[], int sizeOne, int sizeTwo )
{
   int countFreq = 0;
   for(int i = 0; i < strlen(alphaArray); i++ )
    {
       countFreq = 0; 

   for(int j = 0; j < strlen(buffArray); j++)
     {    
         if( alphaArray[i] == buffArray[j] )
         countFreq = countFreq + 1;
     }
         cout << "The letter " << alphaArray[i] << "  matched " << countFreq 
         << "    times." << endl; 
     }
}

I am working on a practice textbook question that asks a user to input 10 characters that go into an array and then that array is compared to a hard-coded alphabet array. The output should display the number of duplicates, if any, per letter. For example: "There are 2 a's." "There are 0 b's." "There are 3 c's." .....and so on.

My code is correctly counting the number of duplicates (or not) between the 2 arrays. However, the problem is that it is displaying the count EVERY TIME THE LOOP ITERATES. I only need it to display THE TOTAL COUNT. I tried moving the "cout" statement below the loop which doesn't work because it needs the [i] and [j] from where it loops thru the arrays. Please point out where my error is, thanks in advance!

#include <iostream>   // using DevCPP editor
#include <iomanip> 
#include <string> 
#include <algorithm> 
#include <Windows.h> 

using namespace std; 

void parseBuffer( char buffArray[], char alphaArray[], int sizeOne, int sizeTwo ); 

int main()  
{ 
 // precode alphabet into an array with null terminating character 
char alphabetArray[]={'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','\0'}; 

char buffer[11]; 
cout << "Enter the string up to 10 letters." << endl; 
cin.get(buffer, 11); 
parseBuffer(buffer, alphabetArray, 11, 11); 

system("Pause"); 
return 0; 

}  
void parseBuffer(char buffArray[], char alphaArray[], int sizeOne, int sizeTwo) 
{ 
 int countFreq = 0; 
 cout << "This is buffer array: " << buffArray << endl; 
 cout << "This is alphabet array: " << alphaArray << endl<< endl; 

 for(int i = 0; i < (sizeTwo - 1); i++) 
 {   
  alphaArray[i];  

 for(int j = 0; j < (sizeOne -1); j++) 
 {  
   buffArray[j];     

 if(alphaArray[i] == buffArray[j] ) 
 {  
  countFreq = countFreq + 1;  
 }  
 else 
  { 
   countFreq = 0; 
  }   
cout << "It's a match.  " << alphaArray[i] << "   shows up   " << countFreq << "  times." << endl << endl;       
  } 
 } 
}  // end "parseBuffer"  

Upvotes: 1

Views: 3443

Answers (3)

Malkavian
Malkavian

Reputation: 372

I have a simpler solution although this might not be what you're looking for, it is faster and smaller. First of all, you don't need an array of chars, chars have int values('A'=65 'F'=70, etc). so with that information it should be clear to you that you can perform this task in O(n) time

Here's the pseudocode:

for(int i=0 to size of your string i++)
{
    char ch is string.at(i);
    ch equal to upper case of ch
    short j = (convert ch to int);
    if( j is between A and Z) then
    {
        count at j ++; 
    }
}
for(A to Z)
{
    display frequency
}

Here's the actual code:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string data;
    int count[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    const int BASE=65;
    cout<<"string: "; 
    getline(cin,data);
    int dataSize=data.length();
    for(int i=0; i<dataSize; i++)
    {
        char ch=data.at(i);
        ch=toupper(ch);
        short j=(int)ch;

        if(j>=BASE && j<=BASE+25)
        {
            count[j-BASE]++;
        }
    }
    for(int i=0; i<26; i++)
    {
        char ch=i+BASE;
        cout<<"There are "<<count[i]<<" "<<ch<<"'s."<<endl;
    }
}

Upvotes: 1

ddacot
ddacot

Reputation: 1210

Take a look at that.

#include <string>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int main()
{
string s;
char c;
char alphabetArray[] =
    {'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','\0'}; 
int count = 0;
cout << "Enter a string : ";
getline(cin, s);
for(int j = 0 ; j < strlen(alphabetArray); j++) {

for(int i = 0 ; i < s.size(); i++) {

if(s[i]==alphabetArray[j]) {
    count++;
    cout<<alphabetArray[j]<<" shows up "<<count<<" times"<<endl;
}
count=0;
}

}
system("pause");
return 0;
}

Upvotes: 1

devil
devil

Reputation: 1839

There are a few main problems with your code.

  1. The looping order is incorrect. If you wish to reuse countFreq for each character in alphaArray, you need to switch the inner and outer loops.

  2. You must initialize countFreq = 0 for each alphaArray character.

  3. You must increment countFreq, i.e., ++countFreq, countFreq++, or countFreq += 1 for each matching character in buffArray INSTEAD of assigning 1 via countFreq = 1. What you are doing now is reseting countFreq = 1 whenever there's a match.

  4. You are looping 1 less, i.e., you check up to 10 chars instead of 11 and you seem to be using the wrong size limit for alphaArray.

Please format your code and use the for loops properly. i.e. for(int j = 0; j < sizeOne; ++j).

Upvotes: 2

Related Questions