Ch7_小組研討問題

Similar documents
105A 資管一程式設計實驗 06 函式定義謝明哲老師 2 程式設計實驗 6.3: 自行定義一個可以接受兩個整數並傳回其最大公因數的函式, 接著利用該函式自 行定義一個可以接受兩個整數並傳回其最小公倍數函式 // gcd_fcn.cpp int gcd(int m,

新版 明解C++入門編

untitled

第3章.doc

untitled

FY.DOC

Microsoft PowerPoint - CH07 Arrays and Vectors [相容模式]

Microsoft Office SharePoint Server MOSS Web SharePoint Web SharePoint 22 Web SharePoint Web Web SharePoint Web Web f Lists.asmx Web Web CAML f

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

<ADB6ADB1C25EA8FAA6DB2D4D56432E706466>

Microsoft Word - ch04三校.doc

untitled

CC213

Microsoft Word - 01.DOC

《大话设计模式》第一章

C/C++语言 - C/C++数据

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

EJB-Programming-4-cn.doc

2 WF 1 T I P WF WF WF WF WF WF WF WF 2.1 WF WF WF WF WF WF

untitled

1 1 大概思路 创建 WebAPI 创建 CrossMainController 并编写 Nuget 安装 microsoft.aspnet.webapi.cors 跨域设置路由 编写 Jquery EasyUI 界面 运行效果 2 创建 WebAPI 创建 WebAPI, 新建 -> 项目 ->

CC213

主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new

概述

Microsoft PowerPoint - Class5.pptx

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

Visual Basic D 3D

運算子多載 Operator Overloading

c_cpp

C 1

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

CC213

投影片 1

全国计算机技术与软件专业技术资格(水平)考试

chp6.ppt

提问袁小兵:

C++ 程式設計

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

840 提示 Excel - Excel -- Excel (=) Excel ch0.xlsx H5 =D5+E5+F5+G5 (=) = - Excel 00

C++ 程序设计 OJ9 - 参考答案 MASTER 2019 年 6 月 7 日 1

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

The golden pins of the PCI card can be oxidized after months or years

建模与图形思考

Strings

Java

投影片 1

3.1 num = 3 ch = 'C' 2

Microsoft Word - ACL chapter02-5ed.docx

多媒體應用 13 新增專案並完成版面配置 <ExMusic01> <activity_main.xml> ImageView ID imgplay ImageView ID imgstop ImageView ID imgfront TextView ID txtsong TextView ID t

untitled

Visual C# 2010 與 UML 開發實戰 C# 第 5 章物件導向基礎 C# C# 第 6 章資料與變數 C# 第 7 章判斷式與迴圈 C# 第 8 章陣列與集合 C# 第 9 章偵錯與例外狀況處理 Visual Studio 2010 C# try...catch ix

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

雲端 Cloud Computing 技術指南 運算 應用 平台與架構 10/04/15 11:55:46 INFO 10/04/15 11:55:53 INFO 10/04/15 11:55:56 INFO 10/04/15 11:56:05 INFO 10/04/15 11:56:07 INFO

ebook39-5

1: public class MyOutputStream implements AutoCloseable { 3: public void close() throws IOException { 4: throw new IOException(); 5: } 6:

新・明解C言語入門編『索引』

SDK 概要 使用 Maven 的用户可以从 Maven 库中搜索 "odps-sdk" 获取不同版本的 Java SDK: 包名 odps-sdk-core odps-sdk-commons odps-sdk-udf odps-sdk-mapred odps-sdk-graph 描述 ODPS 基

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

1.JasperReport ireport JasperReport ireport JDK JDK JDK JDK ant ant...6

Chapter 9: Objects and Classes

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

Microsoft PowerPoint - VB14.ppt

untitled

1 C++ 2 Bjarne Stroustrup C++ (system programming) 6 (infrastructure) C++ 7 Herb Sutter 8 C++ (efficiency) (flexibility) 9 (abstraction) (productivity

用手機直接傳值不透過網頁連接, 來當作搖控器控制家電 ( 電視遙控器 ) 按下按鍵發送同時會回傳值來確定是否有送出 問題 :1. 應該是使用了太多 thread 導致在傳值上有問題 2. 一次按很多次按鈕沒辦法即時反應

epub83-1

ebook39-6

mvc

單步除錯 (1/10) 打開 Android Studio, 點選 Start a new Android Studio project 建立專案 Application name 輸入 BMI 點下 Next 2 P a g e

Microsoft Word - CPE考生使用手冊 docx

C/C++ 语言 - 循环

6 C51 ANSI C Turbo C C51 Turbo C C51 C51 C51 C51 C51 C51 C51 C51 C C C51 C51 ANSI C MCS-51 C51 ANSI C C C51 bit Byte bit sbit

任務二 : 產生 20 個有炸彈的磚塊, 放在隨機的位置編輯 Block 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class

Strings

概述

Microsoft Word - 11.doc

untitled

科学计算的语言-FORTRAN95

Microsoft PowerPoint - Class2.pptx

Transcription:

105A 資管一程式設計實驗 08 陣列與向量謝明哲老師 hmz@nttu.edu.tw 1 程式設計實驗 08 陣列與向量 研討問題 05 5.1 使用 call by value 和 call by reference 進行函式呼叫有何差異? 請舉例說明, 為何 swap() 函式必須使用 call by reference 可以有效地將傳入參數的內容互換 ; 而 call by value 則無法達成 5.2 使用 rand() 函式來產生任意 30 個亂數, 並觀察其是否在每次主程式重新執行後亂數產生的順序都會一樣? 若是, 你會如何改善這個問題 5.3 在課堂上老師已經講解了有關選擇排序 (selection sort) 的演算過程, 並以微笑曲線代表對兩個陣列元素進行比較後決定是否執行 swap 運算 假設排序的元素個數為 n, 則選擇排序的微笑曲線數為 n(n-1)/2, 亦即進行比較的次數為 n(n-1)/2 已知 time() 函式可以取得系統時間, 你要如何利用 time() 函式來統計當 n 分別為 100 200 300 時, 電腦執行選擇排序所花費的時間關係 程式設計實驗 8.1: 使用亂數產生器模擬擲同時兩個骰子 提示 1: 使用 MIT App Inventor 設計, 參考以下 UI 設計及 Blocks MIT App Inventor 網址 :http://ai2.appinventor.mit.edu/

105A 資管一程式設計實驗 08 陣列與向量謝明哲老師 hmz@nttu.edu.tw 2 提示 2: 使用 Dev C++ 設計, 參考以下 C++ 程式碼及輸出畫面 // dice.cpp #include <ctime> int dice1, dice2; srand(time(0)); dice1=1 + rand()%6; dice2=1 + rand()%6; cout<<dice1<<" "<<dice2<<endl; system("pause"); return 0; 提示 3: 使用 #develop 設計 SharpDevelop 5 下載網址 :http://www.icsharpcode.net/opensource/sd/download/ 程式設計實驗 8.2: 使用亂數產生器模擬同時擲兩個骰子 100 萬次, 並統計點數和為 2 3 以及 12 出 現的機率 提示 : 參考以下 C++ 程式碼及輸出畫面 // dice_statistic.cpp #include <ctime> #include <cstdlib> int dice1, dice2; int fq[13]=0; srand(time(0));

105A 資管一程式設計實驗 08 陣列與向量謝明哲老師 hmz@nttu.edu.tw 3 for (int i=0; i<1000000; i++) dice1=1 + rand()%6; dice2=1 + rand()%6; fq[dice1+dice2]++; for (int i=2;i<13;i++) cout<<i<<": "<<fq[i]/1000000.0<<endl; system("pause"); return 0; 程式設計實驗 8.3: 使用選擇排序法, 自行定義一個可以將一維整數陣列的元素從小排到大的函式 提示 : 參考以下 C++ 程式碼及輸出畫面 // selection_sort.cpp #include <cstdlib> void selection_sort(int x[], int n) for (int i=0; i<n; i++) for (int j=i+1; j<n; j++) if (x[i]>x[j]) swap(x[i], x[j]); int A[]=7, 35, 40, 19, 13; for (int i=0;i<5;i++) cout<<a[i]<< " "; selection_sort(a, 5); cout<<"after calling selection_sort(a, 5):\n"; for (int i=0;i<5;i++) cout<<a[i]<< " "; system("pause"); return 0; 程式設計實驗 8.4: 使用泡沫排序法, 自行定義一個可以將一維整數陣列的元素從大排到小的函式 提示 : 參考以下 C++ 程式碼及輸出畫面 // bubble_sort.cpp #include <cstdlib> void bubble_sort (int x[ ], int n) for (int i=0;i<n-1;i++) for (int j=0;j<n-1-i;j++) if (x[ j+1]<x[j]) swap(x[j], x[ j+1]); int A[]=7, 35, 40, 19, 13; for (int i=0;i<5;i++) cout<<a[i]<< " ";

105A 資管一程式設計實驗 08 陣列與向量謝明哲老師 hmz@nttu.edu.tw 4 bubble_sort(a, 5); cout<<"after calling bubble_sort(a, 5):\n"; for (int i=0;i<5;i++) cout<<a[i]<< " "; system("pause"); return 0; 程式設計實驗 8.5: 自行定義一個可以將 n n 矩陣加以轉置的函式 提示 : 參考以下 C++ 程式碼及輸出畫面 // transpose.cpp void transpose(int x[][3], int n) for (int i=0;i<n;i++) for (int j=i+1;j<n;j++) swap(x[i][j], x[j][i]); int A[][3]= 1,1,1, 0,1,1, 0,0,1 ; for (int i=0;i<3;i++) for (int j=0;j<3;j++) cout<<a[i][j]<< " "; transpose(a, 3); cout<<"after calling transpose(a, 3):\n"; for (int i=0;i<3;i++) for (int j=0;j<3;j++) cout<<a[i][j]<< " "; system("pause"); return 0; 程式設計實驗 8.6: 使用 vector 向量物件, 重新定義程式設計實驗 8.4 的泡沫排序函式 提示 : 參考以下 C++ 程式碼及輸出畫面 // vector.cpp #include <vector> void bubblesort(vector<int>& x) for (int i=0; i < x.size()-1; i++) for (int j=0; j < x.size()-1-i; j++) if ( x[j+1] < x[j] ) swap(x[j], x[j+1]); vector<int> x(5); for(int i=0; i<x.size(); i++) cin>>x[i]; bubblesort(x); for(int i=0; i<x.size(); i++) cout<<x[i]<< " ";

105A 資管一程式設計實驗 08 陣列與向量謝明哲老師 hmz@nttu.edu.tw 5 system("pause"); return 0; 程式設計實驗 8.7: 讀取實驗 06 產生的成績檔, 載入成績物件後, 應用 C++ Vector 物件或 C# 陣列 物件加以排序, 並顯示排名及成績分布情形 提示 1: 使用 Dev C++ 設計, 參考以下 C++ 程式碼及輸出畫面 #include <iomanip> #include <fstream> #include <vector> class Score public: char name[9]; int programming; int economics; int mis; double average; int rank; ; void swap(score &x, Score &y) Score tmp=x; x=y; y=tmp; void selectionsort(vector<score> &, int, bool); void bubblesort(vector<score> &, int, bool); // int dist[10]=0;. vector<score> grade(50); double max=0, min=100; ifstream fin("grade.csv", ios::in); if (!fin) cout<<" 檔案開啟失敗 "<<endl; else char sentence[81]; fin.getline(sentence, 81, '\n'); double sum=0; int n=0; do fin.getline(grade[n].name,9,','); fin>>grade[n].programming; fin.get(); fin>>grade[n].economics; fin.get(); fin>>grade[n].mis; grade[n].average=(grade[n].programming+grade[n].economics+grade[n].mis)/3.0; if (grade[n].average>max) max = grade[n].average;

105A 資管一程式設計實驗 08 陣列與向量謝明哲老師 hmz@nttu.edu.tw 6 if (grade[n].average<min) min = grade[n].average; sum+=grade[n].average; if (grade[n].average>=90) dist[9]++; else if (grade[n].average>=80) dist[8]++; else if (grade[n].average>=70) dist[7]++; else if (grade[n].average>=60) dist[6]++; else if (grade[n].average>=50) dist[5]++; else if (grade[n].average>=40) dist[4]++; else if (grade[n].average>=30) dist[3]++; else if (grade[n].average>=20) dist[2]++; else if (grade[n].average>=10) dist[1]++; else if (grade[n].average>=0) dist[0]++; n++; while(fin.get()!=eof); fin.close(); cout<<endl<<endl; cout<<"n: "<<n<<endl; cout<<"avg: "<<fixed<<setprecision(2)<<(sum/n)<<endl; cout<<"max: "<<max<<" "<<"Min: "<<min<<endl; cout<<endl<<"-------------------"<<endl; for (int i=9;i>=0;i-=1) cout<<i*10<<"-"<<(i*10+9)<<": "<<dist[i]<<endl; cout<<"-------------------"<<endl; for (int i=0;i<n;i++) cout<<grade[i].name<<" "<<grade[i].programming<<" " <<grade[i].economics<<" "<<grade[i].mis<<endl; selectionsort(grade, 2, false); int k=1; grade[0].rank=k; for (int i=1;i<n;i++) if (grade[i].average < grade[i-1].average) k=i+1; grade[i].rank=k; bubblesort(grade, 1, true); char *strptr; strptr=strtok(sentence,","); while(strptr!=null) cout<<" "<<strptr; strptr=strtok(null,","); cout<<" 總平均名次 \n" <<"---------------------------------------------------------"<<endl; for (int i=0;i<n;i++) cout<<grade[i].name<<" "<<grade[i].programming<<" " <<grade[i].economics<<" "<<grade[i].mis <<" "<<fixed<<setprecision(2)<<grade[i].average <<" "<<grade[i].rank<<endl; cout<<"---------------------------------------------------------"<<endl;

105A 資管一程式設計實驗 08 陣列與向量謝明哲老師 hmz@nttu.edu.tw 7 system("pause"); return 0; void selectionsort(vector<score> &x, int field, bool ascending) cout<<"selectionsort()..."<<endl; for (int i=0;i<x.size()-1;i++) for (int j=i+1;j<x.size();j++) switch(field) case 1: if (ascending) if (strcmp(x[i].name, x[j].name)>0) swap(x[i], x[j]); else if (strcmp(x[i].name, x[j].name)<0) swap(x[i], x[j]); break; case 2: if (ascending) if (x[i].average > x[j].average) swap(x[i], x[j]); else if (x[i].average < x[ j].average) swap(x[i], x[j]); break; void bubblesort(vector<score> &x, int field, bool ascending) cout<<"bubblesort()..."<<endl; for (int i=0;i<x.size()-1;i++) for (int j=0;j<x.size()-i-1;j++) switch(field) case 1: if (ascending) if (strcmp(x[j].name, x[j+1].name)>0) swap(x[j], x[j+1]); else if (strcmp(x[j].name, x[j+1].name)<0) swap(x[j], x[j+1]); break; case 2: if (ascending) if (x[j].average > x[j+1].average) swap(x[j], x[j+1]); else if (x[j].average < x[j+1].average) swap(x[j], x[j+1]); break; 使用 Dev-C++ 進行編譯執行後, 觀察結果 :

105A 資管一程式設計實驗 08 陣列與向量謝明哲老師 hmz@nttu.edu.tw 8 創作指引 : 本模擬問題基礎程式尚未提供個人成績查詢功能, 請規劃並分析如何增加個人成績查詢功 能? 提示 2: 使用 #develop 設計, 參考以下 UI 設計 C# 程式碼 及輸出畫面 SharpDevelop 5 下載網址 :http://www.icsharpcode.net/opensource/sd/download/ 應用 C# 陣列物件加以排序, 並利用視窗元件顯示排名及成績分布情形 建立 C# Windows Application 專案 :GradeManagement 新增 Score 類別 :Score.cs // 自行定義 Score 類別 :Score.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GradeManagement class Score public static string title_id set; get; public static string title_name set; get; public static string title_computer set; get; public static string title_programming set; get; public static string title_accounting set; get; public static string title_economics set; get; public static string title_calculus set; get; public static string title_average set; get; public static string title_rank set; get;

105A 資管一程式設計實驗 08 陣列與向量謝明哲老師 hmz@nttu.edu.tw 9 public string id set; get; public string name set; get; public int computer set; get; public int programming set; get; public int accounting set; get; public int economics set; get; public int calculus set; get; public double average set; get; public int rank set; get; public Score(string aid, string aname, int acomputer, int aprogramming, int aaccounting, int aeconomics, int acalculus) id = aid; name = aname; computer = acomputer; programming = aprogramming; accounting = aaccounting; economics = aeconomics; calculus = acalculus; average = (acomputer + aprogramming + aaccounting + aeconomics + acalculus) / 5.0; rank = -1; 設計主表單 Form1 類別 :Form1.cs(SharpDevelop 預設名稱為 MainForm) // C# 表單類別 :Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace GradeManagement public partial class Form1 : Form public Form1() InitializeComponent(); private StreamReader reader; private Score[] grade = new Score[50]; private void showtitle() textbox2.clear(); string title = String.Format("0,-14 1,-8 2,-2 3,-2 4,-2 5,-3 6,-3 7,-3 8,-2\n", Score.title_id, Score.title_name, Score.title_computer, Score.title_programming, Score.title_accounting, Score.title_economics, Score.title_calculus, Score.title_average, Score.title_rank); textbox2.text += title + Environment.NewLine; private void button1_click(object sender, EventArgs e)

105A 資管一程式設計實驗 08 陣列與向量謝明哲老師 hmz@nttu.edu.tw 10 try reader = new StreamReader(File.OpenRead(textBox1.Text), Encoding.Default); // 顯示中文 Encoding.Default textbox2.clear(); string line = reader.readline(); char[] seps = ','; string[] linetokens = line.split(seps); textbox2.text += line + Environment.NewLine; Score.title_id = linetokens[0]; Score.title_name = linetokens[1]; Score.title_computer = linetokens[2]; Score.title_programming = linetokens[3]; Score.title_accounting = linetokens[4]; Score.title_economics = linetokens[5]; Score.title_calculus = linetokens[6]; Score.title_average = " 平均 "; Score.title_rank = " 排名 "; int n=0; while(!reader.endofstream) line = reader.readline(); textbox2.text += line + Environment.NewLine; linetokens = line.split(seps); grade[n] = new Score( linetokens[0], linetokens[1], Int32.Parse(lineTokens[2]), Int32.Parse(lineTokens[3]), Int32.Parse(lineTokens[4]), Int32.Parse(lineTokens[5]), Int32.Parse(lineTokens[6])); n++; reader.close(); catch (Exception ee) textbox2.text = ee.tostring(); private void button2_click(object sender, EventArgs e) if (reader == null) textbox2.text = " 請先讀取成績檔案 "; else showtitle(); for (int i = 0; i < grade.length; i++) string line = String.Format( "0,-8 1,-6 2:### 3:### 4:### 5:### 6:### 7:###.0", grade[i].id, grade[i].name, grade[i].computer, grade[i].programming, grade[i].accounting, grade[i].economics, grade[i].calculus, grade[i].average); textbox2.text += line + Environment.NewLine;

105A 資管一程式設計實驗 08 陣列與向量謝明哲老師 hmz@nttu.edu.tw 11 private void button3_click(object sender, EventArgs e) if (reader == null) textbox2.text = " 請先讀取成績檔案 "; else Array.Sort(grade, delegate(score score1, Score score2) // 由大排到小 (score2.average - score1.average) return score2.average.compareto(score1.average); ); int k = 1; grade[0].rank = k; for (int i = 1; i < grade.length; i++) if (grade[i].average < grade[i - 1].average) k = i + 1; grade[i].rank = k; Array.Sort(grade, delegate(score score1, Score score2) return score1.id.compareto(score2.id); // 由小排到大 (score1.id - score2.id) ); showtitle(); for (int i = 0; i < grade.length; i++) string line = String.Format( "0,-8 1,-6 2:### 3:### 4:### 5:### 6:### 7:###.0 8:###", grade[i].id, grade[i].name, grade[i].computer, grade[i].programming, grade[i].accounting, grade[i].economics, grade[i].calculus, grade[i].average, grade[i].rank); textbox2.text += line + Environment.NewLine; private void button4_click(object sender, EventArgs e) if (reader == null) textbox2.text = " 請先讀取成績檔案 "; else double max = 0, min = 100; double sum = 0, average = 0; int[] dist = new int[10] 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ; for (int i = 0; i < grade.length; i++) if (grade[i].average > max) max = grade[i].average; if (grade[i].average < min) min = grade[i].average; sum += grade[i].average; if (grade[i].average >= 90) dist[9]++; else if (grade[i].average >= 80) dist[8]++; else if (grade[i].average >= 70) dist[7]++; else if (grade[i].average >= 60) dist[6]++; else if (grade[i].average >= 50) dist[5]++; average = sum / grade.length; textbox2.clear(); textbox2.text += " 班級平均 :" + average + Environment.NewLine; textbox2.text += " 最高分 :" + max + Environment.NewLine; textbox2.text += " 最低分 :" + min + Environment.NewLine;

105A 資管一程式設計實驗 08 陣列與向量謝明哲老師 hmz@nttu.edu.tw 12 for (int i = 9; i >= 0; i -= 1) textbox2.text += i * 10 + "-" + (i * 10 + 9) + ": " + dist[i] + Environment.NewLine; private void timer1_tick(object sender, EventArgs e) label2.text = DateTime.Now.ToString(); private void Form1_Load(object sender, EventArgs e) timer1.enabled = true; 觀察主程式 Program 類別 :Program.cs // C# 主程式類別 :Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace GradeManagement static class Program /// <summary> /// 應用程式的主要進入點 /// </summary> [STAThread] static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); 使用 Visual Studio 2012 或 Sharp Develop 進行編譯執行後, 觀察結果 :

105A 資管一程式設計實驗 08 陣列與向量謝明哲老師 hmz@nttu.edu.tw 13 創作指引 : 本模擬問題基礎程式尚未提供個人成績查詢功能, 請規劃並分析如何增加個人成績查詢功能 使用工具箱提供的 OpenFileDialog 元件提高成績檔案名稱輸入的使用性 使用工具箱提供的 ReportViewer 元件顯示成績分布統計圖