0

I am new to C++, but this picture here is the goal of my program.

This is what I need my input/output to look like:

image

--- INPUT ---

  • The first line of standard input contains an integer 1 ≤ C ≤ 50, the number of test cases.
  • C data sets follow. Each data set begins with an integer, N, the number of people in the class (1 ≤ N ≤ 1000).
  • N integers follow, separated by spaces or newlines, each giving the final grade (an integer between 0 and 100) of a student in the class.

--- OUTPUT ---
For each case you are to output a line giving the percentage of students whose grade is above average, rounded to exactly 3 decimal places.

This is the code that I currently have:

#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using std::vector;

void aboveAverage(int testCases) {
    // initialize number of students for vector size
    int numOfStudents;

    // initialize a vector to hold grades
    vector<int> grades;

    // for # of testCases, recieve # of students per test case
    for(int i = 0; i < testCases; i++) {
        std::cout << "Num of Students: ";
        std::cin >> numOfStudents;

        // per test case, recieve grade per student and push into vector
        for(int j = 0; j < numOfStudents; j++) {
            int grade1;
            std::string grade;

            // debug statement
            std::cout << "Enter grades: ";

            std::getline(std::cin, grade);
            grade = int(grade1);
            grades.push_back(grade1);
        }
    }

    // sum the vector array and intitialize above average threshold
    int sum = std::accumulate(grades.begin(), grades.end(), 0);

    // debug statement
    std::cout << "Sum = " << sum << std::endl;

    int threshold = sum / numOfStudents;

    // initialize a counter and based on threshold get the # of elements that
    // meet that criteria
    int counter = 0;
    for(int j = 0; j < numOfStudents; j++) {
        // if the grade is larger than the threshold, add to counter
        if(grades[j] > threshold) {
            counter += 1;
        }
    }

    // get the percentage of those above average and print it out
    float percentage = (counter / numOfStudents) * 10;
    std::cout << std::setprecision(3) << std::fixed << percentage << std::endl;
}

int main() {
    int testCases;

    // debug statement
    std::cout << "# of Test Cases: ";

    std::cin >> testCases;

    aboveAverage(testCases);

    return 0;
}

The code as a whole runs "fine", I guess you could say. Just no errors in the compiler that yell at me at least. I just cannot, for the life of me, figure out how to set it up exactly like it should for the input. I think I complicated my life with the vector, although it seems easier to me this way. I think I'm close. Hopefully I am!

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
arusty
  • 1
  • 1
    You read to `grade`, but store `grade1` which is uninitialized. Furthermore note that using integer division you'll get yourself in trouble: The result of a division of 2 integral types is a truncated integer (not necessarily of type `int`). Btw: Why read strings? You can read integral types from `std::cin` directly using the `>>` operator. – fabian Oct 14 '21 at 19:07
  • If I were to read them just using std::cin >> grade instead of the getline then I cannot create the whitespaces I need during input – arusty Oct 14 '21 at 19:32
  • What do you mean by "create the whitespaces during input"? I interpret the description of the input in your assignment as "the input is a non empty sequence of numbers seperated by whitespace or newline and at each point you know whether you need to read additional numbers or not". Furthermore the assignment indicates that grading is done automatically, so I'm pretty sure there's some script that will be executed that first compiles your source code and then executes the program redirecting an input file to stdin, so don't need to (and cannot) do any formatting on the input. `./prog < in.txt` – fabian Oct 14 '21 at 19:42
  • Btw: The additional output,like`std::cout<<"Num of Students:";`may result in the script actually failing to consider your solution as correct:It's not part of the output displayed in the assignment.I strongly recommend creating a file containing the sample input and executing your program using this file as input,redirecting the output to a file and verifying the output matches the input exactly(see https://stackoverflow.com/questions/15680530/bash-read-stdin-from-file-and-write-stdout-to-file).You may even create your own bash script for this since you'll probably do this for every assignment – fabian Oct 14 '21 at 19:51
  • What do you mean by this instruction: `grade = int(grade1);` with `grade` being an `std::string`, and `grade1` being an uninitialized `int`? – zkoza Oct 15 '21 at 19:35

0 Answers0