Hi there,
I would like to ask you whether if my answer to the homework is right or not?
The homework requirment is:
Delivery method
Please archive and compress your file(s) using the tar command
pa6.tgz (all lowercase) for your compress and archived file and
account at the email account for the class. Attach this file to the
class account. Put PA6 in the subject field of the email and your
email. Follow the guidelines for program style and electronic
the web. You must adhere to this policy to receive credit for
archive and compress your files. It is assumed that you are at
have logged on to Ouray, and that all your files are on the current
tar –zcvf pa6.tgz pa6.cpp pa6functions.h pa6functions.
Program objectives
The objectives of this assignment are as follows.
1. Test your ability to design software and/or hardware to meet desired needs
(measurable outcome (d)).
2. Test your ability to identify, formulate, and solve computer
engineering problems (measurable outcome (f)).
Problem
Write a program that prompts for and gets a sentence from the user, parses the sentences
into words, finds the lexicographically largest and smallest words in the sentence and
computes the frequency of the characters in the string.
Input
A string (possibly with space characters and punctuation) entered from the keyboard.
Output
The original string, the formatted list of the words in the string, the lexicographically
largest and smallest words in the string and a list of the frequency of occurrence of the
alphabetic characters in the string.
Program requirements
1. Present the user with a greeting.
2. Prompt the user and get a string from the keyboard.
3. Parse the string into words and store the words in an array.
4. Compute the largest and smallest words in the string.
5. Display the following with annotations.
a. The original sentence.
b. The individual words.
c. The smallest and largest words in the string.
d. The frequency of occurrence of the alphabetic characters. The formatting
for this part of the display should look like this.
a 23
b 13
…..
z 0
The columns must be evenly spaced, so you will need to use an
appropriate field width.
Notes
1. You must write global functions.
2. The input string may contain uppercase and lowercase characters, spaces and
possibly punctuation.
What I did is:
----------------------------
pa6.cpp
----------------------------
Code:
#include <iostream>
#include <iomanip>
#include "pa6functions.h"
using namespace std;
int main()
{
string str;
pa6 mypa6;
cout << "************************************"<<endl;
cout << " Program Assignment 6 "<<endl;
cout << "************************************"<<endl;
cout << "Please Write a sentence -> ";
getline(cin,str);
cout << "Count Word = " << mypa6.CountWord(str)<<endl;
cout << "Count Smallest = " << mypa6.CountSmallest(str)<<endl;
cout << "Count Largest = " <<
mypa6.CountWord(str)-mypa6.CountSmallest(str) <<endl;
// cout<<mypa6.CountLetter (str,'a')<<endl;
cout << "-------------------" <<endl;
cout << "| Letter |" << " | Count |"<<endl;
cout << "-------------------" <<endl;
for(int i=int('a'); i<=int('z');i++)
cout << "| "<<char(i)<<" |"<<"| "<<setw(5)<<setfill('0')<<mypa6.CountLetter(str,char(i))<<" |"<<endl;
for(int i=int('A');i<=int('Z');i++)
cout <<"| "<<char(i)<<" |"<<"| "<<setw(5)<<setfill('0')<<mypa6.CountLetter(str,char(i))<<" |"<<endl;
cout <<"| "<<"."<<" |"<<"| "<<setw(5)<<setfill('0')<<mypa6.CountLetter(str,'.')<<" |"<<endl;
return 0;
}
----------------------------
pa6functions.h
----------------------------
Code:
#include <string>
using std::string;
class pa6
{
public:
int CountWord(string);
int CountLetter(string,char);
int CountSmallest(string);
};
----------------------------
pa6functions.cpp
----------------------------
Code:
#include <cctype>
#include "pa6functions.h"
int pa6::CountWord(string str)
{ int t=0;
for(int i=0; i<str.length();i++)
{if(str[i]==' ' && str[i+1]!=' ') t++;}
if(str[0]==' ') t--;
return (++t);
}
int pa6::CountSmallest(string str)
{int t=0;
for(int i=0; i<str.length(); i++)
{if( isspace(str[i]) && (islower(str[i+1]) )) t++;}
if(islower(str[0])) t++;
return t;
}
int pa6::CountLetter(string str, char a)
{
int t=0;
for(int i=0; i<str.length(); i++)
if(str[i]==a) t++;
return t;
}
----------------------------------
The output is
----------------------------------
Code:
[username@ouray ~]$ ./a.out
************************************
Program Assignment 6
************************************
Please Write a sentence -> Today is a wonderful day!
Count Word = 5
Count Smallest = 4
Count Largest = 1
-------------------
| Letter | | Count |
-------------------
| a || 00003 |
| b || 00000 |
| c || 00000 |
| d || 00003 |
| e || 00001 |
| f || 00001 |
| g || 00000 |
| h || 00000 |
| i || 00001 |
| j || 00000 |
| k || 00000 |
| l || 00001 |
| m || 00000 |
| n || 00001 |
| o || 00002 |
| p || 00000 |
| q || 00000 |
| r || 00001 |
| s || 00001 |
| t || 00000 |
| u || 00001 |
| v || 00000 |
| w || 00001 |
| x || 00000 |
| y || 00002 |
| z || 00000 |
| A || 00000 |
| B || 00000 |
| C || 00000 |
| D || 00000 |
| E || 00000 |
| F || 00000 |
| G || 00000 |
| H || 00000 |
| I || 00000 |
| J || 00000 |
| K || 00000 |
| L || 00000 |
| M || 00000 |
| N || 00000 |
| O || 00000 |
| P || 00000 |
| Q || 00000 |
| R || 00000 |
| S || 00000 |
| T || 00001 |
| U || 00000 |
| V || 00000 |
| W || 00000 |
| X || 00000 |
| Y || 00000 |
| Z || 00000 |
| . || 00000 |
so please help me what's the missing?
Thanks!