簡體   English   中英

顯示 4 個整數中的最大值和最小值,同時顯示它們的位置

[英]Displaying the largest and smallest of 4 integers while also displaying their positions

這是我在大學學習 Comp-Sci class 的第一年,在此 class 之前我沒有編程經驗。 I'm currently trying to write a C program for a class assignment which takes in 4 integer inputs and then displays the largest and smallest of the 4, while also stating the position in which the largest and smallest integers were inputted.

注意:我不允許使用任何函數、arrays 或循環(除了下面給出的 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;
}

據我了解,如果滿足條件,該程序應該將xlargexsmall分配給輸入,對於ixlargeixsmall也是如此。 但是,我在嘗試運行該程序時遇到的一個問題是xlargexsmall顯然未初始化,我不知道從哪里開始。

看代碼:

它應該是xlarge = x1而不是x1 = xlarge並且類似地xsmall = x1因為它是 xlarge 存儲 x1 的值,而不是相反。

你也可以像這樣使用你的 if-else:

if ((x1 > x2) && (x1 > x3) && (x1 > x4))
{
xlarge = x1;
ixlarge = 1;
}
else if ((x2 > x1) && (x2 > x3) && (x2 > x4))
{
xlarge = x2;
ixlarge = 2;
}

等等...

當你認為你的問題太難時

將您的問題分成小塊或簡化您的問題

1.獲取我輸入的內容(1 個整數)並顯示出來

2.獲取我輸入的內容(2個整數)並顯示出來

3.比較我的key in(2 integers) 越來越小並顯示出來

4.獲取我輸入的(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.

自己試試

您需要注意多點。

如果您需要多次運行您的程序,那么您需要一個哨兵控制循環(例如,由諸如是/否或特定重復次數等條件控制),而不是無限循環,即while(1) 如果不是,則不需要while循環。

在程序結束時, getchar()會一直阻塞,直到按下ENTER ,所以你不需要那個while(1)

您可以簡單地使用一個塊在if-else-if中編寫多個語句,這樣您就不必在最后再次檢查索引,例如:

if ((x1 > x2) && (x1 > x3) && (x1 > x4))
{ 
    xlarge = x1; // assign the number to xlarge, not x1 = xlarge
    ixlarge = 1; // assign the position of the number here!!!
}

您正在分配x1 = xlarge; 這是不正確的,因為x1已經包含用戶輸入的值。 該值將被存儲在xlarge中的垃圾值覆蓋,因為它尚未初始化。 它應該是: xlarge = x1; .

if-else中,您不需要檢查最后一個條件,即已經測試了三個備選方案,因此您可以簡單地編寫最后一個結果,而無需在else中對其進行測試。

     if ( condition-1 ) /* do something */
else if ( condition-2 ) /* do something */
else if ( condition-3 ) /* do something */
else                    /* do something if all other conditions are false */

始終初始化您的變量,以便您確定它們位於已知的 state 中,並且不會導致used-before-initialization導致Undefined Behavior

這是一個例子(現場):

#include <stdio.h>

int main(void)
{
    int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
    int xlarge = 0, xsmall = 0, ixlarge = 0, ixsmall = 0;

    printf("Enter x1, x2, x3, x4: ");
    scanf("%d %d %d %d", &x1, &x2, &x3, &x4);

    if ((x1 > x2) && (x1 > x3) && (x1 > x4))
    { 
        xlarge = x1;
        ixlarge = 1;
    }
    else if ((x2 > x1) && (x2 > x3) && (x2 > x4))
    { 
        xlarge = x2;
        ixlarge = 2;
    }
    else if ((x3 > x1) && (x3 > x2) && (x3 > x4))
    { 
        xlarge = x3;
        ixlarge = 3;
    }
    else if ((x4 > x1) && (x4 > x2) && (x4 > x3))
    { 
        xlarge = x4;
        ixlarge = 4;
    }

    if ((x1 < x2) && (x1 < x3) && (x1 < x4))
    {
        xsmall = x1;
        ixsmall = 1;
    }
    else if ((x2 < x1) && (x2 < x3) && (x2 < x4))
    {
        xsmall = x2;
        ixsmall = 2;
    }
    else if ((x3 < x1) && (x3 < x2) && (x3 < x4))
    {
        xsmall = x3;
        ixsmall = 3;
    }
    else if ((x4 < x1) && (x4 < x2) && (x4 < x3))
    {
        xsmall = x4;
        ixsmall = 4;
    }

    printf("Largest  %d @ [index: %d]\n", xlarge, ixlarge);
    printf("Smallest %d @ [index: %d]\n", xsmall, ixsmall);

    return 0;
}

Output:

Enter x1, x2, x3, x4: 1 2 3 4
Largest  4 @ [index: 4]
Smallest 1 @ [index: 1]

如果您被允許使用宏,那么您可以為您的條件編寫:

#define IS_GREATER(n,x,y,z)     ((n > x) && (n > y) && (n > z))
#define IS_SMALLER(n,x,y,z)     ((n < x) && (n < y) && (n < z))

這是一個宏示例: http://ideone.com/G18hci


注意:請記住,您的解決方案不會處理重復元素用例或所有元素都相同的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM