Here\'s the skeleton of the program along with the expected output. nigel baler
ID: 653770 • Letter: H
Question
Here's the skeleton of the program along with the expected output. nigel baler emacs: freq cpp ile Edit View onds Iools Options Buffers C++ Help FUNCTION PROTOTYPES GO HERE oid init vectors (vector & frequencies) tring const string & prompt) pool is a lphabetic (const char character) oid create list (const string & str text, vector char & vec text) oolis ne (const vector char & list, char character) int find index (const vector char & list, char character) ant compute vowel freqs (const vector char & text, const vector char vowels, 2 vector & void display characters (const vector
Explanation / Answer
//C++ program that count the frequencies of a vowels in a string using vectors.
//The program prompts user to enter a string and count the freqencies of vowels
//in the string and display the frequency count and consontant count.
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
using namespace std;
void init_vectors(vector<char>&,vector<int>&);
string read_text(string &prompt);
bool is_aphabetic(const char character);
void create_list(string &str_text,vector<char>&vect_text);
bool is_member(vector<char> &list, char character);
int find_index(vector<char>&list,char character);
int compute_vowel_freqs(vector<char>&text,vector<char>&vowels,
vector<int>&freq);
void display_characters(vector<char>&characters, const int colWidth);
void display_freqs(vector<int>&freqs, const int colWidth);
const int VECTOR_SIZE=5;
int main()
{
vector<char>vowels(5);
vector<int>freqs(5);
string input;
vector<char> text;
int consonants(0);
const int COLUMNWIDTH=2;
init_vectors(vowels,freqs);
read_text(input);
create_list(input,text);
consonants=compute_vowel_freqs(text,vowels, freqs);
display_characters(vowels,COLUMNWIDTH);
display_freqs(freqs,COLUMNWIDTH);
cout<<"There are "<<consonants<<" consonants"<<endl;
system("pause");
return 0;
}
//Initiallization of vowels and freq vectors
void init_vectors(vector<char>& vowels,vector<int>&freq)
{
string vowel_str="aeiou";
for(int index=0;index<VECTOR_SIZE;index++)
{
vowels.insert(vowels.begin()+index,vowel_str.at(index));
freq.insert(freq.begin()+0,0);
}
}
//read string from keyboard
string read_text(string &prompt)
{
cout<<"Enter string "<<endl;
getline(std::cin, prompt);
return prompt;
}
//Returns true if the character is alphabet
bool is_aphabetic(const char character)
{
if(isalpha(character))
return true;
else
return false;
}
//Create a vect_text with text of str_text
void create_list(string &str_text,vector<char>&vect_text)
{
for(int index=0;index<str_text.length();index++)
{
if(is_aphabetic(str_text.at(index)))
vect_text.push_back(str_text.at(index));
}
}
//Returns true if character is in the vector list
bool is_member(vector<char> &list, char character)
{
bool member=false;
for(int index=0;index<list.size() && !member;index++)
if(list.at(index)==character)
member=true;
return member;
}
//Returns the index of the character in list vector
int find_index(vector<char>&list,char character)
{
bool member=false;
int pos=-1;
for(int index=0;index<list.size() && !member;index++)
if(list.at(index)==character)
{
member=true;
pos=index;
}
return pos;
}
//Returns the constants and count the vowels and insert int freq vector
int compute_vowel_freqs( vector<char>&text, vector<char>&vowels,
vector<int>&freq)
{
bool vowel=false;
int acount=0;
int ecount=0;
int icount=0;
int ocount=0;
int ucount=0;
int consonentCount=0;
for(int index=0;index<text.size();index++)
{
switch(text.at(index))
{
case 'a':
acount=acount+1;
freq.insert(freq.begin()+0,acount);
vowel=true;
break;
case 'e':
ecount=ecount+1;
freq.insert(freq.begin()+1,ecount);
vowel=true;
break;
case 'i':
icount=icount+1;
freq.insert(freq.begin()+2,icount);
vowel=true;
break;
case 'o':
ocount=ocount+1;
freq.insert(freq.begin()+3,ocount);
vowel=true;
break;
case 'u':
ucount=ucount+1;
freq.insert(freq.begin()+4,ucount);
vowel=true;
break;
}
if(!vowel)
consonentCount++;
vowel=false;
}
return consonentCount;
}
//Display the vowel characters
void display_characters(vector<char>&characters, const int colWidth)
{
for(int index=0;index<VECTOR_SIZE;index++)
{
cout<<setw(colWidth)<<characters.at(index);
}
cout<<endl;
}
//Display the frequencies
void display_freqs(vector<int>&freqs, const int colWidth)
{
for(int index=0;index<VECTOR_SIZE;index++)
{
cout<<setw(colWidth)<<freqs.at(index);
}
}
Hope this helps you..