C编程查找最小值,最大值和平均值

时间:2015-11-03 23:32:47

标签: c

好的,我编辑了我的程序***我很抱歉,如果格式不正确(我不知道如何做到这一点,让每个人都更容易阅读:(我经历了修复所有内容)可以,但它仍然给我错误。

#include <stdio.h>
#include <stdlib.h>
#define Max 1
#define Min 0

int getValidInteger(char promptString[], char errorString[], int lowerBound,
        int upperBound);
float averageIntegerArray(int height[], int count);

float printIntegerArray(char heading[], int values[], int count);
int findExtremeInIntegerArray(int minOrMax, int values[], int count);

//Get user input
int main(int argc, char** argv) {

//Get the number of scores
  int n = getValidInteger("Welcome to the Statisticamator! \n"
          "How many heights would you like to enter (1-100)?",
          "Number of scores must be 1 or higher\n", 1, 100);
  printf("N is %d\n", n);

//Read in information from the user
  int height[n];
  for (int i = 0; i < n; i++) {
    height[i] = getValidInteger("Enter height (0 to 100):",
            "Height must be between 0 and 100", 0, 100);
  }

//Prints average heights 
  printf("\n");
  printf("Average Height: %.2f inches\n", averageIntegerArray(height, n));

//prints minimum and maximum
  int min = findExtremeInIntegerArray(Min, values, n);    //ISSUE HERE TOO 
  printf("Minimum value is: %d\n", min);

  int max = findExtremeInIntegerArray(Max, values, n);
  printf("Maximum value is: %d\n", max);

  return (EXIT_SUCCESS);

}

//Average height in array
float averageIntegerArray(int height[], int count) {
  int sum = 0;
  for (int i = 0; i < count; i++) {
    sum += height[i];
  }
  return (float) sum / count;
}

//Get integer input from user
int getValidInteger(char promptString[], char errorString[], int lowerBound,
        int upperBound) {
  int input;

//Prompt user for input
  printf(promptString);
  scanf("%d", &input);

//Validate input

  while (input < lowerBound || input > upperBound) {
    printf("errorString");
    printf("\n");
    printf("promptString");
    scanf("%d", &input);
  }

  return input;

}
//Min and Max values
int findExtremeInIntegerArray(int minOrMax, int values[], int count);

  int Extreme = values[0];

  for (int i = 0; i < count; i++)         //THIS IS WHERE IM HAVING 1 ISSUE
          {
    if ((values[i] < Extreme && minOrMax == Min) || (values[i] > Extreme && `enter code here`minOrMax == Max)) {
      Extreme = values[i];
    }

  }
  return (Extreme);
}

1 个答案:

答案 0 :(得分:4)

你应该在&#34;之后添加一个参数。在以下两个陈述中。

printf("Your minimum number is: %d inches \n");
printf("Your maximum number is: %d inches \n");

如果你想打印3个变量,你将有三个格式说明符。

printf("Your maximum number is: %d inches %d feet %d miles \n", inches, feet, miles);

格式说明符是占位符。警告告诉你的是你没有在那个地方给它编号。 %d本身就是在闲逛。