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

Problem solving and debugging. Linux redirection to read data from a file. Use o

ID: 3848548 • Letter: P

Question

Problem solving and debugging.

Linux redirection to read data from a file.

Use of conditional loop (while).

Data File Description
A data file for this exercise will consist of several lines of text that form sentences. The text may include letters, punctuation marks, and/or digits. Each sentence will be terminated with either a period (.), an exclamation mark (!), or a question mark (?).

Design and implement a complete C++ program that will

using Linux redirection, read the contents of the file and

compute the average number of letters per sentence in the file

count the number of digits in the file

display the average with a label and 3 digits to the right of the decimal

display the digit count with a label




NOTES:

Make sure you choose enough test data to ensure that your program meets all the requirements.

If you use any library functions in your program make sure you include the appropriate header file(s).

Sample terminal session:
[keys]$ more data4eight
What is Earth Day? Earth Day is
celebrated annually on April 22 to demonstrate support for
environmental protection. It was first marked
in 1970 and is coordinated globally by the
Earth Day Network. More than
193 countries participate each year.
[keys]$ g++ ex08.cpp
[keys]$ ./a.out < data4eight

Average # of letters per sentence is 48.250
Total # of digits in the file is 9

**EXTRA NOTES:
-This does NOT use fstream. Input will be taken through cin.
-String cannot be used. The program can be created without it.
-USE A WHILE LOOP.
-Do not use functions, arrays, or strings.

Part of my program:

#include <iostream>
#include <cctype>
using namespace std;

int main()
{

cin >> data;
while (cin)
{

cin >> data;
}


return 0;
}

Explanation / Answer

Find the below structured program as per the requirements:

#
include < iostream > #include < cctype > #include < fstream > // file stream
#include < cstdlib >
using namespace std;
int main() {
int ch;
int i, d = 0;
int numWords = 0;
int numLetters = 0;
count = 0;
bool prevWasASpace = true; //spaces at beginning are ignored

ifstream cin; // Input stream
cin = ch;
ofstream cout; // Output stream

//Try to open the file
cin.open("in.txt");
if (!cin.is_open()) {
cerr << "error: open input file failed" << endl;
abort(); // Abnormally terminate the program (in <cstdlib>)
}

while ((cin == getchar()) != EOF && ch != ' ') {
if (ch == ' ')
prevWasASpace = true;
else {
if (prevWasASpace)
numWords++;
prevWasASpace = false;
numLetters++;
}

}
if (numWords > 0) {
double avg = numLetters / (float)(numWords);
printf("average number of letters per sentence in the file: %.1f (C = %d, N = %d) ", avg, numLetters, numWords);
} else if (i > 47 && i < 58) {
d++
} else if (cin / 3) {
cout << " Average is = %f", 'cin'
cout.precision(3);
cout << cin << endl;

} else if (cin! = 0) {
cin /= 10;
++count;
}
printf("Number of digits: %d", count);
else {
printf("You didn't enter any words ");
}
cout << “nNo.Of Digits: ” << d;

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote