我如何制作一个C程序从用户输入(-232或14之类的整数)并打印用户输入的最大值的整数?
到目前为止,我所知道的是(我的伪代码):
int main(void)
{
int variable;
printf("Enter an integer to check if that is the greatest integer you inputted.")
if %d > variable;
printf("The greatest value you entered is %d")
elif
printf("The greatest value you entered is 'variable'")
scanf("%d", &variable) /Will this command help? IDK
}
我不想要实际的代码,但我需要这样做的步骤/命令。 很抱歉让我好像让别人为我做我的工作。 我刚刚开始使用C而且我对它不太熟悉:(
感谢。
PS程序应该存储并记录输入的最大整数。
答案 0 :(得分:0)
你需要两件事,一件是你要找的,一件是你的最终案例(你什么时候停止寻找)
您正在寻找最大的数字,但是什么时候停止寻找? 10个值后?文件结束后?新线后?
所以在Pseudo代码中就像
int i = 0;
int variable = 0; //Good practice to initialize your variables.
while(When will you stop? i < 10 eg 10 inputs?){
if(your input is > variable){
variable = input;
}
i++; //or whatever your end case is. Have to get closer to the end case.
return variable;
答案 1 :(得分:0)
#include <limits.h>
#include <stdio.h>
int main(void)
{
int greatest = INT_MIN, variable;
FILE *fp;
(fp = fopen("record.txt", "a")) &&
(fp = freopen("record.txt", "r+", fp));
if (!fp) return perror("record.txt"), 1;
fscanf(fp, "%d", &greatest);
printf("Enter an integer to check if that is"
" the greatest integer you inputted. ");
if (scanf("%d", &variable) == 1)
if (variable > greatest)
rewind(fp), fprintf(fp, "%d\n", greatest = variable);
printf("The greatest value you entered is %d\n", greatest);
}