html5-img
1 / 10

Introduction to C Programming

Introduction to C Programming. Homework 4. Homework 4-1. 說明 反轉 N x N 的矩陣,每個元素皆為整數, N < 8 先讓使用者輸入 N 值 , 接著,是 N x N 個整數 輸出 此矩陣的轉置矩陣 限定使用 二維陣列 來記錄矩陣的值 規定以 void swap( int *, int *) 做數字的交換 Sample input 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Sample output 1 5 9 13 2 6 10 14

quito
Télécharger la présentation

Introduction to C Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to C Programming Homework 4

  2. Homework 4-1 • 說明 • 反轉Nx N的矩陣,每個元素皆為整數,N<8 • 先讓使用者輸入N值,接著,是Nx N個整數 • 輸出此矩陣的轉置矩陣 • 限定使用二維陣列來記錄矩陣的值 • 規定以void swap(int*, int*)做數字的交換 • Sample input 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 • Sample output 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16

  3. Homework 4-2 • 說明 • 同上題要求,但限定使用一維陣列 • Sample input 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 • Sample output 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16

  4. Homework 4-3 • 說明 • 相加兩個Nx N的矩陣,每個元素皆為整數,N<8 • 先讓使用者輸入N值 • 接著,是兩組Nx N個整數分別代表矩陣1、矩陣2 • 輸出兩矩陣相加結果 • 規定以void print_add(矩陣1, 矩陣2, int N) 做矩陣的加總與印出(將二維陣列傳入函式) • Sample input 3 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 • Sample output 101010 101010 101010

  5. Homework 4-4 • 說明 • 維護一整數數列,支援以下操作,數列容量為20個整數 • add: 加一整數加入此數列,輸入為a 整數 • pop: 取出最新加入的整數印出,並將其中數列中刪除,輸入為p • 操作以EOF(End of file)為結束,印出此數列 • 程式開始會輸入五個整數作為初始值 • 數列為空時,不會作pop操作,不須考慮此情況 • Sample input/output(以下底線代表輸出,^Z代表EOF) 1 2 3 4 5 a 10 p 10 p 5 ^Z 1 2 3 4

  6. Homework 4-4 Tips • EOF為End of File • 代表檔案結尾 • 常用來表示輸入的結束 • 在C語言中,若得到輸入EOF,scanf()也會回傳EOF • 例如輸入數字直到EOF結束可用 • while(scanf("%d", &a) != EOF) { … } • 輸入EOF的方法 • Windows為ctrl+z • Unix-like為ctrl+d • Reference • http://zh.wikipedia.org/wiki/%E6%AA%94%E6%A1%88%E7%B5%90%E5%B0%BE

  7. Homework 4-5 • 說明 • 練習使用argc, argv從參數列讀入使用者輸入 • 將main宣告為int main(intargc, char* argv[])即可取用argc與argv • argc為argument count,代表有幾個參數 • argv為argument vector,實際儲存參數的陣列 • 例如,執行test.exe hello world • argc = 3 • argv[0]: test.exe • argv[1]:hello • argv[2]: world • 在此作業,請印出N x N的單位矩陣(Identity matrix),N將從參數列輸入 • 當使用者忘了輸入時請顯示提示訊息: usage: (程式路徑) <size> • Sample input C:\Users\shuming\Desktop\test\bin\Debug\test.exe 3 • Sample output 1 0 0 0 1 0 0 0 1 • Sample input C:\Users\shuming\Desktop\test\bin\Debug\test.exe 3 • Sample output usage: C:\Users\shuming\Desktop\test\bin\Debug\test.exe 3

  8. Homework 4-5 Tips • 要使用參數列輸入,需要在命令提示字元下執行 • 開始->所有程式->附屬應用程式->命令提示字元 • 以拖拉方式將程式路徑填入命令提示字元 • 在路徑後面即可寫入需要的參數 • Code::Blocks與DevC++也可自行填入參數詳見Reference

  9. Homework 4-5 Tips (cont.) • 要將字串轉成數字,你可能會需要atoi()函式 • http://pydoing.blogspot.tw/2010/07/c-atoi.html • Reference • http://www.opencv.org.cn/index.php?title=Main%E5%87%BD%E6%95%B0%E5%8F%82%E6%95%B0argc%EF%BC%8Cargv%E8%AF%B4%E6%98%8E&variant=zh-tw • http://nknucc.nknu.edu.tw/~jwu/c/cpgch10.htm#third • http://pydoing.blogspot.tw/2010/07/c-atoi.html • Code::Blocks: http://forums.codeblocks.org/index.php?topic=12351.0 • Dev C++ • http://kowala21.blogspot.tw/2011/10/dev-c-mainint-argc-char-argv.html

  10. Deadline • 11/15

More Related