不知道在我的主代码中放什么以使我的代码正常工作

时间:2018-10-29 18:34:21

标签: c++ function loops

我编写了这段代码,但是不确定要在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 %d\n", min, max);

    scanf("%d", &userInput);
    if(min <= userInput && userInput <= max){
      isValid = true;
}else{
  printf("Sorry, that is not in range\n Please try again\n");
}
  }

  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 number\n";
    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 = %f\n", mean, max, min)

 }

1 个答案:

答案 0 :(得分:1)

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函数,该函数应采用最小,最大,均值,数组和大小变量的const引用。

     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);
}

上述代码的原始错误:

  1. printf("mean = %f max = %f min = %f\n", mean, max, min); //<- typo added semi-colon
  2. const int MIN = 1; //<-typo added semi-colon
  3. 在main中没有声明变量min,max,mean。
  4. const限定符应用于不修改值的函数
  5. 函数
  6. 只能执行一项操作calc / print不能同时执行两项操作。

Demo