Go to explaination for the program code.
Explanation:
Before running the program you need to have these files created like below in your unix box.
Unix Terminal> cat nouns.txt
BOY
GIRL
BAT
BALL
Unix Terminal> cat articles.txt
A
THE
Unix Terminal> cat verbs.txt
HIT
SAW
LIKED
Unix Terminal> cat prepositions.txt
WITH
BY
Unix Terminal>
Code:
#!/usr/local/bin/python3
import random
def getWords(filename):
fp = open(filename)
temp_list = list()
for each_line in fp:
each_line = each_line.strip()
temp_list.append(each_line)
words = tuple(temp_list)
fp.close()
return words
articles = getWords('articles.txt')
nouns = getWords('nouns.txt')
verbs = getWords('verbs.txt')
prepositions = getWords('prepositions.txt')
def sentence():
return nounPhrase() + " " + verbPhrase()
def nounPhrase():
return random.choice(articles) + " " + random.choice(nouns)
def verbPhrase():
return random.choice(verbs) + " " + nounPhrase() + " " + prepositionalPhrase()
def prepositionalPhrase():
return random.choice(prepositions) + " " + nounPhrase()
def main():
number = int(input('Enter number of sentences: '))
for count in range(number):
print(sentence())
if __name__=='__main__':
main()
kindly check attachment for code output and onscreen code.
Go to explaination for the program code.
Explanation:
Before running the program you need to have these files created like below in your unix box.
Unix Terminal> cat nouns.txt
BOY
GIRL
BAT
BALL
Unix Terminal> cat articles.txt
A
THE
Unix Terminal> cat verbs.txt
HIT
SAW
LIKED
Unix Terminal> cat prepositions.txt
WITH
BY
Unix Terminal>
Code:
#!/usr/local/bin/python3
import random
def getWords(filename):
fp = open(filename)
temp_list = list()
for each_line in fp:
each_line = each_line.strip()
temp_list.append(each_line)
words = tuple(temp_list)
fp.close()
return words
articles = getWords('articles.txt')
nouns = getWords('nouns.txt')
verbs = getWords('verbs.txt')
prepositions = getWords('prepositions.txt')
def sentence():
return nounPhrase() + " " + verbPhrase()
def nounPhrase():
return random.choice(articles) + " " + random.choice(nouns)
def verbPhrase():
return random.choice(verbs) + " " + nounPhrase() + " " + prepositionalPhrase()
def prepositionalPhrase():
return random.choice(prepositions) + " " + nounPhrase()
def main():
number = int(input('Enter number of sentences: '))
for count in range(number):
print(sentence())
if __name__=='__main__':
main()
kindly check attachment for code output and onscreen code.
Taking data and instructions from a user, processing the data as per instructions, and displaying or storing the processed data, are the four major functions of a computer. These functions are also known as the input function, process function, output function, and storage function, respectively.
Taking data and instructions from a user, processing the data as per instructions, and displaying or storing the processed data, are the four major functions of a computer. These functions are also known as the input function, process function, output function, and storage function, respectively.
SIR I AM A BRAINLT MODERRATER U CAN NOT POST THINGS LIKE THIS I LOOKED THIS UP AND ITS A TEST
Explanation:
// testscores.txt
Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 48
Gupta 92 83 30 69 87
Muhammed 23 45 96 38 59
Clark 60 85 45 39 67
Patel 77 31 52 74 83
Abara 93 94 89 77 97
Abebe 79 85 28 93 82
Abioye 85 72 49 75 63
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
// function declarations
void readData(ifstream& dataIn, double** scores, string names[], int size);
void calculateGrade(double* avgs, char grades[], int size);
void outputResults(string names[], double** scores,double avgs[], char grades[], int size,int NUM_TESTS);
void calculateAverage(double **scores,double avgs[],int NUM_STUDENTS,int NUM_TESTS);
void sortByName(string names[],double avgs[],char grades[],int NUM_STUDENTS);
void countStudents(string names[],char grades[],char gradeLetter,int NUM_STUDENTS);
int main()
{
const int NUM_STUDENTS = 10;
const int NUM_TESTS = 5;
// Declaring variables
string name;
int score;
char ch;
// defines an input stream for the data file
ifstream dataIn;
// Defines an output stream for the data file
ofstream dataOut;
// setting the precision to two decimal places
std::cout << std::setprecision(2) << std::fixed;
// Opening the input file
dataIn.open("testscores.txt");
// checking whether the file name is valid or not
if (dataIn.fail())
{
cout << "** File Not Found **";
return 1;
}
else
{
// Creating 2-D array Dynamically
double** scores = new double*[NUM_STUDENTS];
for (int i = 0; i < NUM_STUDENTS; ++i)
scores[i] = new double[NUM_TESTS];
// Creating array dynamically
string* names = new string[NUM_STUDENTS];
char* grades = new char[NUM_STUDENTS];
double* avgs = new double[NUM_STUDENTS];
// Closing the intput file
dataIn.close();
// calling the functions
readData(dataIn, scores, names, NUM_STUDENTS);
calculateAverage(scores,avgs,NUM_STUDENTS,NUM_TESTS);
calculateGrade(avgs, grades, NUM_STUDENTS);
sortByName(names,avgs,grades,NUM_STUDENTS);
outputResults(names, scores,avgs, grades, NUM_STUDENTS,NUM_TESTS);
cout<<"Enter Grade Letter :";
cin>>ch;
countStudents(names,grades,ch,NUM_STUDENTS);
}
return 0;
}
/* This function will read the data from
* the file and populate the values into arrays
*/
void readData(ifstream& dataIn, double** scores, string names[], int size)
{
// Opening the input file
dataIn.open("testscores.txt");
for (int i = 0; i < size; i++)
{
dataIn >> names[i];
for (int j = 0; j < 5; j++)
{
dataIn >> scores[i][j];
}
}
dataIn.close();
}
/* This function will find the average of
* each student and find the letter grade
*/
void calculateGrade(double* avgs, char grades[], int size)
{
char gradeLetter;
for (int i = 0; i < size; i++)
{
if (avgs[i]>=90 && avgs[i]<=100)
gradeLetter = 'A';
else if (avgs[i]>=80 && avgs[i]<=89.99)
gradeLetter = 'B';
else if (avgs[i]>=70 && avgs[i] <=79.99)
gradeLetter = 'C';
else if (avgs[i]>=60 && avgs[i] <=69.99)
gradeLetter = 'D';
else if (avgs[i]<=59.99)
gradeLetter = 'F';
grades[i] = gradeLetter;
}
}
// This function will display the report
void outputResults(string names[], double** scores,double avgs[], char grades[], int size,int NUM_TESTS)
{
cout << "Name\t\tTest1\tTest2\tTest3\tTest4\tTest5\tAverage\tGrade" << endl;
cout << "\t\t\t\t\t\t\t\t" << endl;
for (int i = 0; i < size; i++)
{
cout <<setw(8)<<left<< names[i] << "\t";
for (int j = 0; j <NUM_TESTS; j++)
{
cout << scores[i][j] << "\t";
}
cout<<avgs[i]<<"\t"<<grades[i] << endl;
}
}
/* calculates the average test score per student
* and stores it as the last column of the test score array
*/
void calculateAverage(double **scores,double avgs[],int NUM_STUDENTS,int NUM_TESTS)
{
double average=0.0,tot=0,totAvg=0,overallAvg=0.0;
for (int i = 0; i < NUM_STUDENTS; i++)
{
tot = 0;
for (int j = 0; j < NUM_TESTS; j++)
{
tot += scores[i][j];
}
average = tot / NUM_TESTS;
avgs[i]=average;
}
}
void sortByName(string names[],double avgs[],char grades[],int NUM_STUDENTS)
{
string tempName;
double tempAvg;
char tempGrade;
//This Logic will Sort the Array of elements in Ascending order
int temp;
for (int i = 0; i < NUM_STUDENTS; i++)
{
for (int j = i + 1; j < NUM_STUDENTS; j++)
{
if (names[i]>names[j])
{
tempName = names[i];
names[i] = names[j];
names[j] = tempName;
tempAvg = avgs[i];
avgs[i] = avgs[j];
avgs[j] = tempAvg;
tempGrade = grades[i];
grades[i] = grades[j];
grades[j] = tempGrade;
}
}
}
}
void countStudents(string names[],char grades[],char gradeLetter,int NUM_STUDENTS)
{
int cnt=0;
for(int i=0;i<NUM_STUDENTS;i++)
{
if(grades[i]==gradeLetter)
{
cnt++;
}
}
cout<<"No of Students Who got the grade letter '"<<gradeLetter<<"' is :"<<cnt<<endl;
}
// testscores.txt
Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 48
Gupta 92 83 30 69 87
Muhammed 23 45 96 38 59
Clark 60 85 45 39 67
Patel 77 31 52 74 83
Abara 93 94 89 77 97
Abebe 79 85 28 93 82
Abioye 85 72 49 75 63
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
// function declarations
void readData(ifstream& dataIn, double** scores, string names[], int size);
void calculateGrade(double* avgs, char grades[], int size);
void outputResults(string names[], double** scores,double avgs[], char grades[], int size,int NUM_TESTS);
void calculateAverage(double **scores,double avgs[],int NUM_STUDENTS,int NUM_TESTS);
void sortByName(string names[],double avgs[],char grades[],int NUM_STUDENTS);
void countStudents(string names[],char grades[],char gradeLetter,int NUM_STUDENTS);
int main()
{
const int NUM_STUDENTS = 10;
const int NUM_TESTS = 5;
// Declaring variables
string name;
int score;
char ch;
// defines an input stream for the data file
ifstream dataIn;
// Defines an output stream for the data file
ofstream dataOut;
// setting the precision to two decimal places
std::cout << std::setprecision(2) << std::fixed;
// Opening the input file
dataIn.open("testscores.txt");
// checking whether the file name is valid or not
if (dataIn.fail())
{
cout << "** File Not Found **";
return 1;
}
else
{
// Creating 2-D array Dynamically
double** scores = new double*[NUM_STUDENTS];
for (int i = 0; i < NUM_STUDENTS; ++i)
scores[i] = new double[NUM_TESTS];
// Creating array dynamically
string* names = new string[NUM_STUDENTS];
char* grades = new char[NUM_STUDENTS];
double* avgs = new double[NUM_STUDENTS];
// Closing the intput file
dataIn.close();
// calling the functions
readData(dataIn, scores, names, NUM_STUDENTS);
calculateAverage(scores,avgs,NUM_STUDENTS,NUM_TESTS);
calculateGrade(avgs, grades, NUM_STUDENTS);
sortByName(names,avgs,grades,NUM_STUDENTS);
outputResults(names, scores,avgs, grades, NUM_STUDENTS,NUM_TESTS);
cout<<"Enter Grade Letter :";
cin>>ch;
countStudents(names,grades,ch,NUM_STUDENTS);
}
return 0;
}
/* This function will read the data from
* the file and populate the values into arrays
*/
void readData(ifstream& dataIn, double** scores, string names[], int size)
{
// Opening the input file
dataIn.open("testscores.txt");
for (int i = 0; i < size; i++)
{
dataIn >> names[i];
for (int j = 0; j < 5; j++)
{
dataIn >> scores[i][j];
}
}
dataIn.close();
}
/* This function will find the average of
* each student and find the letter grade
*/
void calculateGrade(double* avgs, char grades[], int size)
{
char gradeLetter;
for (int i = 0; i < size; i++)
{
if (avgs[i]>=90 && avgs[i]<=100)
gradeLetter = 'A';
else if (avgs[i]>=80 && avgs[i]<=89.99)
gradeLetter = 'B';
else if (avgs[i]>=70 && avgs[i] <=79.99)
gradeLetter = 'C';
else if (avgs[i]>=60 && avgs[i] <=69.99)
gradeLetter = 'D';
else if (avgs[i]<=59.99)
gradeLetter = 'F';
grades[i] = gradeLetter;
}
}
// This function will display the report
void outputResults(string names[], double** scores,double avgs[], char grades[], int size,int NUM_TESTS)
{
cout << "Name\t\tTest1\tTest2\tTest3\tTest4\tTest5\tAverage\tGrade" << endl;
cout << "\t\t\t\t\t\t\t\t" << endl;
for (int i = 0; i < size; i++)
{
cout <<setw(8)<<left<< names[i] << "\t";
for (int j = 0; j <NUM_TESTS; j++)
{
cout << scores[i][j] << "\t";
}
cout<<avgs[i]<<"\t"<<grades[i] << endl;
}
}
/* calculates the average test score per student
* and stores it as the last column of the test score array
*/
void calculateAverage(double **scores,double avgs[],int NUM_STUDENTS,int NUM_TESTS)
{
double average=0.0,tot=0,totAvg=0,overallAvg=0.0;
for (int i = 0; i < NUM_STUDENTS; i++)
{
tot = 0;
for (int j = 0; j < NUM_TESTS; j++)
{
tot += scores[i][j];
}
average = tot / NUM_TESTS;
avgs[i]=average;
}
}
void sortByName(string names[],double avgs[],char grades[],int NUM_STUDENTS)
{
string tempName;
double tempAvg;
char tempGrade;
//This Logic will Sort the Array of elements in Ascending order
int temp;
for (int i = 0; i < NUM_STUDENTS; i++)
{
for (int j = i + 1; j < NUM_STUDENTS; j++)
{
if (names[i]>names[j])
{
tempName = names[i];
names[i] = names[j];
names[j] = tempName;
tempAvg = avgs[i];
avgs[i] = avgs[j];
avgs[j] = tempAvg;
tempGrade = grades[i];
grades[i] = grades[j];
grades[j] = tempGrade;
}
}
}
}
void countStudents(string names[],char grades[],char gradeLetter,int NUM_STUDENTS)
{
int cnt=0;
for(int i=0;i<NUM_STUDENTS;i++)
{
if(grades[i]==gradeLetter)
{
cnt++;
}
}
cout<<"No of Students Who got the grade letter '"<<gradeLetter<<"' is :"<<cnt<<endl;
}
Below is the class diagram. You can very easily create this from Visual Studio. If you need help implementing the methods, let me know.
(I hope you can do a little of this yourself?)
Below is the class diagram. You can very easily create this from Visual Studio. If you need help implementing the methods, let me know.
(I hope you can do a little of this yourself?)
It will provide an instant answer!