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

#include <iostream> #include <iomanip> #include <string> #include <vector> using

ID: 3539436 • Letter: #

Question

 #include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

// FUNCTION PROTOTYPES GO HERE:
void init_vectors(vector<char> & vowels, vector<int> & frequencies);
string read_text(const string & prompt);
bool is_alphabetic(const char character);
void create_list(const string & str_text, vector<char> & vec_text);
bool is_member(const vector<char> & list, char character);
int find_index(const vector<char> & list, char character);
int compute_vowel_freqs(const vector<char> & text, const vector<char> & vowels, vector<int> & freqs);
void display_characters(const vector<char> & characters, const int colwidth);
void display_freqs(const vector<int> & freqs, const int colwidth);

int main()
{
// Define local variables and constants
vector<char> vowels;
vector<int> freqs;
string input;
vector<char> text;
int consonants(0);

const int COLUMNWIDTH = 2;

// Initialize the list of vowels and vowel frequencies.


// Call function init_vectors with variables vowels and freqs
init_vectors(vowels,freqs);

// Prompt the user for the input text by calling function read_text
input=read_text("Enter your text: ");

// Copy the characters (ignoring non-alphabetic characters) in the input string to the vector of characters in variable text
// Call function create_list to do this

create_list(input,text);

// Compute the frequencies of vowels and consonants from the input text containing only alphabetic letters
// Call function compute_vowel_freqs to do this
compute_vowel_freqs(text,vowels,freqs);


// Display the vowels and their frequencies
// Call functions display_characters and display_freqs

display_characters(vowels,COLUMNWIDTH);
display_freqs(freqs,COLUMNWIDTH);

// Display the number of consonants. No function calls here.
cout<<"There are consonants."<<endl;

return 0;
}

// FUNCTION DEFINITIONS GO HERE:
void init_vectors(vector<char> & vowels, vector<int> & frequencies)
{
vowels.push_back('a');
vowels.push_back('e');
vowels.push_back('i');
vowels.push_back('o');
vowels.push_back('u');
vowels.push_back('y');

frequencies.push_back(0);
frequencies.push_back(0);
frequencies.push_back(0);
frequencies.push_back(0);
frequencies.push_back(0);
frequencies.push_back(0);

}


string read_text(const string & prompt)
{
string phrase;
cout<<prompt;
getline(cin,phrase);
return phrase;
}

void create_list(const string & str_text, vector<char> & vec_text)
{
for( int i = 0 ; i < str_text.length() ; i++)
{
if(is_alphabetic(str_text[i]))
vec_text.push_back(str_text[i]);
}
}

bool is_alphabetic(const char character)
{
bool alphabet;
if( character >= 'a' && character <= 'z' || character >= 'A' && character<= 'Z')
alphabet = true;
else
alphabet = false;
return alphabet;
}

bool is_member(const vector<char> & list, char character)
{
bool vowels;

for(int i=0;i<list.size();i++)
{
if(character == list[i])
vowels = true;
else
vowels = false;
}

return vowels;
}



int find_index(const vector<char> & list, char character)
{
int index = -1;

for(int i=0;i<list.size();i++)
{
if(character == list[i])
{
index = i;
break;
}
}

return index;
}

int compute_vowel_freqs(const vector<char> & text, const vector<char> & vowels, vector<int> & freqs)
{
int count_consonant = 0;

for(int i = 0 ; i < text.size() ; i++)
{ int index;
if(is_member(vowels, text[i]))
{
index = find_index(vowels , tolower(text[i]));
freqs[index]++;
}
else
count_consonant++;


}

return count_consonant;
}



void display_characters(const vector<char> & characters, const int colwidth)
{

for(int i=0;i<characters.size();i++)
{
cout<<setw(colwidth)<<characters[i];

if((i+1)<characters.size())
cout<<",";
}
cout<<endl;
return;
}
void display_freqs(const vector<int> & freqs, const int colwidth)
{
for(int i=0;i<freqs.size();i++)
{
cout<<setw(colwidth)<<freqs[i];

if((i+1)<freqs.size())
cout<<",";
}
cout<<endl;
return;
}

Explanation / Answer

//CORRECTED CODE

// I HAVE HIGHLIHTED YOUR ERRORS IN BOLD, YOU HAD TWO ERRORS

// ONE IN THE is_member() function, ONE IN THE main() function


#include <iostream>

#include <iomanip>

#include <string>

#include <vector>


using namespace std;


// FUNCTION PROTOTYPES GO HERE:

void init_vectors(vector<char> & vowels, vector<int> & frequencies);

string read_text(const string & prompt);

bool is_alphabetic(const char character);

void create_list(const string & str_text, vector<char> & vec_text);

bool is_member(const vector<char> & list, char character);

int find_index(const vector<char> & list, char character);

int compute_vowel_freqs(const vector<char> & text, const vector<char> & vowels, vector<int> & freqs);

void display_characters(const vector<char> & characters, const int colwidth);

void display_freqs(const vector<int> & freqs, const int colwidth);


int main()

{

// Define local variables and constants

vector<char> vowels;

vector<int> freqs;

string input;

vector<char> text;

int consonants(0);


const int COLUMNWIDTH = 2;


// Initialize the list of vowels and vowel frequencies.



// Call function init_vectors with variables vowels and freqs

init_vectors(vowels,freqs);


// Prompt the user for the input text by calling function read_text

input=read_text("Enter your text: ");


// Copy the characters (ignoring non-alphabetic characters) in the input string to the vector of characters in variable text

// Call function create_list to do this


create_list(input,text);


// Compute the frequencies of vowels and consonants from the input text containing only alphabetic letters

// Call function compute_vowel_freqs to do this

// it also counts the consonants

consonants = compute_vowel_freqs(text,vowels,freqs);



// Display the vowels and their frequencies

// Call functions display_characters and display_freqs


display_characters(vowels,COLUMNWIDTH);

display_freqs(freqs,COLUMNWIDTH);


// Display the number of consonants. No function calls here.

cout<<"There are "<<consonants<<" consonants."<<endl;


return 0;

}


// FUNCTION DEFINITIONS GO HERE:

void init_vectors(vector<char> & vowels, vector<int> & frequencies)

{

vowels.push_back('a');

vowels.push_back('e');

vowels.push_back('i');

vowels.push_back('o');

vowels.push_back('u');

vowels.push_back('y');


frequencies.push_back(0);

frequencies.push_back(0);

frequencies.push_back(0);

frequencies.push_back(0);

frequencies.push_back(0);

frequencies.push_back(0);


}



string read_text(const string & prompt)

{

string phrase;

cout<<prompt;

getline(cin,phrase);

return phrase;

}


void create_list(const string & str_text, vector<char> & vec_text)

{

for( int i = 0 ; i < str_text.length() ; i++)

{

if(is_alphabetic(str_text[i]))

vec_text.push_back(str_text[i]);

}

}


bool is_alphabetic(const char character)

{

bool alphabet;

if( character >= 'a' && character <= 'z' || character >= 'A' && character<= 'Z')

alphabet = true;

else

alphabet = false;

return alphabet;

}


//as soons as vowels becomes true, you should break, else, it'll become false once the loop continues again

bool is_member(const vector<char> & list, char character)

{

bool vowels=false;


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

{

if(character == list[i])

{vowels = true; break; }


}


return vowels;

}




int find_index(const vector<char> & list, char character)

{

int index = -1;


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

{

if(character == list[i])

{

index = i;

break;

}

}


return index;

}


int compute_vowel_freqs(const vector<char> & text, const vector<char> & vowels, vector<int> & freqs)

{

int count_consonant = 0;


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

{ int index;

if(is_member(vowels, text[i]))

{

index = find_index(vowels , tolower(text[i]));

freqs[index]++;

}

else

count_consonant++;



}


return count_consonant;

}




void display_characters(const vector<char> & characters, const int colwidth)

{


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

{

cout<<setw(colwidth)<<characters[i];


if((i+1)<characters.size())

cout<<",";

}

cout<<endl;

return;

}

void display_freqs(const vector<int> & freqs, const int colwidth)

{

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

{

cout<<setw(colwidth)<<freqs[i];


if((i+1)<freqs.size())

cout<<",";

}

cout<<endl;

return;

}