C++ programming please ! Will rate ! I need the code below fixed and need a head
ID: 3870871 • Letter: C
Question
C++ programming please ! Will rate !
I need the code below fixed and need a header file as well.
Only able to Have one data member, of type int*, which keeps track of how many times each letter appears. While this class could be implemented using a static array this requirement is designed to test your ability to implement a class that allocates and deallocates memory dynamically.
Output should be printed in the form letter number, where letter appears number times in the count. You should print the counts in alphabetical order, but you should not print a letter or count if the letter does not appear.(no spaces,!,etc)
Code Below:
// lettercount.cpp
#include <iostream>
#include <vector>
using namespace std;
#include "lettercount.h"
int main()
{
//Testing default constructor and totalCount()
LetterCount lc;
cout << "Total letters in new LC: " << lc.totalCount() << endl;
//Testing print()
cout << "Letters in empty LC: ";
lc.print();
cout << endl;
//Testing single-value constructor
LetterCount area("area");
cout << "Letters in "area": ";
area.print();
cout << endl;
//Testing copy constructor
cout << "Making a copy of "area": ";
LetterCount copy(area);
copy.print();
cout << endl;
//Testing move constructor
vector<LetterCount> vec;
cout << "Moving "temp": ";
vec.push_back(LetterCount("temp"));
vec[0].print();
cout << endl;
//Testing destructor
LetterCount* ydd = new LetterCount("yabba dabba doo");
cout << "Before deleting "yabba dabba doo": ";
ydd->print();
cout << endl;
delete ydd;
//Note: next line should cause a seg fault or print garbage data when uncommented
//cout << "After deleting: ";
//ydd->print();
//cout << endl;
//Testing equals
LetterCount are("are"), era("era");
cout << ""are"";
if (are.equals(era))
cout << " equals "era" ";
else
cout << " does not equal "era" ";
//Testing hasAsMany
cout << ""area"";
if (area.hasAsMany(are))
cout << " has as many letters as "are" ";
else
cout << " does not have as many letters as "are" ";
//Testing add
cout << ""area" + "era" = ";
area.add(era);
area.print();
cout << endl;
//Testing subtract
cout << ""area" - "era" = ";
copy.subtract(era);
copy.print();
cout << endl;
return 0;
}
output txt sample:
Total letters in new LC: 0
Letters in empty LC: none
Letters in "area": a 2 e 1 r 1
Making a copy of "area": Copy a 2 e 1 r 1
Moving "temp": Move e 1 m 1 p 1 t 1
Before deleting "yabba dabba doo": a 4 b 4 d 2 o 2 y 1
"are" equals "era"
"area" has as many letters as "are"
"area" + "era" = a 3 e 2 r 2
"area" - "era" = a
Explanation / Answer
Given below is the lettercount.h file need to get the program running. The output of the main() program is shown below. Post a comment in case of any issues, I will help. Please don't forget to rate the answer if it helped. Thank you.
lettercount.h
#ifndef lettercount_h
#define lettercount_h
#include <iostream>
using namespace std;
class LetterCount
{
private:
int *count; //an array of counters, count[0] is the counter for 'a' , count[1] is a counter for 'b' and so on
//helper function called by constructor to create and initialize the counters
void initialize()
{
count = new int[26]; //allocate space for counters for the 26 letters of alphabet
for(int i = 0; i < 26; i++)
count[i] = 0;
}
public:
LetterCount()
{
initialize();
}
LetterCount(const string &str)
{
int index;
char ch;
initialize();
//iterate over each of the letters in the string
for(int i = 0; i < str.size(); i++)
{
ch = str[i];
if(ch >= 'a' && ch <= 'z') //lower case letters
{
index = ch - 'a'; //calculate the index of the counter for the current character
count[index] ++;
}
else if(ch >= 'A' && ch <= 'Z') //upper case letters
{
index = ch - 'A'; //calculate the index of the counter for the current character
count[index] ++;
}
}
}
//copy constructor
LetterCount(const LetterCount &other)
{
initialize();
for(int i = 0; i < 26; i++)
count[i] = other.count[i];//copy all the counter values individually
}
void print()
{
if(totalCount() == 0) //if all counters were 0
cout << "none";
else
{
//iterate over each of counters
for(int i = 0; i < 26; i++)
{
//print only non-zero counters
if(count[i] != 0)
{
char ch = (char) ('a' + i); //get the character for the index value
cout << ch << " " << count[i] << " ";
}
}
cout << endl;
}
}
//get the total of all the 26 counters
int totalCount()
{
int total = 0;
//iterate each of the counters
for(int i = 0; i < 26; i++)
total += count[i];
return total;
}
//2 objects of LetterCount are equal if their correspoding counters for each of the alphabet lettesr are equal
//returns true if this object is having the same values of counter as in the the passed other object
//return false if not equal
bool equals(const LetterCount& other)
{
for(int i = 0; i < 26; i++)
{
if(count[i] != other.count[i])
return false;
}
//all matched by end of loop
return true;
}
//for each of the non-zero counters in other, if this objects has a corresponding non-zero counter value, then
//returns true, otherwise returns false. i.e each letter present in other should be present in this
bool hasAsMany(const LetterCount &other)
{
for(int i = 0; i < 26; i++)
{
if(other.count[i] != 0) //a non-zero counter in other,
{
if(count[i] == 0) //counter is zero in this object
return false;
}
}
return true;
}
void add(const LetterCount &other)
{
for(int i = 0; i < 26; i++)
{
count[i] = count[i] + other.count[i]; //add the corresponding counters
}
}
void subtract(const LetterCount &other)
{
for(int i = 0; i < 26; i++)
count[i] = count[i] - other.count[i]; //subtract the corresponding counters
}
~LetterCount()
{
delete[] count;
}
};
#endif /* lettercount_h */
output
Total letters in new LC: 0
Letters in empty LC: none
Letters in "area": a 2 e 1 r 1
Making a copy of "area": a 2 e 1 r 1
Moving "temp": e 1 m 1 p 1 t 1
Before deleting "yabba dabba doo": a 4 b 4 d 2 o 2 y 1
"are" equals "era"
"area" has as many letters as "are"
"area" + "era" = a 3 e 2 r 2
"area" - "era" = a 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.