Question

In: Computer Science

Create a c++ program that: Create an input file using notepad ( .txt ). When testing...

Create a c++ program that:

  1. Create an input file using notepad ( .txt ). When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors.
  2. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File” marker.
  3. Input date file consists of scores (one per line), might contain no score at all (an empty file) and program does not know how many scores before execution.
  4. If file not empty, determine how many valid scores (0 to 100), and how many invalid scores (less than 0 or greater than 100).
  5. Take only the valid scores and determine what letter grade and keep track of how many As, Bs, Cs, Ds, and Fs.  

(90 to 100 is an A, 80 to 89 is a B, 70 to 79 is a C, 60 to 69 is a D, 0 to 60 is a F)

  1. Output to the screen (not file output) with the followings: the number of valid scores, the number of invalid scores, the highest valid score, the lowest valid score, and the average of the valid scores. Also print out the histogram of the grades (sideway). Look at my output examples
  2. If the file is empty, don’t print any statistics at all. If the input file only has invalid scores, just print how many invalid scores and no valid scores, don’t include any statistics.
  3. Test your program with difference cases/scenarios.

Solutions

Expert Solution

Code:

#include <fstream>
#include<iostream>
#include <vector>
using namespace std;

void printHistogram(char ch, int num) {
    printf("[%c]",ch);
      for (int j = 0; j < num; ++j) {
        printf("*");
      }
      printf("\n");
}

int main() {
    vector<int>v;
   ifstream is("a.txt");
   int x;
   if ( is.peek() == ifstream::traits_type::eof() )
  {
   // Empty File
  }else{
        while (is >> x){
            //read integer from file
            v.push_back(x);
        }
        int sum=0;
        vector<int>v2;
        int inv, val, A,B,C,D,F;
        for(int i=0; i<v.size(); i++){
            if(v[i]<0 || v[i]>100){
                inv++;
            }else{
                v2.push_back(v[i]);
                val++;
                sum+=v[i];
                if(v[i]<60){
                    F++;
                }else if(v[i]>=60 && v[i]<70){
                    D++;
                }else if(v[i]>=70 && v[i]<80){
                    C++;
                }else if(v[i]>=80 && v[i]<90){
                    B++;
                }else if(v[i]>=90 && v[i]<=100){
                    A++;
                }
            }
        }
        sort(v2.begin(), v2.end());
        if(val==0){
            cout<<"Invalid scores: "<<inv<<endl;
            cout<<"No valid scores"<<endl;
        }else{
            float avg = sum/v2.size();
            cout<<"Valid scores: "<<val<<endl;
            cout<<"Inalid scores: "<<inv<<endl;
            cout<<"Highest valid score: "<<v2[v2.size()-1]<<endl;
            cout<<"Lowest valid score: "<<v2[0]<<endl;
            cout<<"Average valid score: ";
            printf("%.2f\n",avg);
            printHistogram('A',A);
            printHistogram('B',B);
            printHistogram('C',C);
            printHistogram('D',D);
            printHistogram('F',F);
        }
   }
    
    is.close();
        return 0;
}

Output:


Related Solutions

Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like...
JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to go...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
1. Create a local file on your hard drive by using the NotePad editor that comes...
1. Create a local file on your hard drive by using the NotePad editor that comes with Windows or TextEdit on the MAC. Type into this file some remarks stating that you understand the FTP process. 2. Save it with the name "assign3.txt" 3. Start the FTP program. (SSH for Windows OR FileZilla for MAC) 4. Log on to electron.cs.uwindsor.ca server how do i do this on a mac computer It is intro to internet class
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the range of 0 to 100. There may be repeats. The numbers must not be ordered/sorted. The task is to find and print the two smallest numbers. You must accomplish this task without sorting the file and without using arrays for any purpose. It is possible that the smallest numbers are repeated – you should print the number of occurrences of the two smallest numbers....
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT