此代码使用Typedef数组返回表单函数的值。
// This program will read in a group of test scores (positive integers from 1 to 100)
// from the keyboard and then calculate and output the average score
// as well as the highest and lowest score. There will be a maximum of 100 scores.
// Najah Johnson
#include <iostream>
using namespace std;
typedef int GradeType[100]; // declares a new data type:
// an integer array of 100 elements
float findAverage(const GradeType, int); // finds average of all grades
int findHighest(const GradeType, int); // finds highest of all grades
int findLowest(const GradeType, int); // finds lowest of all grades
int main()
{
GradeType grades; // the array holding the grades.
int numberOfGrades; // the number of grades read.
int pos; // index to the array.
float avgOfGrades; // contains the average of the grades.
int highestGrade; // contains the highest grade.
int lowestGrade; // contains the lowest grade.
// Read in the values into the array
pos = 0;
cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
cin >> grades[pos];
while (grades[pos] != -99)
{
grades[pos] =
pos ++;
cout << "The total number of grades entered is: " << grades[pos] << endl;
// Fill in the code to read the grades
}
numberOfGrades = pos; // Fill blank with appropriate identifier
// call to the function to find average
avgOfGrades = findAverage(grades, numberOfGrades);
cout << endl << "The average of all the grades is " << avgOfGrades << endl;
highestGrade = findHighest(GradeType, size);
// Fill in the call to the function that calculates highest grade
cout << endl << "The highest grade is " << highestGrade << endl;
lowestGrade = findLowest(GradeType, size);
cout << endl << "The lowest grade is " << lowestGrade << endl;
// Fill in the call to the function that calculates lowest grade
// Fill in code to write the lowest to the screen
return 0;
}
//********************************************************************************
// findAverage
//
// task: This function receives an array of integers and its size.
// It finds and returns the average of the numbers in the array
// data in: array of floating point numbers
// data returned: average of the numbers in the array
//
//********************************************************************************
float findAverage(const GradeType array, int size)
{
float sum = 0; // holds the sum of all the numbers
for (int pos = 0; pos < size; pos++)
sum = sum + array[pos];
return (sum / size); // returns the average
}
//****************************************************************************
// findHighest
//
// task: This function receives an array of integers and its size.
// It finds and returns the highest value of the numbers in
// the array
// data in: array of floating point numbers
// data returned: highest value of the numbers in the array
//
//****************************************************************************
int findHighest(const GradeType array, int size)
{
int highest;
highest = array[0];
for(int count = 0; count < size; count++)
{
if(array[count] > highest)
highest = array[count];
}
return highest;
// Fill in the code for this function
}
//****************************************************************************
// findLowest
//
// task: This function receives an array of integers and its size.
// It finds and returns the lowest value of the numbers in
// the array
// data in: array of floating point numbers
// data returned: lowest value of the numbers in the array
//
//****************************************************************************
int findLowest(const GradeType array, int size)
{
int lowest;
lowest = array[0];
for (int count = 1; count < size; count++)
{
if (array[count] < lowest)
lowest = array[count];
}
return lowest;
// Fill in the code for this function
}
这是我删除的问题的代码的更新版本,以便每个人都可以获取全部要点。这是我的代码遇到的三个问题:
#main.cpp: In function ‘int main()’:
#main.cpp:55:44: error: expected primary-expression before ‘,’ token
highestGrade = findHighest(GradeType, size);
^
#main.cpp:55:46: error: ‘size’ was not declared in this scope
highestGrade = findHighest(GradeType, size);
^~~~
#main.cpp:61:41: error: expected primary-expression before ‘,’ token
lowestGrade = findLowest(GradeType, size);
^
如您所见,它们都与我的函数调用的参数有关。如果有人想知道的话,该程序是C ++。