这是我上大学的第一年参加Comp课程,在此之前我没有编程经验。我目前正在尝试为类赋值编写一个C程序,它接受4个整数输入,然后显示其中最大和最小的一个,同时还说明了输入最大和最小整数的位置。
注意到:我不允许使用任何函数、数组或循环(除了下面给出的while循环,但不再使用)。
我的教授提供了一个提纲来帮助开始。
这是我的密码:
#include <stdio.h>
int main(void)
{
int x1, x2, x3, x4;
int xlarge, xsmall, ixlarge, ixsmall;
while (1)
{
printf("enter x1, x2, x3, x4:\n");
scanf("%d%d%d%d", &x1, &x2, &x3, &x4);
/* add code to calculate xlarge, xsmall,
* ixlarge, ixsmall
* --> between here */
if ((x1 > x2) && (x1 > x3) && (x1 > x4))
x1 = xlarge;
else if ((x2 > x1) && (x2 > x3) && (x2 > x4))
x2 = xlarge;
else if ((x3 > x1) && (x3 > x2) && (x3 > x4))
x3 = xlarge;
else if ((x4 > x1) && (x4 > x2) && (x4 > x3))
x4 = xlarge;
if ((x1 < x2) && (x1 < x3) && (x1 < x4))
x1 = xsmall;
else if ((x2 < x1) && (x2 < x3) && (x2 < x4))
x2 = xsmall;
else if ((x3 < x1) && (x3 < x2) && (x3 < x4))
x3 = xsmall;
else if ((x4 < x1) && (x4 < x2) && (x4 < x3))
x4 = xsmall;
if (xlarge = x1)
ixlarge = 1;
else if (xlarge = x2)
ixlarge = 2;
else if (xlarge = x3)
ixlarge = 3;
else if (xlarge = x4)
ixlarge = 4;
if (xsmall = x1)
ixsmall = 1;
else if (xsmall = x2)
ixsmall = 2;
else if (xsmall = x3)
ixsmall = 3;
else if (xsmall = x4)
ixsmall = 4;
/* <-- and here */
printf("largest = %4d at position %d, ", xlarge, ixlarge);
printf("smallest = %4d at position %d\n", xsmall, ixsmall);
}
while (1) getchar();
return 0;
}据我所知,如果满足条件,这个程序应该将xlarge和xsmall分配给输入,ixlarge和ixsmall也是如此。但是,我在尝试运行程序时遇到的一个问题是,xlarge和xsmall显然没有初始化,我不知道从哪里开始。
发布于 2019-10-07 04:31:40
查看代码:
它应该是xlarge = x1,而不是x1 = xlarge和类似的xsmall = x1,因为存储x1值的是xlarge,而不是相反。
你也可以把你的“如果-否则”像这样:
if ((x1 > x2) && (x1 > x3) && (x1 > x4))
{
xlarge = x1;
ixlarge = 1;
}
else if ((x2 > x1) && (x2 > x3) && (x2 > x4))
{
xlarge = x2;
ixlarge = 2;
}等等..。
发布于 2019-10-07 04:32:49
当你认为你的问题太难的时候
把你的问题分成小块或者简化你的问题。
1.获取i输入的(1整数)并显示它
2.获取i输入的内容(2个整数)并显示出来
3.比较我在(两个整数)中的键大小,并显示出来。
4.获取我在(3个整数)中键入的内容并显示出来,比较我在(3个整数)中的键(3个整数),并显示它。
5.尝试4个整数
我想告诉你们的不是编程,而是学习方法。
1.
#include <stdio.h>
#include <stdlib.h>
int main() {
int FirstKeyIn;
scanf("%d",&FirstKeyIn);
printf("I key in :%d \n",FirstKeyIn);
return 0;
}2.
#include <stdio.h>
#include <stdlib.h>
int main() {
int FirstKeyIn,SecondKeyIn;
scanf("%d %d",&FirstKeyIn,&SecondKeyIn);
printf("First key in :%d \n",FirstKeyIn);
printf("Second key in :%d \n",SecondKeyIn);
return 0;
}3.
#include <stdio.h>
#include <stdlib.h>
int main() {
int FirstKeyIn,SecondKeyIn;
int Bigger,Smaller;
scanf("%d %d",&FirstKeyIn,&SecondKeyIn);
printf("First key in :%d \n",FirstKeyIn);
printf("Second key in :%d \n",SecondKeyIn);
if(FirstKeyIn>SecondKeyIn){
Bigger=FirstKeyIn;//save FirstKeyIn value into Bigger
Smaller=SecondKeyIn;//save SecondKeyIn value into Smaller
}else{
Bigger=SecondKeyIn;//save SecondKeyIn value into Bigger
Smaller=FirstKeyIn;//save FirstKeyIn value into Smaller
}
printf("Bigger value is :%d \n",Bigger);
printf("Smaller value is :%d \n",Smaller);
return 0;
}4.
#include <stdio.h>
#include <stdlib.h>
int main() {
int FirstKeyIn,SecondKeyIn,ThirdKeyIn;
int Bigger,Smaller;
scanf("%d %d %d",&FirstKeyIn,&SecondKeyIn,&ThirdKeyIn);
printf("First key in :%d \n",FirstKeyIn);
printf("Second key in :%d \n",SecondKeyIn);
printf("Third key in :%d \n",ThirdKeyIn);
if(FirstKeyIn>SecondKeyIn){
Bigger=FirstKeyIn;//save FirstKeyIn value into Bigger
Smaller=SecondKeyIn;//save SecondKeyIn value into Smaller
}else{
Bigger=SecondKeyIn;//save SecondKeyIn value into Bigger
Smaller=FirstKeyIn;//save FirstKeyIn value into Smaller
}
if(ThirdKeyIn>Bigger){
Bigger=ThirdKeyIn;//save ThirdKeyIn value into Bigger
}
if(ThirdKeyIn<Smaller){
Smaller=ThirdKeyIn;//save ThirdKeyIn value into Smaller
}
printf("Bigger value is :%d \n",Bigger);
printf("Smaller value is :%d \n",Smaller);
return 0;
}用最大的,最小的地方
#include <stdio.h>
#include <stdlib.h>
int main() {
int FirstKeyIn,SecondKeyIn,ThirdKeyIn;
int Bigger,Smaller;
int MaxPlace,MinPlace;
scanf("%d %d %d",&FirstKeyIn,&SecondKeyIn,&ThirdKeyIn);
printf("First key in :%d \n",FirstKeyIn);
printf("Second key in :%d \n",SecondKeyIn);
printf("Third key in :%d \n",ThirdKeyIn);
if(FirstKeyIn>SecondKeyIn){
Bigger=FirstKeyIn;//save FirstKeyIn value into Bigger
Smaller=SecondKeyIn;//save SecondKeyIn value into Smaller
MaxPlace=1;
MinPlace=2;
}else{
Bigger=SecondKeyIn;//save SecondKeyIn value into Bigger
Smaller=FirstKeyIn;//save FirstKeyIn value into Smaller
MaxPlace=2;
MinPlace=1;
}
if(ThirdKeyIn>Bigger){
Bigger=ThirdKeyIn;//save ThirdKeyIn value into Bigger
MaxPlace=3;
}
if(ThirdKeyIn<Smaller){
Smaller=ThirdKeyIn;//save ThirdKeyIn value into Smaller
MinPlace=3;
}
printf("Bigger value is :%d \n",Bigger);
printf("Bigger Place is :%d \n",MaxPlace);
printf("Smaller value is :%d \n",Smaller);
printf("Smaller Place is :%d \n",MinPlace);
return 0;
}5.
你自己试试看
https://stackoverflow.com/questions/58263273
复制相似问题