Question

In: Computer Science

Comments : For each function in your program, you must include a comment block that includes...

Comments :
For each function in your program, you must include a comment block that includes a name of the function, the input parameters of the function, and the return type of the function. Use short phrase descriptions to describe each.

Guard Conditions and Exception Handling:
This time, your program must guard against common errors. An empty vector cannot be used for a variance or standard deviation. A file must be opened correctly. A file must be written to correctly.


Part 1: The Main and the Menu and the Name on the Output
As with the previous two programs, this project will start with a main.

In addition, it should call a version of a print-your-name function and a menu.


The Menu will contain the following items.


1. Load From a File

2. Calculate the Statistics

3. Write to a File

4. Perform the count

5. Display the Bargraph

6. Quit

Part 2: Load from a File
The first step of this assignment is to read 100,000 numbers from a comma-separated value file.

First, the user will be prompted for the name of the file.

Second, the file must be opened correctly.

Third, all the values must be read into a vector and returned.

Fourth, the file must be closed.

Fifth, the vector must be returned to the Menu function.

Part 3: Calculate the Statistics
Use functions to calculate the total, average, variance, and standard deviation of the numbers in the data vector. These values should be returned and stored in local variables in the menu function.



Part 4: Writing to a File
First, prompt the user for a filename. Second, open a file for output. Then, write the information to the file such that it looks like the following:


Your Name
---------------
Total : 0.00
Average : 0.00
Variance: 0.00
StDev : 0.00

Replace Your Name with your name

Replace each 0.00 with the correct value as a double with two-decimal places.

Finally, close the file cleanly.

Part 4: Perform the count.

This next part is a new piece of the programming. Students will have to create a class called Counter, and initialized to 0 in the local scope.

Class Counter will have an integer count for the values 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12.

The class Counter would have 11 integers, one for each value.

A shortcut would be to have a vector of 13 integers, and ignore the indexes of 0 and 1.


· The class should have a constructor that will set all the counters to 0.
· The class should have a function Increment(int index), that will increase a counter by one based on the input parameter of 2 through 12.
· The class should have a function ShowCount(), which will print out a list of all the counters to the screen. Such as:


Sample output; This is just an example; the real counts will be different.

2| 1000
3| 2000
4| 3000
5| 2000
6| 3000
7| 4000
8| 2000
9| 1000
10| 5000
11| 4000
12| 20000

· Finally, the class should have a function Normalize(int index), with a parameter that is the index. For that index, divide the total by 1000, and truncate to an integer. The type-casting (int) works well. Return the integer.


From the menu, use a loop to go through the vector, operate the Increment function, and then show the count.


Part 5: Showing a text-only Bargraph

The final function is the bargraph function. Using a pair of nested loops, show a bargraph of the numbers.


Sample code:

int rowmax=0;
for (i=2;i<=12;i++)
{
cout<<i;
cout<<”|”;
rowmax=myCounter.Normalize(i);
for (j=0;j<rowmax;j++)
{
cout<<”X”;
}
cout<<endl;
}


And the output would look something like the figure below. Be aware that the bar graphs will be different.


2|X
3|XX
4|XXX
5|XX
6|XXX
7|XXXX
8|XX
9|X
10|XXXXX
11|XXXX
12|XXXXXXXXXXXXXXXXXXXXX

the above program is in c++ language and the menu has to be created with a switch function.

theres a file guven of 100,000 numbers ranging from 1 to 12

Solutions

Expert Solution

//find the code below

#include<bits/stdc++.h>
using namespace std;
vector<int> readCsv()
{
   string fname;
   cout<<"enter name of file :";
   cin>>fname;
   fstream fin;
   fin.open(fname);
   string temp,line;
   vector<int> nums;
   while(fin>>temp)
   {
       getline(fin,line);
       stringstream ss(line);
       string word;
       while(getline(ss,word,',')){
           remove(word.begin(),word.end(),' ');
           int n = stoi(word);
           nums.push_back(n);
       }
   }
   fin.close();
   return nums;
}
float total=0,avg=0,variance=0,sd=0;
void calcStats(vector<int> num)
{
   total=0;avg=0;variance=0;sd=0;
   int n = num.size();
   for(int i=0;i<n;i++)
   {
       total+=num[i];
   }
   avg = total/n;
   double sqDiff = 0;
   for(int i=0;i<n;i++)
   {
       sqDiff += (num[i]-avg)*(num[i]-avg);
   }
   variance = sqDiff/n;
   sd = sqrt(variance);
  
  
}
void writeStats(string uname)
{
   string fname;
   cout<<"enter output file name\n";
   cin>>fname;
   fstream fout;
   fout.open(fname,ios::out);
   fout<<uname<<"\n";
   fout<<"---------------------\n";
   fout<<"Total : "<<fixed<<setprecision(2)<<total<<"\n";
   fout<<"Average :"<<fixed<<setprecision(2)<<avg<<"\n";
   fout<<"Variance :"<<fixed<<setprecision(2)<<variance<<"\n";
   fout<<"StDev :"<<fixed<<setprecision(2)<<sd<<"\n";
   fout.close();
}
class Counter{
   public:
   vector<int> cnt;
   Counter()
   {
       for(int i=0;i<13;i++)
       {
       cnt.push_back(0);
       }
   }
   void increment(int idx)
   {
       cnt[idx]++;
   }
   void showCount()
   {
       for(int i=2;i<=12;i++)
       {
           cout<<i<<"| "<<cnt[i]<<"\n";
       }
   }
   void normalize()
   {
       for(int i=2;i<=12;i++)
       {
           cnt[i]/=1000;
       }
   }
};
Counter counter;
void letsCount(vector<int> nums)
{
   int n=nums.size();
   for(int i=0;i<n;i++)
   {
       counter.increment(nums[i]);
   }
}
void displayBar()
{
   for(int i=2;i<=12;i++)
   {
       cout<<i<<"|";
       for(int j=0;j<counter.cnt[i];j++)
       cout<<"X";
       cout<<endl;
   }
}
int main()
{
  
   cout<<"----------MENU--------\n";
   cout<<"1. Load From a File\n";
   cout<<"2. Calculate the Statistics\n";
   cout<<"3. Write to a File\n";
   cout<<"4. Perform the count\n";
   cout<<"5. Display the Bargraph\n";
   cout<<"6. Quit\n\n";
   cout<<"enter your name\n";
   string uname;
   cin>>uname;
   int choice = 0;
   vector<int> nums;
  
   while(1)
   {
       cin>>choice;
       switch(choice)
       {
           case 1: nums.clear(); nums=readCsv(); break;
           case 2: calcStats(nums); break;
           case 3: writeStats(uname); break;
           case 4: letsCount(nums); counter.showCount(); break;
           case 5: counter.normalize(); displayBar(); break;
           case 6: cout<<"Quitting"; return 0;
           default : cout<<"Invalid choice\n";;
       }
   }
   return 0;
}


Related Solutions

Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at...
Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at the top and at least one more good comment later in the program), to process data for the registrar as follows: NOTE: Include prompts. Send all output to the screen. 1. Read in the id number of a student, the number of credits the student has, and the student’s grade point average (this is a number like 3.25). Print the original data right after...
The code that creates this program using Python: Your program must include: You will generate a...
The code that creates this program using Python: Your program must include: You will generate a random number between 1 and 100 You will repeatedly ask the user to guess a number between 1 and 100 until they guess the random number. When their guess is too high – let them know When their guess is too low – let them know If they use more than 5 guesses, tell them they lose, you only get 5 guesses. And stop...
PLEASE COMMENT THE FOLLOWING LEXICAL ANALYSER PROGRAM. Thank you. #include #include #include #include #include using namespace...
PLEASE COMMENT THE FOLLOWING LEXICAL ANALYSER PROGRAM. Thank you. #include #include #include #include #include using namespace std; int isKeyword(char buffer[]){ char keywords[32][10] = {"auto","break","case","char","const","continue","default", "do","double","else","enum","extern","float","for","goto", "if","int","long","register","return","short","signed", "sizeof","static","struct","switch","typedef","union", "unsigned","void","volatile","while"}; int i, flag = 0; for(i = 0; i < 32; ++i){ if(strcmp(keywords[i], buffer) == 0){ flag = 1; break; } } return flag; } int main(){ char ch, buffer[15], operators[] = "+-*/%="; ifstream fin("Text.txt"); int i,j=0; if(!fin.is_open()){ cout<<"error while opening the file\n"; exit(0); } while(!fin.eof()){ ch = fin.get();    for(i =...
The coding for this program to run as described on Python: Your (turtle) program must include:...
The coding for this program to run as described on Python: Your (turtle) program must include: include import turtle on a line after the comments so you can use the various Turtle-related objects and methods. Create a turtle named after your favorite ice cream flavor. Make sure the name has no punctuation and consists only of letters, numbers and the underscore character. Write your python program so your turtle is constantly moving and can be turned left and right using...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include #define END_OF_LIST -999 using namespace std; /* * */ int exercise_1() { int x = 100; int *ptr; // Assign the pointer variable, ptr, to the address of x. Then print out // the 'value' of x and the 'address' of x. (See Program 10-2) return 0; } int exercise_2() { int x = 100; int *ptr; // Assign ptr to the address of...
Please include comments on what you are doing.   Using linked lists, write a Python program that...
Please include comments on what you are doing.   Using linked lists, write a Python program that performs the following tasks: store the records for each college found in the input file - colleges.csv - into a linked list. (File includes name and state data fields) allow the user to search the linked list for a college’s name; display a message indicating whether or not the college’s name was in the database allow the user to enter a state's name and...
Write the following program in java please. a. Include a good comment when you write the...
Write the following program in java please. a. Include a good comment when you write the method described below: Method method1 receives an integer, n, and a real number, d, and returns a real number. If the integer is positive it returns the square root of the real number added to the integer. If the integer is negative or zero it returns the absolute value of the integer multiplied by the real number. b. Write the statements from the main...
Design a one-week corrective exercise program for a friend or client. Your program must include the...
Design a one-week corrective exercise program for a friend or client. Your program must include the following: Have your client fill out the Lower Extremity Functional Index and the Upper Extremity Functional Index. Summarize what the results tell you about your client (you do not need to submit the filled out forms). Conduct an Upper Body Multi-Joint Movement Assessment and a Lower Body Multi-Joint Movement Assessment on your client. Summarize your findings and how these findings influenced your program design....
Design a one-week corrective exercise program for a friend or client. Your program must include the...
Design a one-week corrective exercise program for a friend or client. Your program must include the following: Have your client fill out the Lower Extremity Functional Index and the Upper Extremity Functional Index. Summarize what the results tell you about your client (you do not need to submit the filled out forms). Conduct an Upper Body Multi-Joint Movement Assessment and a Lower Body Multi-Joint Movement Assessment on your client. Summarize your findings and how these findings influenced your program design....
In your responses to your peers’ posts you must provide constructive and insightful comments that go...
In your responses to your peers’ posts you must provide constructive and insightful comments that go beyond that of agree or disagree. Nursing intervention: Deficient knowledge r/t understanding of complex disease condition. ( Ackley, Ladwig & Mackic 2017p.493) Parent explains disease state, identify child needs and verbalize understanding of the disease within seven days of the pediatric appointment. Nursing interventions: 1.Educate parents Educate on different skills needed to manage the child’s care reinforce learning through frequent repetition and follow up...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT