我的程序需要使用局部变量而不是全局变量。但是,当我尝试执行此操作时,似乎找不到正确的参数来从main和function来回传递数据。我不断收到错误消息,说“ int的参数类型与float类型的参数不兼容”。请帮助我了解在这里做什么。谢谢您的宝贵时间,谢谢。
我已经尝试过搜索错误代码,但是只找到关于指针问题的答案/问题,而我还没有学过。 我已经工作了几个小时,只是为了使变量在“ int main”中工作,但无济于事。
//This program asks user how many grades there are,
//inputs grades, and displays median of said grades.
//"int main" is at the bottom of the program, preceded by
//variables, function headers, and a single array.
#include <iostream>
using namespace std;
void grdTaker(float [], int);
void sortArray(float[], int);
void median(float[], int);
//Main
int main()
{
//Variables
//int grdsCounted; //Number of grades from user.
const int arraySize = 20;
int grdsCounted; //Number of grades from user.
float grades[arraySize]; //Max grades that can be entered.
grdTaker(grdsCounted, grades[]);
sortArray(grades, grdsCounted);
median(grades, grdsCounted);
system("pause");
}
void grdTaker(float array[], int size) //Function gathers grades.
{
//const int arraySize = 20;
//int grdsCounted; //Number of grades from user.
//float grades[arraySize]; //Max grades that can be entered.
cout << "You may input up to 20 grades. \n";
cout << "First enter the number of grades you have: ";
cin >> grdsCounted;
while (grdsCounted > arraySize)
{
cout << "That is more than 20 grades, try again: \n";
cin >> grdsCounted;
}
cout << "Enter each grade: \n";
//requests how many grades there are and stores them in array
for (int grdCount = 0; grdCount < grdsCounted; grdCount++)
{
cin >> grades[grdCount];
}
};
void sortArray(float array[], int size) //Function sorts array values.
{
bool swap;
float temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void median(float array[], int size) //Outputs the median of entered grades.
{
int med = size / 2;
int odd = med - 1;
cout << "The median grade is: ";
if (size % 2 == 1)
{
cout << array[med] << endl;
}
else
{
cout << (array[med] + array[odd]) / 2 << endl;
}
}
答案 0 :(得分:2)
您分配给浮点数的问题可能是由于在C ++中以这种方式创建了数组。尝试使用new
以c ++的方式声明数组。
您是否想过返回值!看看这个!例如,将第一个功能划分为2!编程始终是将问题分解为小问题。
int numberGradesFromUser() {
int grdsCounted;
int arraySize = 20;
cout << "You may input up to 20 grades. \n";
cout << "First enter the number of grades you have: ";
cin >> grdsCounted;
while (grdsCounted > arraySize)
{
cout << "That is more than 20 grades, try again: \n";
cin >> grdsCounted;
}
return grdsCounted;
}
float* grdTaker(int grdsCounted) //Function gathers grades.
{
float * grades = new float[grdsCounted];
cout << "Enter each grade: \n";
//requests how many grades there are and stores them in array
for (int grdCount = 0; grdCount < grdsCounted; grdCount++)
{
cin >> grades[grdCount];
}
return grades;
};
int main()
{
//Variables
int grdsCounted; //Number of grades from user.
grdsCounted = numberGradesFromUser();
float *gradess = new float[grdsCounted];
sortArray(gradess, grdsCounted);
median(gradess, grdsCounted);
system("pause");
}
有了这个,我想其余的功能应该可以工作。以您的方式调整它们!
此外,优良作法是在标头中声明函数,或者至少在主函数顶部声明函数,而不是在其下方!
答案 1 :(得分:1)
尝试一下:
//This program asks user how many grades there are,
//inputs grades, and displays median of said grades.
//"int main" is at the bottom of the program, preceded by
//variables, function headers, and a single array.
#include <iostream>
using namespace std;
void grdTaker(float [], int, const int);
void sortArray(float[], int);
void median(float[], int);
//Main
int main()
{
//Variables
//int grdsCounted; //Number of grades from user.
const int arraySize = 20;
int grdsCounted; //Number of grades from user.
float grades[arraySize]; //Max grades that can be entered.
grdTaker(grades,arraySize);
sortArray(grades, grdsCounted);
median(grades, grdsCounted);
system("pause");
}
void grdTaker(float array[], const int arraySize) //Function gathers grades.
{
//const int arraySize = 20;
//int grdsCounted; //Number of grades from user.
//float grades[arraySize]; //Max grades that can be entered.
int grdsCounted;
cout << "You may input up to 20 grades. \n";
cout << "First enter the number of grades you have: ";
cin >> grdsCounted;
while (grdsCounted > arraySize)
{
cout << "That is more than 20 grades, try again: \n";
cin >> grdsCounted;
}
cout << "Enter each grade: \n";
//requests how many grades there are and stores them in array
for (int grdCount = 0; grdCount < grdsCounted; grdCount++)
{
cin >> array[grdCount];
}
};
void sortArray(float array[], int size) //Function sorts array values.
{
bool swap;
float temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void median(float array[], int size) //Outputs the median of entered grades.
{
int med = size / 2;
int odd = med - 1;
cout << "The median grade is: ";
if (size % 2 == 1)
{
cout << array[med] << endl;
}
else
{
cout << (array[med] + array[odd]) / 2 << endl;
}
}
说明:我在 grdTaker 函数中添加了 arraySize ,并在其中声明了 grdsCounted 。