problem Counting letters in a dynamically created array of chars

Jun 6, 2009 at 3:20am
This program is supposed to store random lowercase letters into an array and then count each letter occurence. My problem is that for some strange reason this program is only counting the first 15 letters of a 100 element array of chars.

#include<iostream>
using namespace std;
#include "time.h"


const int NUMBER_OF_LETTERS = 100;
char* createArray();
void displayArray(char []);
int* countLetters(char []);
void displayCounts(int []);

int main()
{

char* chars=createArray();

cout<<"The lowercase letters are: "<<"\n"<<endl;
displayArray(chars);



int* counts=countLetters(chars);
displayCounts(counts);


return 0;

}

char* createArray()
{

srand(time(0));
char* lCC=new char;

for(int i=0;i<NUMBER_OF_LETTERS;i++)
lCC[i]=static_cast<char>(((rand()%26)+97));


return(lCC);
}

void displayArray(char a[])
{
for(int i=0;i<NUMBER_OF_LETTERS;i++)
cout<<a[i]<<" ";

cout<<"\n"<<endl;
}

int* countLetters(char b[])
{

int* countsL=new int;


for(int i=0;i<NUMBER_OF_LETTERS;i++)
countsL[i]=0;



for(int i=0;i<NUMBER_OF_LETTERS;i++)
{
for(int j=97;j<123;j++)
{
if(b[i]==static_cast<char>(j))
countsL[j-97]++;
}

}
return(countsL);
}


void displayCounts(int c[])
{


for(int i=0;i<26;i++)
cout<<static_cast<char>(i+97)<<" = "<<c[i]<<"\n";

}
Jun 6, 2009 at 2:22pm
In allocating both of your arrays you haven't specified the size, i.e., you only allocate a single char and a single int. You need:

1
2
3
4
// In createArray()
    char* lCC=new char[NUMBER_OF_LETTERS];
// In countLetters()
    int* countsL=new int[26];

Jun 6, 2009 at 5:21pm
Thanks for the solution, it works!.
Jun 6, 2009 at 5:56pm
Make your life easier. For dynamic arrays, use std::vector. For counting, use std::count. Use code tags when posting and please indent properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <vector>
#include <algorithm>
#include <iostream>

void countLetters(const std::vector<char>& letters, std::vector<int>& countsL);
void displayCounts(const std::vector<char>& letters, std::vector<int>& countsL);
const unsigned int NUMBER_OF_LETTERS(100);
const unsigned int LETTERS_IN_ALPHABET(26);
const unsigned int OFFSET_TO_a(97);

int main()
{
   std::vector<char> letters(NUMBER_OF_LETTERS);
   std::vector<int> countsL(LETTERS_IN_ALPHABET); // zero initialize vector with 26 elements
   for(unsigned int i = 0; i < NUMBER_OF_LETTERS; ++i)
   {
       letters[i] = static_cast<char>(((rand() % LETTERS_IN_ALPHABET) + OFFSET_TO_a));
   }
   countLetters(letters, countsL);
   displayCounts(letters, countsL);
   return 0;
}

void countLetters(const std::vector<char>& letters, std::vector<int>& countsL)
{
    char c('a');
    unsigned int i(0); 
    do {
	// count all occurrences of character c then increment c.
	// increment i, and test i before next loop.
	countsL[i++] = std::count(letters.begin(), letters.end(), c++);
    }while(i < LETTERS_IN_ALPHABET);
}

void displayCounts(const std::vector<char>& letters, std::vector<int>& countsL)
{
    unsigned int i(0);
    
    // display the original, 20 per line.  
    const unsigned int NUM_PER_LINE(20);
    do {
	std::cout << letters[i++] << ' ';
	if(0 == (i % NUM_PER_LINE)) 
	{
	    std::cout << std::endl;
	}
    } while(i < NUMBER_OF_LETTERS);

    char c('a');
    i = 0;
    do {
	// display the character, then increment c to the next letter.
	// display the count for that letter, increment i, and test
	// i before next loop.
	std::cout << c++ << "'s : " << countsL[i++] << std::endl;
    }while(i < LETTERS_IN_ALPHABET);
}
Topic archived. No new replies allowed.