[英]Using C to Find Min and Max value using Function
我需要編寫一個程序,用戶可以輸入他們定義的數字,然后程序將嘗試找出最低值和最高值。 我面臨的問題是:
printf
之前等待用戶輸入(數字)您可以在答案中給出提示或更正。 非常感謝你。
#include <stdio.h>
float max(float num1){
float a=0, b;
if(num1 > a){
a=num1;
}
return a;
}
float min(float num2){
float x=100, y;
if(num2 < x ){
x=num2;
}
return num2;
}
int main(){
int times, interval;
float mini, maxi, data_Input;
printf("How many number would you like to type in ? : ");
scanf("%d\n",×);
printf("Type in the number: ");
scanf("%f", &data_Input);
for(interval=2; interval<=times; interval++){
printf("\nType in the number: ");
scanf("%f",&data_Input);
while(data_Input<0){
printf("Invalid Input! Please re-enter the number:");
scanf("%f",&data_Input);
}
while(data_Input>100){
printf("Invalid Input! Please re-enter the number:");
scanf("%f",&data_Input);
}
}
maxi= max(data_Input);
mini= min(data_Input);
printf("The Lowest Number is %.2f\n", mini);
printf("The Highest Number is %.2f\n", maxi);
return 0;
}
Output:
How many number would you like to type in? : 5
70
Type in the number :
Type in the number : 90.7
Type in the number : 99
Type in the number : 30
Type in the number : 50
The Lowest Number is 50.00
The Highest Number is 50.00
好的,問題是在輸入每個連續的數字后你沒有更新 data_input。 你所做的是,將最后一個數字與 0 或 100 進行比較,這在邏輯上是不正確的。 您如何將第一個數字作為輸入,然后在每次連續輸入之后,將其與最小值和最大值進行比較。 這是示例代碼。
#include <stdio.h>
float max(float num1, float num2){
if(num1 > num2){
return num1;
}
return num2;
}
float min(float num1, float num2){
if(num1 < num2){
return num1;
}
return num2;
}
int main(){
int times, interval;
float mini, maxi, data_Input;
printf("How many number would you like to type in ? : ");
scanf("%d\n",×);
printf("Type in the number: ");
scanf("%f", &data_Input);
// the first number will be minimum and maximum
mini = data_Input;
maxi = data_Input;
for(interval=2; interval<=times; interval++){
printf("\nType in the number: ");
scanf("%f",&data_Input);
// make it a composite if condition
while(data_Input<0 || data_Input>100){
printf("Invalid Input! Please re-enter the number:");
scanf("%f",&data_Input);
}
maxi= max(maxi, data_Input);
mini= min(mini, data_Input);
}
printf("The Lowest Number is %.2f\n", mini);
printf("The Highest Number is %.2f\n", maxi);
return 0;
}
該程序會檢查最后一個數字,因為您正在從 for 手鐲中調用最小值和最大值 function,因此您可以在 for 手鐲中調用它們,例如:
for(interval=2; interval<=times; interval++){
printf("\nType in the number: ");
scanf("%f",&data_Input);
while(data_Input<0){
printf("Invalid Input! Please re-enter the number:");
scanf("%f",&data_Input);
}
while(data_Input>100){
printf("Invalid Input! Please re-enter the number:");
scanf("%f",&data_Input);
}
maxi= max(data_Input);
mini= min(data_Input);
}
而不是重寫相同的代碼,您只需在 for 循環中詢問數字並將您的間隔初始化為 1,這樣您的 main 將如下所示:
int main(){
int times, interval;
float mini, maxi, data_Input;
printf("How many number would you like to type in ? : ");
scanf("%d\n",×);
for(interval=1; interval<=times; interval++){
printf("\nType in the number: ");
scanf("%f",&data_Input);
while(data_Input<0){
printf("Invalid Input! Please re-enter the number:");
scanf("%f",&data_Input);
}
while(data_Input>100){
printf("Invalid Input! Please re-enter the number:");
scanf("%f",&data_Input);
}
maxi= max(data_Input);
mini= min(data_Input);
}
printf("The Lowest Number is %.2f\n", mini);
printf("The Highest Number is %.2f\n", maxi);
return 0;
}
解決printf 問題很容易。 由於stdout
是行緩沖的(至少在默認情況下 - 它可以更改),只要在緩沖區中插入換行符,它就會被刷新。 因此,例如,只需在每條消息打印后添加一個換行符
printf("How many number would you like to type in ? : \n");
你會沒事的。
談到min和max的錯誤計算,您的嘗試基本上有兩個大問題:
times
輸入,而只是一個。 事實上,每次您調用scanf
時,您都會覆蓋相同的變量data_Input
而無需與之前的輸入進行任何比較min()
和max()
function 一次。 此外,您嘗試將參數與具有本地存儲且生命周期限制為 function 本身的局部變量進行比較,以便在下一次調用時再次初始化為了在 function 中有一個變量,它只在第一次初始化時可以使用static
關鍵字。 但這不是我建議你解決的問題。
在我看來,您不需要比較功能:每次獲得新輸入時只需更新maxi
和mini
(一次解決上述兩個問題):
int main(){
int times, interval;
float mini, maxi, data_Input;
printf("How many number would you like to type in ? : \n");
scanf("%d",×);
printf("Type in the number: ");
scanf("%f", &data_Input);
maxi = data_Input;
mini = data_Input;
for(interval=2; interval<=times; interval++){
printf("\nType in the number: \n");
scanf("%f",&data_Input);
while(data_Input<0){
printf("Invalid Input! Please re-enter the number:\n");
scanf("%f",&data_Input);
}
while(data_Input>100){
printf("Invalid Input! Please re-enter the number:\n");
scanf("%f",&data_Input);
}
/* Check input and, in case, update max and min */
if(data_Input > maxi)
maxi = data_Input;
if(data_Input < mini)
mini = data_Input;
}
printf("The Lowest Number is %.2f\n", mini);
printf("The Highest Number is %.2f\n", maxi);
return 0;
}
比較在循環內執行,因此您不需要將輸入存儲到數組中。
您的代碼中有很多問題。
min
和max
沒有任何意義scanf
的結果,也不知道它是否成功#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
int main()
{
int times, interval;
float mini, maxi, data_Input;
do
{
printf("\nHow many number would you like to type in ? : ");
}while(scanf(" %d\n",×) != 1);
for(interval = 0; interval < times; interval++)
{
do
{
printf("\nType in the number: ");
}while(scanf(" %f",&data_Input) != 1 && (data_Input < 0 || data_Input > 100));
printf("%f\n", data_Input);
maxi = interval == 0 ? data_Input : MAX(maxi, data_Input);
mini = interval == 0 ? data_Input : MIN(mini, data_Input);
}
printf("The Lowest Number is %.2f\n", mini);
printf("The Highest Number is %.2f\n", maxi);
return 0;
}
當程序執行時,第二行將在 printf 之前等待用戶輸入(數字)
從"%d\n"
中刪除"\n"
n" 。 它會阻塞,直到在數字后檢測到非空白並且不需要為止。
printf("How many number would you like to type in ? : ");
// scanf("%d\n",×);
scanf("%d",×);
printf("Type in the number: ");
如果 output 在預期時仍未出現,請刷新它。 通常打印'\n'
會刷新stdout
,但不一定。
printf("How many number would you like to type in ? : ");
fflush(stdout); // add
錯誤“系統”似乎不可靠,有時有效,有時無效
至少這個問題:順序檢查與檢查兩個條件一起輸入有效性。
而不是
while(data_Input<0){
...
}
while(data_Input>100){
...
}
一起測試。
while(data_Input<0 || data_Input>100){
...
}
該程序只檢查最后一個數字條目,因此它只顯示最小和最大的最后一個數字
為真,因為代碼僅通過調用一次max()
來比較一次值。 function 僅將數字與 0 進行比較,而不是將先前的值進行比較。 同樣對於min()
。
考慮改變算法
// Pseudo code for max
prompt How many number would you like to type in ? : "
get times
max_value = -INF // set to minimum possible float
for each value 1 to times
prompt "Type in the number: "
get data_Input
if data_Input > max_value
max_value = data_Input
print max_value
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.