You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++课程作业:实现基于数组大小的星号垂直表格

Implementing a Vertical Star Table for Grade Distributions

Got it, let's walk through how to build this vertical star table step by step. You already have the hard parts done—classifying random numbers into vectors and finding the maximum size among them. Now we just need to translate those sizes into a vertical grid of asterisks.

Core Idea

The vertical table will have rows equal to the largest vector size. For each row (starting from the top and working down), we'll check each grade vector: if the vector's size is larger than the current row index, we print an asterisk; otherwise, we print empty space. This way, each grade's column of asterisks will stack up to exactly the size of its vector.

Example Code Implementation

First, let's assume you already have your grade vectors populated. Here's a clean, maintainable way to build the table:

#include <iostream>
#include <vector>
#include <algorithm> // for max_element

using namespace std;

int main() {
    // Assume these vectors are already filled with your graded random numbers
    vector<int> gradeA, gradeB, gradeC, gradeD, gradeF;
    
    // Populate vectors with sample data (replace with your random number logic)
    for (int i = 0; i < 3; ++i) gradeA.push_back(i);
    for (int i = 0; i < 5; ++i) gradeB.push_back(i);
    for (int i = 0; i < 5; ++i) gradeC.push_back(i);
    for (int i = 0; i < 4; ++i) gradeD.push_back(i);
    for (int i = 0; i < 5; ++i) gradeF.push_back(i);

    // Bundle our data for easier iteration
    vector<vector<int>> gradeVectors = {gradeA, gradeB, gradeC, gradeD, gradeF};
    vector<char> gradeLabels = {'A', 'B', 'C', 'D', 'F'};
    vector<int> gradeSizes;

    // Collect sizes of each grade vector
    for (auto& vec : gradeVectors) {
        gradeSizes.push_back(vec.size());
    }

    // Find the maximum size to determine table height
    int maxTableHeight = *max_element(gradeSizes.begin(), gradeSizes.end());

    // Print table header
    cout << "Vertical Grade Distribution\n";
    cout << "---------------------------\n";

    // Print each row from top to bottom
    for (int row = maxTableHeight - 1; row >= 0; --row) {
        for (int col = 0; col < gradeVectors.size(); ++col) {
            cout << gradeLabels[col] << ": ";
            // Print asterisk if this grade has enough elements for this row
            if (gradeSizes[col] > row) {
                cout << "* ";
            } else {
                cout << "  "; // Empty space to keep columns aligned
            }
        }
        cout << endl;
    }

    // Print divider and final counts
    cout << "---------------------------\n";
    for (int size : gradeSizes) {
        cout << "   " << size;
    }
    cout << endl;

    return 0;
}

What This Does

  1. Bundling Data: We put our grade vectors and their labels into containers so we can loop through them instead of writing repetitive code for each grade.
  2. Row Loop: We start from maxTableHeight - 1 (the top row) and go down to 0. This ensures the tallest column of asterisks is at the top, with shorter columns stacking below it.
  3. Column Check: For each column (grade), we check if the vector's size is larger than the current row index. If yes, we print an asterisk—otherwise, empty space to keep columns aligned.
  4. Final Counts: The bottom row shows the exact size of each vector, making it easy to cross-reference with the asterisks.

Optional: Add Visual Separators

If you want a more polished table, you can add vertical dividers between columns. Modify the column print line like this:

cout << gradeLabels[col] << ": ";
if (gradeSizes[col] > row) {
    cout << "* | ";
} else {
    cout << "  | ";
}

This will add a | between each column for clearer separation.

Sample Output

With the sample data in the code, you'll get:

Vertical Grade Distribution
---------------------------
A:    B: *  C: *  D: *  F: * 
A:    B: *  C: *  D: *  F: * 
A: *  B: *  C: *  D: *  F: * 
A: *  B: *  C: *  D: *  F: * 
A: *  B: *  C: *  D:    F: * 
---------------------------
   3    5    5    4    5

This matches exactly what you need—each column of asterisks corresponds to the size of its grade vector, and the table adjusts automatically based on how many elements are in each vector when you run the program.

内容的提问来源于stack exchange,提问作者Vincent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:04:17