不确定要在我的main中放入什么才能使我的代码正常工作
Not sure what to put in my main to get my code to work
我写了这段代码,但不确定该在我的main((中放什么,如果有人能告诉我该怎么做,我会尽力想办法。这正是我想要做的:
- 程序描述了它应该做什么
- 提示用户输入1到10之间的数字,然后用正浮点数填充数组
- 使用函数输出浮点数组的内容
- 使用函数计算浮点数组的平均值、最大值和最小值。这些值在传递引用变量中返回
- 输出计算值
- 如果用户在任何时候输入了无效输入,请再次提示他们
- 安全终止
这是代码:
//include go here
#include <cstdio>
#include <iostream>
#include <cfloat>
using namespace std;
//Constants go here
const int MAX = 10;
const int MIN = 1
//outputs overview of program to user
void displayOverview();
//prompts user to enter a number between min and max and return it
//validated using a loop
int getIntInRange(int min, int max);
//prompts user to enter a floating point number that is > 0
//validated using a loop
float getPositiveFloat();
//prompts user for size of array (< size)
//fills nums with that many floating point values
int fillArray(float nums[], int size);
//outputs the array
void printArray (float arr[], int Size);
//Computes and returns the mean, maximum, and minimum
void computesValues(float arrr[], int size, float &mean, float &max, float &min);
int main(){
displayOverview();
float myArr[MAX];
int size = fillArray(myArr, MAX);
return 0;
}
//Prompt user to enter a number between Min and max
//If user entered a number within the range, is valid is true
//If user entered a number not within min and max, output sorry not in range
int getIntInRange(int min, int max){
int userInput = -1;
bool isValid = false;
while(!isValid){
printf("Please enter an integer between %d and %dn", min, max);
scanf("%d", &userInput);
if(min <= userInput && userInput <= max){
isValid = true;
}else{
printf("Sorry, that is not in rangen Please try againn");
}
}
return userInput;
}
//int numVals
int fillArray(float nums[], int size){
int numVals = getIntInRange(MIN, MAX);
for(int i=0; i< numVals&& i<size ; i++){
nums[i] = getPositiveFloat();
}
return numVals;
}
//Prompt user to enter a positive number
//if User enters a number that is not positive, output "Not a Positive"
float getPositiveFloat(){
float input;
do{
cout << "Please enter a positive numbern";
cin >> input;
if(!(input>0)){
cout << "Not a positive!n";
}
}while(!(input>0));
return input;
}
//Introduction to the program
void displayOverview(){
cout << "Welcome to my program. You will see how magically I can compute things " <<
"from numbers!!" << endl;
}
//Print an array
void printArray(float arr[], int size){
for (int i = 0; i<size; i++){
cout << arr[i] << " ";
}
}
//Compute Min, max and mean.
void computesValues (float arr[], int size, float &mean, float &max, float &min){
float sum = 0;
for (int i = 0; i<size; i++){
sum = sum + arr[i];
}
mean = sum/size;
max = arr[0];
for (int i = 1; i<size; i++){
if(arr[i] > max)
max = arr[i];
}
min = arr[0];
for (int i = 1; i<size; i++){
if(arr[i] < min)
min = arr[i];
}
printf("mean = %f max = %f min = %fn", mean, max, min)
}
Main不调用来计算数组中的值。
void computesValues (float arr[], int size, float &mean, float &max, float &min)
应该使最后3个Float变量成为函数的本地变量,并将它们从原型和声明中删除:
void void computesValues (float &arr[], int size){ float mean{}, max{}, min{};
您可以调用printArray函数,该函数应采用min、max、mean、array和size变量的常量引用。
void computesValues (float arr[], int size){
float min{}, max{}, mean{};
float sum = 0;
for (int i = 0; i<size; i++){
sum = sum + arr[i];
}
mean = sum/size;
max = arr[0];
for (int i = 1; i<size; i++){
if(arr[i] > max)
max = arr[i];
}
min = arr[0];
for (int i = 1; i<size; i++){
if(arr[i] < min)
min = arr[i];
}
printArray(arr, size, mean, max, min); // call from within your computesValues function
}
//Print an array
void printArray(const float arr[], const int size, const float mean, const float max, const float min){
for (int i = 0; i < size; i++){
cout << arr[i] << " ";
}
printf("mean = %f max = %f min = %f n", mean, max, min);
}
上面代码的原始错误:
printf("mean = %f max = %f min = %fn", mean, max, min); //<- typo added semi-colon
const int MIN = 1; //<-typo added semi-colon
- 没有在main中声明变量min、max和mean
- const限定符应用于不修改值的函数
- 函数应该只做一件事calc/print,而不是两者都做
演示
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 为什么我的代码在输出中增加了93天
- 0-1背包代码中的错误.我的代码中有什么错误
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 在我的代码中,获得最大的Pair Wise产品C++和输出并不总是正确的
- 为什么我的C++代码中出现'Segmentation Fault: 11'行?
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 如何重写全局方法名称以在调用原始方法之前将我的代码推到前面
- 我的代码运行良好,但在游戏循环中中断
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?
- 我认为我的代码很好,但它在 cin a 之后停止并且没有进一步?
- 我的代码中的意外价值以及我如何修复它
- 为什么 rand 不在我的代码中生成随机数?
- 编写了一个C++代码来检查表达式是否具有平衡括号并且我的代码未运行.我已经卡了一天了
- 如何改进我的代码,使其不会因超时而终止?(黑客排名挑战)
- 无法找出我的代码中的内存泄漏
- 我不明白为什么我的代码不起作用并且需要更长的时间来运行
- 我正在尝试创建一个菜单,但我的代码一直在循环