一堆变量正在取消初始化.不知道该怎么办
A bunch of variables are being uninitialized. Not sure what to do
需要帮助弄清楚为什么会发生这个错误,我觉得我只是掌握了一些函数和数组。这个问题应该把最多100个等级读入一个数组,计算平均值,找到最高等级,然后找到最低等级。我相信你们只要看代码就能知道。我只是输入剩下的内容,以便能够满足发布代码所需的要求。
-编辑:我想回答所有帮助我解决这个问题的人。我解决了这个问题,从答案中学到了很多东西。现在,我遇到的问题就是弄清楚如何正确地取出最高的数组和最低的数组。我应该能够弄清楚这一点,你们都给了我很大的帮助,我非常感激!
// This program will read in a group of test scores( positive integers from 1 to 100)
// from the keyboard and then calculates and outputs the average score
// as well as the highest and lowest score. There will be a maximum of 100 scores.
// PLACE YOUR NAME HERE
#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)
{
pos++;
cin >> grades[pos];
}
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;
findHighest (grades, numberOfGrades);// Fill in the call to the function that calculates highest grade
cout << endl << "The highest grade is " << highestGrade << endl;
findLowest (grades, numberOfGrades);// Fill in the call to the function that calculates lowest grade
cout << "n The lowest grade is n" << lowestGrade<< "/n"; // 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: avarage 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)
{
float highestgrade;
for (int pos = 0; pos < size; pos++)
if(array[pos]>highestgrade)
{
highestgrade= array[pos];
}
return highestgrade;
// 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)
{
float lowestgrade;
for (int pos =0; pos > size; pos++)
if(array[pos] < lowestgrade)
{
lowestgrade=array[pos];
// Fill in the code for this function
}
return lowestgrade;
}
您的变量并没有被"未初始化"(不管这意味着什么),它们根本没有被初始化。例如:
float lowestgrade;
for (int pos =0; pos > size; pos++)
if(array[pos] < lowestgrade) // <-- lowestgrade hasn't been initialized.
// ...
尝试使用数组的第一个元素初始化有问题的变量:
float lowestgrade = array[0];
for (int pos =0; pos > size; pos++)
if(array[pos] < lowestgrade) // <-- lowestgrade hasn't been initialized.
// ...
您还应该检查size > 0
和执行者是否提前退出(使用默认值)或其他适当的错误处理。
更改这些语句
findHighest (grades, numberOfGrades);// Fill in the call to the function that calculates highest grade
findLowest (grades, numberOfGrades);// Fill in the call to the function that calculates lowest grade
至
highestGrade = findHighest (grades, numberOfGrades);// Fill in the call to the function that calculates highest grade
lowestGrade = findLowest (grades, numberOfGrades);// Fill in the call to the function that calculates lowest grade
同时更改
int findLowest (const GradeType array, int size)
{
float lowestgrade;
for (int pos =0; pos > size; pos++)
if(array[pos] < lowestgrade)
{
lowestgrade=array[pos];
// Fill in the code for this function
}
return lowestgrade;
}
至
int findLowest (const GradeType array, int size)
{
int lowestgrade = array[0];
for (int pos =0; pos < size; pos++)
if(array[pos] < lowestgrade)
{
lowestgrade=array[pos];
// Fill in the code for this function
}
return lowestgrade;
}
和
int findHighest (const GradeType array, int size)
{
float highestgrade;
for (int pos = 0; pos < size; pos++)
if(array[pos]>highestgrade)
{
highestgrade= array[pos];
}
return highestgrade;
// Fill in the code for this function
}
至
int findHighest (const GradeType array, int size)
{
int highestgrade = array[0];
for (int pos = 0; pos < size; pos++)
if(array[pos]>highestgrade)
{
highestgrade= array[pos];
}
return highestgrade;
// Fill in the code for this function
}
相关文章:
- 是否可以初始化不可复制类型的成员变量(或基类)
- C++使用整数的压缩数组初始化对象
- C++初始化基类
- GLEW/GLUT:调用init并创建一个窗口后,取消初始化并重新初始化?
- 为什么"std::uninitialized_copy"通常取消对未初始化内存的迭代器的引用不是未定
- 无法取消引用值初始化迭代器
- 销毁 pthread 互斥体和 C++ 中的取消初始化顺序
- 我可以从取消引用"new"的返回值初始化引用吗
- 删除通过取消引用新对象初始化的对象
- 有没有办法取消初始化类 c++ 中的变量
- 取消初始化内存区域
- 错误:无法取消引用值初始化的矢量迭代器
- 如何正确取消初始化OpenSSL
- 为什么在取消引用结构后结构成员未初始化
- 如何取消初始化 std::实验::可选
- 一堆变量正在取消初始化.不知道该怎么办
- _取消选中取消初始化移动????c++
- C++智能指针取消引用 - 检查它是否已初始化的开销是多少?
- OpenCL - c++包装器-动态库中的上下文取消初始化导致访问冲突
- 如何在类中取消初始化指针"this"?