Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Background: The purpose of this assignment is to practice dealing with arrays an

ID: 3665773 • Letter: B

Question

Background:

The purpose of this assignment is to practice dealing with arrays and array parameters. To Computer Scientists, C++ is consider not pure because not everything is actually a class or object. For example, arrays are neither classes nor objects. As a result, they have their own way of being passed as a parameter and they also do not support the dot-guy (.), so you cannot determine how full they are. This results in some pain and suffering when coding with arrays. It is this awareness that I am trying to get across to you in this homework assignment.

In C++, string handling can be kinda challenging. But this topic pretty much summarizes all the things we have seen far so I think it makes for a very worthwhile assignment.

The class string in C++ provides us many use features including:

a constructor that converts c-strings into string objects; as in:   char * cstr = "Hello"; string s( cstr );

a converting to produce a c-string from a string object; as in: char * cstr = s.c_str( );

a member that tells us the strings length; as in: int len = s.length( );

a member that produces a letter; as in: char c = s.at( 5 );

an operator that combines strings; as in: string u = s + t;

we also have to remember to use getline when we want to read a string that potentially has spaces because operator >> breaks on whitespace; as in:
  string s; std::getline( std::cin, s );

With this assignment, I hope you'll get a chance to experiment with all these different features.

Project 10: String Histogram
A Histogram (http://en.wikipedia.org/wiki/Histogram) is a visual way of representing frequency counts. Most computer programs that produce histograms show the output horizontally rather than vertically. The goal of this assignment is to create a class that produces histograms from string data using some of the string functions outlined above. The output will occur when the output operator is invoked as shown below.

Histogram

enum LETTER { A=1,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 };

void acceptString( string data );
void acceptCString( char * data );
void reset( );

ostream& operator<<( ostream& outs, const Histogram& h );

string my_DataSoFar;
int my_FrequencyCount[ 27 ];

void incrementFrequency( LETTER l );

Histogram h;

string s;
cout << "Enter Data:"
cin >> s;

h.acceptString( );

cout << h << endl;

h.reset( );

char * cstr = "Hello";

h.acceptCString( cstr );

cout << h << endl;

Enter Data: supercalifragilisticexpialadocious
A - 4 - ****
C - 3 - ***
D - 1 - *
E - 2 - **
F - 1 - *
G - 1 - *
I - 6 - ******
L - 3 - ***
O - 2 - **
P - 2 - **
R - 2 - **
S - 3 - ***
T - 1 - *
U - 2 - **
X - 1 - *

E - 1 - *
H - 1 - *
L - 2 - **
O - 1 - *

Implementation Details Sample Driver

Histogram

Histogram( );

enum LETTER { A=1,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 };

void acceptString( string data );
void acceptCString( char * data );
void reset( );

ostream& operator<<( ostream& outs, const Histogram& h );

string my_DataSoFar;
int my_FrequencyCount[ 27 ];

void incrementFrequency( LETTER l );

Histogram h;

string s;
cout << "Enter Data:"
cin >> s;

h.acceptString( );

cout << h << endl;

h.reset( );

char * cstr = "Hello";

h.acceptCString( cstr );

cout << h << endl;

Sample Output

Enter Data: supercalifragilisticexpialadocious
A - 4 - ****
C - 3 - ***
D - 1 - *
E - 2 - **
F - 1 - *
G - 1 - *
I - 6 - ******
L - 3 - ***
O - 2 - **
P - 2 - **
R - 2 - **
S - 3 - ***
T - 1 - *
U - 2 - **
X - 1 - *

E - 1 - *
H - 1 - *
L - 2 - **
O - 1 - *

Explanation / Answer

Histogram.h

#pragma once
#include<iostream>
#include<string>
using namespace std;
class Histogram
{
public:
   Histogram();
   ~Histogram();
   enum LETTER
   {
       A = 1, 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
   };
   void acceptString(string data);
   void acceptCString(char *data);
   void reset();
   friend ostream& operator<<(ostream& outs, const Histogram& h);
private:
   string my_DataSoFar;
   int my_FrequencyCount[27];
   void incrementFrequency(LETTER l);

};

Histogram.cpp

#include "stdafx.h"
#include "Histogram.h"


Histogram::Histogram()
{
   for (int i = 0; i < 27; ++i)
   {
       my_FrequencyCount[i] = 0;
   }
   my_DataSoFar = "";
}


Histogram::~Histogram()
{
}

void Histogram::acceptString(string data)
{

   my_DataSoFar = data;
   for (unsigned int i = 0; i < data.length(); ++i)
   {

       //Finding the ascii value of each character of string
       int c = (int)data.at(i);
       //ascii for a is 97 and my_Frequency[0] represents frequency of a
       if (c >= 97 && c <= 122)
       {
           c = c - 97;
       }
       //for uppercase letters
       else if (c >= 65 && c <= 90)
       {
           c = c - 65;
       }
       if (c >= 0 && c <= 25)
       {
           my_FrequencyCount[c]++;
       }
   }

}

void Histogram::acceptCString(char* data)
{
   string s(data);
   my_DataSoFar = s;
   for (unsigned int i = 0; i < my_DataSoFar.length(); ++i)
   {

       //Finding the ascii value of each character of string
       int c = (int)my_DataSoFar.at(i);
       //ascii for a is 97 and my_Frequency[0] represents frequency of a
       if (c >= 97 && c <= 122)
       {
           c = c - 97;
       }
       //for uppercase letters
       else if (c >= 65 && c <= 90)
       {
           c = c - 65;
       }
       if (c >= 0 && c <= 25)
       {
           my_FrequencyCount[c]++;
       }
   }
}

void Histogram::reset()
{
   for (int i = 0; i < 27; ++i)
   {
       my_FrequencyCount[i] = 0;
   }
   my_DataSoFar = "";
}

void Histogram::incrementFrequency(LETTER l)
{
   my_FrequencyCount[l]++;
}

ostream& operator<<(ostream& outs, const Histogram& h)
{
   for (int i = Histogram::LETTER::A; i != Histogram::Z+1; i++)
   {
       if (h.my_FrequencyCount[i - 1] > 0)
       {

//finding the character based on ascii value
           char c = (char)(i + 64);
           outs <<c << "-" << h.my_FrequencyCount[i - 1];
           for (int j = 0; j < h.my_FrequencyCount[i - 1]; ++j)
           {
               outs << "*";
           }
           cout << endl;
       }
      
   }
   return outs;
}