# 113-1 東華資管程設實習題 week3 2024/09/25 --- [Medium-程式碼詳細解釋](https://medium.com/@11035032_19538/week3-程設實習題-db3ffaa37cef "Medium 程式碼解釋") ***Question.*** ``` - 評估一間公司的存貨流動性可以參考「存貨周轉率」與「存貨周轉天數」這兩個財務指標。請撰寫一支完整的程式,程式當中先後讓使用者輸入一家公司的營業成本、期初存貨與期末存貨(假設皆為帶小數點的數字,單位為萬元),之後再輸出該公司的存貨周轉率與存貨周轉天數 。(讓使用者輸入數值之前請先印出提示字串) - 存貨周轉率 = 營業成本 ÷ 平均存貨 - 平均存貨 = (期初存貨+期末存貨) ÷ 2 - 存貨周轉天數 = 365天 ÷ 存貨周轉率 ``` ***Output.*** ``` 參考執行畫面如下:(粗體字為使用者輸入的) Please input the cost of sales, beginning inventory, ending inventory. 100 20 5 The inventory turnover is 8 The days sales in inventory is 45.625 ``` ***My code.*** ```cpp= #include <iostream> using namespace std; int main() { float cost_of_sales, beginning_inventory, ending_inventory, inventory_turnover, average, days_sales_in_inventory; cout << "Please input the cost of sales, beginning inventory, ending inventory." << endl; cin >> cost_of_sales >> beginning_inventory >> ending_inventory; average = (beginning_inventory + ending_inventory) / 2; inventory_turnover = cost_of_sales / average; days_sales_in_inventory = 365 / inventory_turnover; cout << "The inventory turnover is " << inventory_turnover << "\n"; cout << "The days sales in inventory is " << days_sales_in_inventory; return 0; } ``` ***Reference answer.*** ```cpp= #include <iostream> using namespace std; int main() { double CostOfSales, BeginningInventory, EndingInventory; double inventory_turnover, days_sales_in_inventory; cout << "Please input the cost of sales, beginning inventory, ending inventory.\n"; cin >> CostOfSales >> BeginningInventory >> EndingInventory; double AverageValueOfInventory = (BeginningInventory + EndingInventory) / 2; inventory_turnover = CostOfSales / AverageValueOfInventory; days_sales_in_inventory = 365 / inventory_turnover; cout << "The inventory turnover is " << inventory_turnover << endl; cout << "The days sales in inventory is " << days_sales_in_inventory << endl; return 0; } ``` week5 2024/10/09 --- [Medium-程式碼詳細解釋](https://medium.com/@11035032_19538/week5-程設實習題-d86451ef5acb "Medium 程式碼解釋") ***Question.1*** ``` 吉尼係數(Gini index)為衡量年所得分配公平程度的一個指標。以下圖為例,實際所得分配曲線(紅線)和所得分配絕對平等線(綠線)之間的面積為A,實際所得分配曲線(紅線)和所得分配絕對不平等線(藍 線)之間的面積為B,吉尼係數的算法為。A/(A+B)請撰寫一程式讓使用者輸入A與B的值Lorenz Curve接著計算吉尼係數後輸出於螢幕上。(A與B皆為實數,讓使用者輸入數值之前請先印出提示字串) ``` ***Output.*** ``` 參考執行畫面如下:(粗體字為使用者輸入的) Please input the areas A and B: 21.4 50.5 The Gini Index is 0.297636 ``` ***My code.*** ```cpp= #include <iostream> using namespace std; int main() { double A, B, Gini_index; cout << "Please input the areas A and B:" << "\n"; cin >> A >> B; Gini_index = A / (A + B); cout << "The Gini index is " << Gini_index << "\n"; return 0; } ``` <br/> *** <br/> ***Question.2*** ``` - 請撰寫一支完整的程式,程式當中先後讓使用者輸入身高、體重(假設皆為帶小數點的數字),之後再輸出其使用者的BMI值。(讓使用者輸入數值之前請先印出提示字串,請輸出至小數點後兩位。) - BMI = weight / (height * height) - 假設使用者輸入的身高單位為公尺,體重單位為公斤。 - 不用對輸出格式做限制。 ``` ***Output.*** ``` 參考執行畫面如下:(粗體字為使用者輸入的) How tall are you? 1.81 How much do you weigh? 80.0 Your BMI is 24.42 ``` ***My code.*** ```cpp= #include <iostream> #include <iomanip> using namespace std; int main() { double height, weight, BMI; cout << "How tall are you?" << "\n"; cin >> height; cout << "How much do you weight?" << "\n"; cin >> weight; BMI = weight / (height * height); cout << fixed << setprecision(2); cout << "Your BMI is " << BMI << "\n"; return 0; } ``` <br/> *** <br/> ***Question.3*** ``` 承上題,在輸出使用者的BMI值之後,若使用者的BMI小於18,就輸出 “You are slim.”若是大於或等於18,就輸出 “You are fit.”。 ``` ***Output.*** ``` 參考執行畫面如下:(粗體字為使用者輸入的) How tall are you? 1.81 How much do you weigh? 80.0 Your BMI is 24.42 You are fit. ``` ***My code.*** ```cpp= #include <iostream> #include <iomanip> using namespace std; int main() { double height, weight, BMI; cout << "How tall are you?" << "\n"; cin >> height; cout << "How much do you weight?" << "\n"; cin >> weight; BMI = weight / (height * height); cout << fixed << setprecision(2); cout << "Your BMI is " << BMI << "\n"; if (BMI < 18) { cout << "You are slim.\n"; } else if (BMI > 18 || BMI == 18) { cout << "You are fit.\n"; } else { cout << "error\n"; } return 0; } ``` week6 2024/10/16 --- [Medium-程式碼詳細解釋](https://medium.com/@11035032_19538/week6-程設實習題-5a162147ad7a "Medium 程式碼解釋") ***Question.1*** ``` - 請撰寫一支完整的程式,程式當中先後讓使用者輸入身高、體重( 假設皆為帶小數點的數字),之後再輸出其使用者的BMI值在輸出使用者的BMI值之後,若使用者的BMI介於18與24之間(包含18與24),就輸出 “You are fit.”,不然就輸出 “You are good.”。(讓使用者輸入數值之前請先印出提示字串,請輸出至小數點後兩位。) - BMI = weight / (height * height) - 假設使用者輸入的身高單位為公尺,體重單位為公斤。 ``` ***Output.*** ``` 參考執行畫面如下:(粗體字為使用者輸入的) How tall are you? 1.81 How much do you weigh? 80.0 Your BMI is 22.89 You are fit. 。 ``` ***My code.*** ```cpp= #include <iostream> #include <iomanip> using namespace std; int main() { double height, weight, BMI; cout << "How tall are you?" << "\n"; cin >> height; cout << "How much do you weight?" << "\n"; cin >> weight; BMI = weight / (height * height); cout << fixed << setprecision(2); cout << "Your BMI is " << BMI << "\n"; if (BMI < 18) { cout << "You are slim.\n"; } else if (BMI >= 18 && BMI <= 24) { cout << "You are fit.\n"; } else { cout << "You are good."; } return 0; } ``` <br/> *** <br/> ***Question.2*** ``` 請撰寫一支完整的程式以計算數年後的本利和,該程式一開始先讓使用者輸入目前本金多少,再讓使用者輸入欲知多少年後的本利和,接著輸出算得的本利和(輸出至小數點後兩位);假設本金及本利和皆可為帶小數的數字,年利率為2%,採複利計算。 ``` ***Output.*** ``` Please input account balance and years to maturity: 100 5 Your final balance will be 110.41 ``` ***My code.*** ```cpp= #include <iostream> #include <iomanip> using namespace std; int main() { double account_balance, years, sum; cout << "Please input account balance and years to maturity:" << "\n"; cin >> account_balance >> years; sum = account_balance * pow(1 + 0.02, years); cout << fixed << setprecision(2) << "Your final balance will be " << sum; return 0; } ``` week6 advance question --- ***Question.*** ```今天有一客戶希望你能幫助他設計一個繪圖軟體,條件如下:能夠讓使用者輸入"rectangle"、"triangle"以及"parallelogram",來選擇要繪製何種圖形,另外,讓使用者輸入要繪製幾層的圖形,並輸出結果,為了讓使用者有較好的體驗,需要有提示字串``` ***Output.*** ![pic1](https://hackmd.io/_uploads/H1-bnP5y1g.png) ![pic1](https://hackmd.io/_uploads/H1Wb2D9y1e.png) ![pic1](https://hackmd.io/_uploads/Hk-Z2D9kkx.png) ***My code.*** ```cpp= #include <iostream> using namespace std; int main() { string shape_type; int num; cout << "請問要繪製何種圖形(rectangle/triangle/parallelogram):"; cin >> shape_type; cout << "請問要輸出幾層:"; cin >> num; if (shape_type == "rectangle") { for (int i = 1; i <= num; ++i) { for (int j = 1; j <= num; ++j) { cout << "*"; } cout << "\n"; } } else if (shape_type == "triangle") { for (int i = 1; i <= num; ++i) { for (int j = 1; j <= i; ++j) { cout << "*"; } cout << "\n"; } } else if (shape_type == "parallelogram") { for (int i = 1; i <= num; ++i) { for (int j = 2; j <= i; ++j) { cout << " "; } for (int j = 1; j <= num; ++j) { cout << "*"; } cout << "\n"; } } else { cout << "error"; } return 0; } ``` week7 2024/10/23 --- ***Question.1*** ``` 世界經濟論壇(WEF)自2022年開始發佈各個國家的旅遊與觀光發展指數(Travel & Tourism DevelopmentIndex, TTDI)以評估各國觀光業的發展與永續經營,其為一大於0的實數。請設計一支程式可以讓使用者輸入若干個國家(國家數量由使用者決定)的國名(一個字串)與 TTDI 值,最後輸出這些國家中 TTDI 值最高的國家其國名與 TTDI 值。(讓使用者輸入數值之前,記得先輸出提示語) ``` ***Output*** ``` Please input how many countries do you have. 3 Please input the name and TDDI of each country. Japan 5.3 USA 5.1 Taiwan 5.5 The country with the highest TDDI is Taiwan and its TDDI is 5.5 ``` ***Mycode.*** ```cpp= #include <iostream> #include <string> using namespace std; int main() { int num_country; cout << "Please input how many countries do you have.\n"; cin >> num_country; string max_country = ""; double max_value = 0.0; cout << "Please input the name and TTDI of each country.\n"; for (int i = 1; i <= num_country; i++) { string country; double ttdi_value; cin >> country >> ttdi_value; if (ttdi_value > max_value) { max_value = ttdi_value; max_country = country; } } cout << "The country with the highest TDDI is " << max_country << " and is " << max_value << endl; return 0; } ``` <br/> *** <br/> ***Question.2*** ``` 請為快樂餐廳撰寫一支點餐程式,程式中會讓使用者輸入欲選擇的套餐代號(代號為一個字母),輸入後,若代號為A(或a),則輸出 good choice,若為B (或b) ,則輸出 wonderful choice,若為C (或c) ,則輸出 excellent choice,若為其他字母,則輸出 wrong input!。(請使用if, else if,讓使用者輸入數值之前,記得先輸出提示語) ``` ***Output*** ``` Please input your choice: a good choice ``` ***Mycode.*** ```cpp= #include <iostream> using namespace std; int main() { string choice; cout << "Please input your choice:"; cin >> choice; if (choice == "A" | choice == "a") { cout << "good choice\n"; } else if (choice == "B" | choice == "b") { cout << "wonderful choice\n"; } else if (choice == "C" | choice == "c") { cout << "excellent choice\n"; } else { cout << "wrong input!\n"; } return 0; } ``` week7 hw --- ***Question.1*** ``` Write a C++ program that reads two whole numbers into two variables of type double and then outputs the whole number part of the quotient and the remainder when the first number is divided by the second. ``` ***My code.*** ```cpp= #include <iostream> using namespace std; int main() { int first_num, second_num; double quotient, remainder; cout << "Enter the two whole numbers:\n"; cin >> first_num >> second_num; quotient = first_num / second_num; remainder = first_num % second_num; cout << "Quotient:" << quotient << endl; cout << "Remainder:" << remainder << endl; return 0; } ``` ***Question.2*** ``` Write a C++ program that repeatedly asks a user to input exam scores (in whole numbers). After the user inputs a score, the user determines if the insertion continues by inputting a letter. When ‘y’ or ‘Y’ is input, the insertion continues. Otherwise, the input terminates. Next, this program outputs the sum of these scores. Be sure to prompt for input and label all output. ``` ***My code.*** ```cpp= #include <iostream> using namespace std; int main() { int score, sum = 0; string TF; while (true) { cout << "Please enter the score: "; cin >> score; sum += score; cout << "Continue entering data? (Y/N): "; cin >> TF; if (TF != "Y" && TF != "y") { break; } } cout << "Total sum of scores: " << sum << endl; return 0; } ``` ***Question.3*** ``` Please write a complete program that reads a positive whole number and stores it in variable max. If max is divisible by 2 or 3, increase max by 1 until it is not. For the numbers in the interval 1 to max that are divisible by 2 or 5, compute their average and then output the result. ``` ***My code.*** ```cpp= #include <iostream> using namespace std; int main() { int max; cout << "Enter a positive whole number: "; cin >> max; if (max <= 0) { cout << "Error: Please enter a positive whole number." << endl; return 1; } while (max % 2 == 0 || max % 3 == 0) { max += 1; } int sum = 0; int count = 0; for (int i = 1; i <= max; i++) { if (i % 2 == 0 || i % 5 == 0) { sum += i; count++; } } if (count > 0) { double average = static_cast<double>(sum) / count; cout << "The average of numbers divisible by 2 or 5 from 1 to " << max << " is: " << average << endl; } else { cout << "No numbers in the range are divisible by 2 or 5." << endl; } return 0; } ``` week7 advance --- ***Question.*** ``` 假定一對兔子在它們出生整整兩個月以後可以生一對小兔子,其後每隔一個月又可以再生一對小兔子。假定現在在一個籠子裡有一對剛生下來的小兔子,請問需要多長的時間才會超過1000對兔子? 假設 小兔出生後兩個月就能長成大兔,可以生小兔。 可生育的大兔子都不會累,每個月可以生一對小兔,而且剛好是雄雌各一。 兔子永生不死。 Fn 表示第 n 代兔子的數目。 而F1 = F2 = 1 而當 n≧3 時,Fn = F(n-1) + F(n–2) 所以F1=1 F2=1 F3=2 F4=3 F5=5 F6=8 以此類推 請輸出每個月的兔子對數變化和需要幾個月才會超過1000對兔子 ``` ***Output.*** ![image](https://hackmd.io/_uploads/H1k75r6m1x.png) ***Mycode.*** ```cpp= #include <iostream> using namespace std; int main() { int previous = 1; int current = 1; int month = 2; cout << "F1=1 "; cout << "F2=1 "; do { int next = previous + current; previous = current; current = next; month++; cout << "F" << month << "=" << current << " "; } while (current <= 1000); cout << endl; cout << "要超過1000隻兔子需要" << month << "個月" << endl; return 0; } ``` week 8 2024/10/30 --- ***Question.1*** ``` 請為快樂餐廳撰寫一支點餐程式,程式中會讓使用者輸入欲選擇的套餐代號(代號為一個字母),輸入後,若代號為A(或a),則輸出 good choice,若為B (或b) ,則輸出 wonderful choice,若為C (或c) ,則輸出 excellent choice,若為其他字母,則輸出 wrong input!。(請使用 switch,讓使用者輸入數值之前,記得先輸出提示語) ``` ***Output*** ``` Please input your choice: C excellent choice ``` ***My code.*** ```cpp= #include <iostream> using namespace std; int main() { char choice; cout << "Please input your choice:"; cin >> choice; switch (choice) { case 'A': case 'a': cout << "good choice\n"; break; case 'B': case 'b': cout << "wonderful choice\n"; break; case 'C': case 'c': cout << "excellent choice\n"; break; default: cout << "wrong input!\n"; break; } return 0; } ``` ***參考答案*** ```cpp= #include <iostream> using namespace std; int main() { char choice; cout << "Please input your choice: "; cin >> choice; switch(choice) { case 'A': case 'a': cout << "good choice"; break; case 'B': case 'b': cout << "wonderful choice"; break; case 'C': case 'c': cout << "excellent choice"; break; default: cout << "wrong input"; } return 0; } ``` <br/> *** <br/> ***Question2.*** ``` 請撰寫一支完整的程式,該程式一開始請使用者輸入一個數字,接著會輸出從1到該數字之間的奇數數字(讓使用者輸入數值之前,記得先輸出提示語,請使用for loop) ``` ***Output.*** ```Please input a number: 9 The odd numbers are: 1 3 5 7 9 ``` ***My code.*** ```cpp= #include <iostream> using namespace std; int main() { int num; cout << "Please input a number:"; cin >> num; for (int i = 1; i <= num; i++) { if (i % 2 != 0) { cout << i << endl; } } return 0; } ``` ***參考答案*** ```cpp= #include <iostream> using namespace std; int main() { int number; cout << "Please input a number:\n"; cin >> number; cout << "The odd numbers are:\n"; for(int cnt = 1; cnt <= number; cnt+=2) cout << cnt << endl; return 0; } ``` <br/> *** <br/> ***Question.3*** ``` 請撰寫一支完整的程式,該程式可讓 3 個使用者各自輸入若干個正整數,每個使用者在輸入若干個正整數後,再輸入一個小於或等於 0 的數字代表輸入結束,每個使用者輸入結束後,會輸出該使用者所輸入的數字(不包含最後那個小於或等於 0 的數字)的總合。(第一層 loop 請使用 for loop,第二層 loop 請使用 do while loop,可參考第三章投影片第 99 頁的程式)。 ``` ***Output*** ``` Please input values: 2 3 -1 The sum is 5 Please input values: 2 -1 The sum is 2 Please input values: 3 4 6 -1 The sum is 13 ``` ***Mycode.*** ```cpp= #include <iostream> using namespace std; int main() { int num, sum; for (int i = 1; i <= 3; i++) { sum = 0; cout << "Please input values:\n"; do { cin >> num; if (num > 0) { sum += num; } } while (num > 0); cout << "The sum is " << sum << endl << endl; } return 0; } ``` ***參考答案*** ```cpp= #include <iostream> using namespace std; int main() { for (int Ucnt = 0; Ucnt < 3; Ucnt++) { int Value, Sum = 0; cout << "Please input values:\n"; do { cin >> Value; if (Value > 0) Sum += Value; } while (Value > 0); cout << "The sum is " << Sum << endl << endl; } return 0; } ``` week10 2024/11/20 --- ***Question1.*** ![image](https://hackmd.io/_uploads/BJVbOrpQJx.png) ***Mycode.*** ```cpp= #include <iostream> #include <cmath> using namespace std; int main() { // array變數->nums,可存有三個整數值 int nums[3]; // 讓user輸入value,跑3次,並存入array for (int i = 0; i < 3; i++) { cout << "Please input value\n"; cin >> nums[i]; } // 輸出單數值集合( 0.1.2 place value output ) for (int i = 0; i < 3; i++) { cout << "{" << nums[i] << "}" << endl; } // 輸出雙數值集合 for (int i = 0; i < 3; i++) { for (int j = i + 1; j < 3; j++) { cout << "{" << nums[i] << "," << nums[j] << "}" << endl; } } cout << "{" << nums[0] << "," << nums[1] << "," << nums[2] << "}" << endl; return 0; } ``` <br/> *** <br/> ***Question2.*** ![image](https://hackmd.io/_uploads/rJ-9Or6mkl.png) ***Mycode.*** ```cpp= #include <iostream> #include <vector> #include <sstream> using namespace std; int main() { vector<int> arr; string inputLine; cout << "數字= "; getline(cin, inputLine); stringstream ss(inputLine); int num; while (ss >> num) { arr.push_back(num); } int valueToRemove; cout << "值="; cin >> valueToRemove; vector<int> result; for (size_t i = 0; i < arr.size(); ++i) { if (arr[i] != valueToRemove) { result.push_back(arr[i]); } } cout << "輸出= ["; for (size_t i = 0; i < result.size(); ++i) { cout << result[i]; if (i < result.size() - 1) cout << ", "; } cout << "]" << endl; return 0; } ``` week11 2024/11/20 --- ***Question1.*** ``` 請撰寫一支完整的程式,該程式可讓 3 個使用者各自輸入若干個正整數,每個使用者在輸入若干個正整數後,再輸入一個小於或等於 0 的數字代表輸入結束,每個使用者輸入結束後,會輸出該使用者所輸入的數字(不包含最後那個小於或等於 0 的數字)的總合。(第一層 loop 請使用 for loop,第二層 loop 請使用 do while loop,可參考第三章投影片第 99 頁的程式)。 ``` ***Output.*** ``` Please input values: 2 3 -1 The sum is 5 Please input values: 2 -1 The sum is 2 Please input values: 3 4 6 -1 The sum is 13 ``` ***My code.*** ```cpp= #include <iostream> using namespace std; int main() { int value; for (int count = 1; count <= 3; count++) { int i = 0; cout << "Please input values:\n"; do { cin >> value; if (value > 0) i += value; } while (value > 0); cout << "The sum is " << i << endl; } return 0; } ``` <br/> *** <br/> ***Question2.*** ``` 請撰寫一支完整的程式,該程式一開始請使用者輸入兩個double數字,接著會輸出第一個數字的平方根與第二個數字的絕對值的和(讓使用者輸入數值之前,記得先輸出提示語) ``` ***Output.*** ``` Please input two numbers: 9 -5 The answer is 8 ``` ***My code.*** ```cpp= #include <iostream> using namespace std; int main() { double num1, num2; cout << "Please input two numbers:\n"; cin >> num1 >> num2; // num1 = sqrt(num1); // num2 = abs(num2); //abs->int fabs->double cout << "The answer is " << sqrt(num1) + fabs(num2) << endl; return 0; } ``` week12 2024/11/27 --- ***Question1.*** ``` 請撰寫一支完整程式,程式中有一個function稱為AddFive,其有一個名為Value的int參數,回傳值也是int,在AddFive中,會將Value的值加5之後回傳。在main function中,宣告一個int變數Num,並讓使用者輸入Num的值,隨後呼叫AddFive(將Num當做是參數),之後將AddFive的回傳輸出至螢幕。 ``` ***Output*** ``` Please input a value: 8 The answer is 13 ``` ***Mycode.*** ```cpp= #include <iostream> using namespace std; int AddFive(int Value) { return Value + 5; } int main() { int Num; cout << "Please input a value:\n"; cin >> Num; cout << "The answer is " << AddFive(Num) << endl; return 0; } ``` <br/> *** <br/> ***Question2.*** ``` 請撰寫一支完整的程式,該程式一開始請使用者輸入兩個double數字,然後將兩個數字當做參數傳給Compute這個function,而在Compute中計算第一個數字的ceiling與第二個數字的floor的和後,將此算得的和當做回傳值,最後在main function中輸出該算得的和。(讓使用者輸入數值之前,記得先輸出提示語) ``` ***Output*** ``` Please input two numbers: 9.3 7.2 The answer is 17 ``` ***Mycode.*** ```cpp= #include <iostream> #include <cmath> using namespace std; int Compute(double num1, double num2) { return ceil(num1) + floor(num2); } int main() { double num1, num2; cout << "Please input two numbers:\n "; cin >> num1 >> num2; double result = Compute(num1, num2); cout << "The answer is " << result << endl; return 0; } ``` week12 advance --- ***Question.*** ![image](https://hackmd.io/_uploads/H1Z7TvhNyl.png) ***Output.*** ![image](https://hackmd.io/_uploads/B1OPTD241x.png) ***Mycode.*** ```cpp= #include <iostream> using namespace std; struct Point { int x, y; }; struct Rectangle { Point bottomLeft; Point topRight; }; bool isOverlapping(Rectangle r1, Rectangle r2) { if (r1.topRight.x <= r2.bottomLeft.x || r2.topRight.x <= r1.bottomLeft.x) { return false; } if (r1.topRight.y <= r2.bottomLeft.y || r2.topRight.y <= r1.bottomLeft.y) { return false; } return true; } int main() { int x1, y1, x2, y2, x3, y3, x4, y4; cout << "請輸入4個點的座標: \n"; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; Rectangle r1, r2; r1.bottomLeft.x = x1; r1.bottomLeft.y = y1; r1.topRight.x = x2; r1.topRight.y = y2; r2.bottomLeft.x = x3; r2.bottomLeft.y = y3; r2.topRight.x = x4; r2.topRight.y = y4; if (isOverlapping(r1, r2)) { cout << "yes" << endl; } else { cout << "no" << endl; } return 0; } ``` week13 2024/12/04 --- ***Question1.*** ``` 請撰寫一支完整的程式,當中包含兩個名稱為ave的function,第一個ave function有兩個double參數,回傳值為double值,其會計算兩個參數的平均值並回傳,第二個ave function有三個double參數,回傳值為double值,其會計算三個參數的平均值並回傳。在main function,會先詢問使用者要輸入幾個數字,若使用者輸入為2,則接著讓使用者輸入兩個實數並使用ave function計算出平均值後輸出於螢幕上,若使用者輸入為3,則接著讓使用者輸入兩個實數並使用ave function計算出平均值後輸出於螢幕上,若使用者輸入的值不為2也不為3,則輸出 wrong input 並結束。 ``` ***Output.*** ``` How many values you have? 3 Please enter three values 3.2 4.2 5.1 The average is 4.16667 ``` ***Mycode.*** ```cpp= #include <iostream> using namespace std; double ave(double num1, double num2) { return (num1 + num2) / 2.0; } double ave(double num1, double num2, double num3) { return (num1 + num2 + num3) / 3.0; } int main() { int num; cout << "How many values you have?\n"; cin >> num; if (num == 2) { double num1, num2; cout << "Please enter three values\n"; cin >> num1 >> num2; double result = ave(num1, num2); cout << result << endl; } else if (num == 3) { double num1, num2, num3; cout << "Please enter three values\n"; cin >> num1 >> num2 >> num3; double result = ave(num1, num2, num3); cout << result << endl; } else { cout << "wrong input"; } return 0; } ``` <br/> *** <br/> ***Question2.*** ``` 請撰寫一支程式計算 b2 – 4 × a × c 的值,該程式的演算法有下列三個步驟: 讓使用者輸入變數 a, b, c 的值(皆為整數)計算 b2 – 4 × a × c 的值輸出第二步驟所算得的值於螢幕上請分別將三個步驟各自寫成一個function,然後在 main function 中呼叫這三個function 以完成該程式。 ``` ***Output.*** ``` Please input three values: 5 20 3 The result is 340 ``` ***Mycode.*** ```cpp= #include <iostream> using namespace std; void Input(int &a, int &b, int &c) { cout << "Please input three values:\n "; cin >> a >> b >> c; } int calculate(int a, int b, int c) { return b * b - 4 * a * c; } void Output(int ans) { cout << "The result is " << ans << endl; } int main() { int a, b, c; Input(a, b, c); int ans = calculate(a, b, c); Output(ans); return 0; } ``` <br/> *** <br/> ***Question3.*** ``` 請撰寫一支完整的程式以計算數年後的本利和,該程式有一function稱為Input,其有一個float參數名為Balance,另有一個int參數名為Term,皆為call-by-reference參數,在Input中會讓使用者輸入Balance與Term的值,兩個變數的值分別代表一開始的本金為多少與欲知多少年後的本利和。另有一function稱為Compute,該function有一個float參數名為Balance,另有一個int參數名為Term,皆為call-by-value參數,該function會計算經過Term這麼多年後的本利和,該function有一float的回傳值,其值為算得的本利和。在main function中連續呼叫Input與Compute以求得本利和並輸出於螢幕上,假設本金及本利和皆可為帶小數的數字,年利率為2%。(採複利計算,請使用for loop,輸出至小數點後兩位) ``` ***Output.*** ``` Please input account balance and years to maturity: 100 5 Your final balance will be 110.41 ``` ***Mycode.*** ```cpp= #include <iostream> #include <iomanip> using namespace std; void Input(float &Balance, int &Term) { cout << "Please input account balance and years to maturity:\n"; cin >> Balance >> Term; } float Compute(float Balance, int Term) { const float rate = 0.02; for (int i = 0; i < Term; ++i) { Balance += Balance * rate; } return Balance; } int main() { float Balance; int Term; Input(Balance, Term); float total = Compute(Balance, Term); cout << fixed << setprecision(2); cout << "Your final balance will be " << total << endl; return 0; } ``` week14 2024/12/11 --- ***Question1.*** ``` 請撰寫一完整的程式,其中定義一function名稱為SUM,其formal parameter為兩個double 變數FirstPar跟SecondPar,其回傳值為一double值,在SUM中,會計算FirstPar與SecondPar的和並回傳。另有一function稱為Input,其有兩個double的call-by-reference參數,在Input中會讓使用者輸入兩個數字儲存在兩個參數中。在main function中,會宣告兩double變數First及Second,並呼叫Input讓使用者輸入這兩變數的值,隨後呼叫SUM function,把First、Second當成參數傳給SUM,並把SUM的回傳值輸出至螢幕上。 (輸出、輸入數值之前請先印出提示字串) ``` ***Output.*** ``` Please input two values: 3 5 The sum is 8 ``` ***Mycode.*** ```cpp= #include <iostream> using namespace std; double SUM(double FirstPar, double SecondPar) { return FirstPar + SecondPar; } void Input(double &a, double &b) { cout << "Please input two values:\n"; cin >> a >> b; } int main() { double First, Second, a, b; Input(a, b); double ans = SUM(a, b); cout << "The sum is " << ans << endl; return 0; } ``` <br/> *** <br/> ***Question2.*** ``` 請撰寫一完整程式,將檔案”infile.txt”中的數字(可能含有自然數,即有小數點的數字)全部讀入(infile.txt中有3個數字),然後求出這些數字各自的平方根後,再求這些平方根之平均數,再把下列文字輸出到檔案”outfile.txt”中: mean: (求得的平均數) *需檢查檔案開啟是否成功 ``` ***file.*** <https://elearn4.ndhu.edu.tw/moodle/mod/resource/view.php?id=57347> ***Mycode.*** ```cpp= #include <iostream> #include <fstream> #include <cmath> #include <vector> using namespace std; int main() { ifstream infile("infile.txt"); ofstream outfile("outfile.txt"); if (!infile) { cerr << "無法打開輸入文件!" << endl; return 1; } if (!outfile) { cerr << "無法打開輸出文件!" << endl; return 1; } vector<double> numbers; double num; while (infile >> num) { numbers.push_back(num); } if (numbers.empty()) { cerr << "文件中沒有數字!" << endl; return 1; } for (double n : numbers) { outfile << "數字: " << n << ", 平方根: " << sqrt(n) << endl; } double sum = 0; for (double n : numbers) { sum += n; } double mean = sum / 3; outfile << "平均值: " << mean << endl; infile.close(); outfile.close(); return 0; } ```