C二維數組練習

來源:互聯網
上載者:User

標籤:執行個體   c   數組   練習   

這次執行個體的要求是:

* 在n行n列的二維整數數組中,*
按照以下要求選出兩個數。
* 首先從每行中選出最大數,在從選出的n個最大數中選出最小數;*
* 其次,從每行選出最小數,再從選出的n個小數中選出最大數。*

下面就是My Code,在注釋中可以看到我的想法:

#include <stdio.h>/** * 執行個體要求: * 在n行n列的二維整數數組中, * 按照以下要求選出兩個數。 * 首先從每行中選出最大數,在從選出的n個最大數中選出最小數; * 其次,從每行選出最小數,再從選出的n個小數中選出最大數。 * */int main(void){    int order;    printf("%s\n","Please enter the order of the matrix:");    scanf("%d",&order);    printf("Please input the elements of the matrix,from a[0][0] to a[%d][%d]:\n",order-1,order-1);    int matrix[order][order];    /**     * 擷取使用者輸入,並填充到二維數組中     */    int colums,rows;    for(rows = 0;rows < order;rows++){        for(colums = 0; colums < order;colums++){            scanf("%d",&matrix[rows][colums]);            //這裡也可以這樣寫            //scanf("%d",matrix[rows]+colums);        }    }    /**     *  找到最大元素的最小元素     *     */    //用於儲存最大元素中的最小元素    int minInMax = 0;    for(rows = 0;rows < order;rows++){        //用於儲存行最大元素        int maxInLine = 0;        for(colums = 0;colums < order;colums++){            if(matrix[rows][colums] > maxInLine)                maxInLine = matrix[rows][colums];        }        if(rows == 0){            //當擷取到第一行的最大元素時,直接賦值給最小元素            minInMax = maxInLine;        }else{            if(minInMax > maxInLine)                minInMax = maxInLine;        }    }    printf("The minimum of maximum number is %d.\n",minInMax);    /**     *  找到最小元素的最大元素     *     */    //用於儲存最小元素中的最大元素    int maxInMin = 0;    for(rows = 0;rows < order;rows++){        //用於儲存行最小元素        int minInLine = matrix[rows][0];        for(colums = 0;colums < order;colums++){            if(matrix[rows][colums] < minInLine)                minInLine = matrix[rows][colums];        }        if(rows == 0){            //當擷取到第一行的最小元素時,直接賦值給最大元素            maxInMin = minInLine;        }else{            if(maxInMin < minInLine)                maxInMin = minInLine;        }    }    printf("The maximum of minimum number is %d.\n",maxInMin);    return 0;}

C二維數組練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.