C語言編程入門——數組(下)

來源:互聯網
上載者:User

標籤:c語言   編程   入門   

    這裡講了對數組元素的操作及二維數組與多維陣列,注意,多維陣列在記憶體中也是線性排列存放的。


將數組中的元素倒序排列:

# include <stdio.h>int main(void){int a[7] = {1, 2, 3, 4, 5, 6, 7};  //升序對數組賦值int i, j, t;i = 0;j = 6;while (i < j)  //倒序排列{t = a[i];a[i] = a[j];a[j] = t;++i;--j;}/*for (i=0, j=6; i<j; ++i, --j) //這樣寫也可以,分號為三段分隔,逗號為內部分隔。但這樣寫有些麻煩,可以使用上面的while寫法。{t = a[i];a[i] = a[j];a[j] = t;}*/for (i=0; i<7; ++i)printf("a[%d] = %d\n", i, a[i]);return 0;}


二維數組的賦值與輸出:

# include <stdio.h>int main(void){int a[3][4] = {{1, 2,  3,  4},{5, 6,  7,  8},{9, 10, 11, 12}};                //推薦這樣寫,比較整齊。//輸出多維陣列內容,需要多重迴圈嵌套使用。int i, j;for (i=0; i<3; i++){for (j=0; j<4; j++)printf("%-4d", a[i][j]); //對輸出格式的控制:%-4d中,負號表示靠左對齊,4表示每個元素佔4個位元組。printf("\n");  //輸出分行符號}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.