C++ 使用数组-C+调用函数+; //编写一个程序,输入字母等级(a、B、C、D、F)和 //由用户输入(最多50个等级)。成绩将被宣读 //排列成一个数组。函数将被调用五次(每个字母等级调用一次) //并将返回该类别中的年级总数。输入到 //函数将包括数组、数组中的元素数和字母 //类别(A、B、C、D或F)。该程序将打印所需的分数 //是A、B等。 #包括 使用名称空间std; 双计数等级(字符字母类别、整数大小、字符数组); 常数int SIZE=5; 字符letterCategory[SIZE]={'A','B','C','D','F'}; 字符用户字母[50]; int main() { //声明变量 int numberOfGrades=0; int gradeNumbersA=0; int gradeNumbersB=0; int gradeNumbersC=0; int gradeNumbersD=0; int gradeNumbersF=0; //获取要读取的分数 cout>等级数; //输入验证 如果(numberOfGrades50) { cout>等级数; } 而(numberOfGrades50); cout userlets[i]; } } //输出每个类别中的编号 cout
您不需要switch语句。只需将数组元素中的级别与参数中的级别进行比较。在函数结束之前,不应该返回总数。只要你符合一个等级,你就会回到循环中,所以你停止循环C++ 使用数组-C+调用函数+; //编写一个程序,输入字母等级(a、B、C、D、F)和 //由用户输入(最多50个等级)。成绩将被宣读 //排列成一个数组。函数将被调用五次(每个字母等级调用一次) //并将返回该类别中的年级总数。输入到 //函数将包括数组、数组中的元素数和字母 //类别(A、B、C、D或F)。该程序将打印所需的分数 //是A、B等。 #包括 使用名称空间std; 双计数等级(字符字母类别、整数大小、字符数组); 常数int SIZE=5; 字符letterCategory[SIZE]={'A','B','C','D','F'}; 字符用户字母[50]; int main() { //声明变量 int numberOfGrades=0; int gradeNumbersA=0; int gradeNumbersB=0; int gradeNumbersC=0; int gradeNumbersD=0; int gradeNumbersF=0; //获取要读取的分数 cout>等级数; //输入验证 如果(numberOfGrades50) { cout>等级数; } 而(numberOfGrades50); cout userlets[i]; } } //输出每个类别中的编号 cout,c++,arrays,function,C++,Arrays,Function,您不需要switch语句。只需将数组元素中的级别与参数中的级别进行比较。在函数结束之前,不应该返回总数。只要你符合一个等级,你就会回到循环中,所以你停止循环 //Write a program that will input letter grades (A, B, C, D, F), the number of //which is input by the user (a maximum of 50 grades). The grades will be read //into an a
//Write a program that will input letter grades (A, B, C, D, F), the number of
//which is input by the user (a maximum of 50 grades). The grades will be read
//into an array. A function will be called five times (once for each letter grade)
//and will return the total number of grades in that category. The input to the
//function will include the array, number of elements in the array and the letter
//category (A, B, C, D or F). The program will print the number of grades that
//are A, B, etc.
#include <iostream>
using namespace std;
double countGrade(char letterCategory, int size, char array);
const int SIZE = 5;
char letterCategory[SIZE] = {'A', 'B', 'C', 'D', 'F'};
char userLetters[50];
int main()
{
// Declare Variables
int numberOfGrades = 0;
int gradeNumbersA = 0;
int gradeNumbersB = 0;
int gradeNumbersC = 0;
int gradeNumbersD = 0;
int gradeNumbersF = 0;
// Get the number of grades to be read
cout << "Please input the number of grades to be read in. (1-50): ";
cin >> numberOfGrades;
// Input Validation
if(numberOfGrades < 1 || numberOfGrades > 50)
{
cout << "Error! Invalid Input. Please enter a number between 1 and 50.\n";
cin >> numberOfGrades;
}
while(numberOfGrades < 1 || numberOfGrades > 50);
cout << "All grades must be upper case A B C D or F.\n";
// Get the grade
{
for(int i = 0; i < numberOfGrades; i++)
{
cout << "Input a grade: ";
cin >> userLetters[i];
}
}
// Output the number in each category
cout << "Number of A: " << gradeNumbersA << endl;
cout << "Number of B: " << gradeNumbersB << endl;
cout << "Number of C: " << gradeNumbersC << endl;
cout << "Number of D: " << gradeNumbersD << endl;
cout << "Number of F: " << gradeNumbersF << endl;
return 0;
}
double countGrade(char letterCategory, int size, char array)
{
for(int count = 0; count < 5; count++)
{
int a = 0, b = 0, c = 0, d = 0, f = 0;
switch(array)
{
case 'A':
a++;
return a;
break;
case 'B':
b++;
return b;
break;
case 'C':
c++;
return c;
break;
case 'D':
d++;
return d;
break;
case 'F':
f++;
return f;
break;
}
}
}
对于其他等级也是如此。首先声明函数以获取数组:
getGrade(chargrades[])代码>或<代码>获取等级(字符*等级)
然后用数组名调用它:getGrade(grades)
您可以尝试使用Google
作为开始。这是最基本的C++
int countGrade(char letterCategory, int size, char array[]) {
int total = 0;
for (int count = 0; count < size; count++) {
if (array[count] == letterCategory) {
total++;
}
}
return total;
}
int numberOfGradesA = countGrade('A', numberOfGrades, userLetters);