如何用数组实现随机分数生成器并自动填充数组完成班级成绩统计?
Hey there! Let's tackle this random score generation problem for your class stats program. I'll walk you through exactly how to replace manual input with auto-generated scores between 60 and 100, and integrate it with your existing code.
First, you need to seed the random number generator to ensure you get different scores every time you run the program. Add the <ctime> header (it provides the time() function we need) and call srand(time(0)) once at the start of your program. This uses the current system time as a unique seed.
The rand() function generates integers from 0 to RAND_MAX (a large value defined in <cstdlib>). To get a number between 60 and 100:
- Calculate the range size:
100 - 60 + 1 = 41(we add 1 to include both 60 and 100) - Use
rand() % 41to get a value between 0 and 40 - Add 60 to shift the range up to 60-100
If you want decimal scores (e.g., 85.5 instead of just integers), you can scale the result to include fractions. For example:60.0 + (rand() % 410) / 10.0 gives scores with one decimal place (60.0 to 100.0)
Here's a complete example that builds on your existing Average function, adds grade counting, and fills the array with random scores:
#include <iostream> #include <cstdlib> #include <ctime> // Required for time() to seed random numbers using namespace std; double Average(double *scores, int N) { double total = 0.0; for (int i = 0; i < N; ++i) { total += scores[i]; } return total / N; } // Helper function to count students in each grade bracket void countGradeLevels(double *scores, int classSize, int& countA, int& countB, int& countC, int& countD) { countA = countB = countC = countD = 0; for (int i = 0; i < classSize; ++i) { if (scores[i] >= 90) countA++; else if (scores[i] >= 80) countB++; else if (scores[i] >= 70) countC++; else if (scores[i] >= 60) countD++; // No need to handle scores below 60 since we generate 60-100 } } int main() { int classSize; cout << "Enter the number of students in the class: "; cin >> classSize; // Dynamically allocate an array to hold scores double *scores = new double[classSize]; // Seed the random number generator srand(time(0)); // Fill the array with random scores (60 to 100) for (int i = 0; i < classSize; ++i) { // Option 1: Integer scores (60, 61, ..., 100) // scores[i] = 60 + rand() % 41; // Option 2: Decimal scores (one decimal place) scores[i] = 60.0 + (rand() % 410) / 10.0; } // Calculate and display average double classAverage = Average(scores, classSize); cout << "\nClass Average: " << classAverage << endl; // Count and display grade distribution int aCount, bCount, cCount, dCount; countGradeLevels(scores, classSize, aCount, bCount, cCount, dCount); cout << "\nGrade Distribution:\n"; cout << "A (90+): " << aCount << "\n"; cout << "B (80-89): " << bCount << "\n"; cout << "C (70-79): " << cCount << "\n"; cout << "D (60-69): " << dCount << "\n"; // Clean up dynamically allocated memory delete[] scores; return 0; }
- Seeding: Always call
srand(time(0))once at the start of your program—don't call it inside the loop, or you'll get repeated numbers. - Dynamic Arrays: If you don't know the class size in advance, using
newto allocate the array is a good approach. Just remember to usedelete[]to free the memory afterward. - Grade Bounds: Adjust the grade thresholds (90 for A, 80 for B, etc.) in the
countGradeLevelsfunction if your school uses different ranges.
内容的提问来源于stack exchange,提问作者Unknown Shin




