![]() |
有兩題作業點疑問
7. 輸入資料個數 n ,及 n 筆整數資料, 求出各資料平方的總和.
測試資料: (1) 3 1 2 3 印 1*1+2*2+3*3=14 (2) 5 1 -1 2 -2 0 8. 連續輸入整數資料, 以 999 結束, 求出平均值(浮點數). 測試資料: (1) 1 2 4 999 印 2.333333 (2) 5 -4 3 -2 -1 999 這是我目前寫的(第7) int x,m,n,y,i,l; printf("輸入幾筆資料:"); scanf("%d",&m); printf("輸入幾個數:"); scanf("%d",&n); for(l=1;l<=m;l++) { x=0; for(i=1;i<=n;i++) { printf("input:"); scanf("%d",&x); } } system("PAUSE"); return 0; 我不知道要怎麼把輸入的數字平方並相加 第8題相加後取平均的部份也是不會寫? 麻煩會的大大教一下,謝謝 |
數字平方 直接自己乘自己 就好了
先 printf("輸入幾筆資料:"); scanf("%d",&n); 來得知 資料個數 n (也就是迴圈數) 再用 for 迴圈包含 輸入 n 筆整數資料 + 運算 運算部分要使用一個累積用的變數 //以下個人將 for 改成 do{~}while(); 來實作 #include <cstdlib> #include <iostream> //#include <math.h> using namespace std; int main(int argc, char *argv[]) { int n,num; long total=0; printf("輸入幾筆資料:"); scanf("%d",&n); printf("\nStart input...\n"); do{ scanf("%d",&num); num=num*num; total=total+num; n--; //printf("%d",n); }while(n>0); printf("\nTotal= %ld\n",total); system("PAUSE"); return EXIT_SUCCESS; } To "相加後取平均" 就是 total/n 此時 total 可以用 double printf("%lf",total); 雙精度浮點數 %lf,對應 double 長整數 %ld,對應 long |
這幾行不太懂
int main(int argc, char *argv[]) //#include <math.h> //printf("%d",n); 我記得//不是註解用的嗎,程式部會去執行 |
引用:
懶得刪除而已 :bj375mg: 因為是用 Dev-C++ 的建立新專案方式做出模版 所以才有較詳細的 int main(int argc, char *argv) 你也可以改成 main() 原本說是否有 sqr 的數學涵式 所以引入 #include <math.h> 結果 math.h 裡面好像沒有這個的說... :dcft689kj∼懶得刪除 直接註解化 |
喔,原來如此,
我的8題寫出來了,不過有地方錯誤,不知該怎麼改 #include <stdio.h> #include <stdlib.h> main() { int a,m; float b; a=0; m=0; while(a!=999) { b+=a; m++; printf("請輸入數值:"); scanf("%d",&a); } printf("平均直=%f",b/m); system("PAUSE"); return 0; } 我輸入3個數後,在輸入999讓他結束 但是他會把999那次也算進去?? |
改成
do{ }while(a!=999); |
引用:
|
#include <stdio.h>
#include <stdlib.h> main() { int a,m; float b=0; //要養成給初值的動作 a=0; m=-1; //因為do迴圈一開使就 m++ 使 m=1, //當輸入3組數字時 又加上 999 就變成 m=4 //所以改成m=-1或改成下面藍字 do{ b=b+a; m++; printf("請輸入數值:"); scanf("%d",&a); }while(a!=999); b=b/(float)m; //呼應前面 ...b=b/(float)(m-1); printf("平均直=%.0f",b); system("PAUSE"); return 0; } |
可是這題怎麼不會??
#include <stdio.h> main() { int i; float score, sum; sum = 0.0; i = 0; printf("Enter next score (negative to quit ) : "); scanf("%f",&score); while (score >= 0) { sum += score; i++; printf("Enter next score (negative to quit ) : "); scanf("%f",&score); } printf("Total %d scores counted\n",i); printf("Total score : %8.2f \n",sum); printf("Average score : %8.2f \n",sum/i); } |
您那題可以說是標準模式 (有上過正統程式設計老師都會這樣教/寫)
在 迴圈外先執行一次 printf("Enter next score (negative to quit ) : "); scanf("%f",&score); 做輸入 接著迴圈內在執行同樣的 "輸入" 因為外面已經執行一次 有效的輸入 (也就是符合 score >= 0 之輸入) 所以 i=1,代表 有效輸入 之次數為一份 個人的寫法好處很明顯 少寫一次 printf("Enter next score (negative to quit ) : "); scanf("%f",&score); 編譯出來的執行檔體積就會比較小 建議寫作程式設計 因為大部分'玩'家不會真的比照學校教的 畫出流程圖 所以要養成 寫上除錯的顯示 之程式碼 的習慣 比如 printf("%dEnter next score (negative to quit ) : ", i); 這樣您就會了解問題所在 |
那請問為甚麼外面寫一次,裡面還要寫一次阿??
要養成 寫上除錯的顯示 之程式碼 的習慣 是指除錯時的錯誤的部份要用保留嗎 |
Q:那請問為甚麼外面寫一次,裡面還要寫一次阿??
A:因為外面輸入的條件不過關 就不會再執行迴圈裡面的輸入 ,其實寫程式也沒有說一定要怎樣 一定要那樣, 只是那些是前人累積的經驗,自己寫久了也會認同其裡有何在的 Q:要養成 寫上除錯的顯示 之程式碼 的習慣 是指除錯時的錯誤的部份要用保留嗎 A:簡單講就是養成 使用除錯的習慣 |
好像回答完了
天上天下大 你好好加油吧 未來資料結構 系統程式還有長路要走 我一想到OS作業要寫context switch就昏 哀 |
請問程式執行完要讓他選擇要不要在執行一次,要怎麼寫?
如果不用goto的話. |
引用:
大致結構如下 main() { do{ }while(condition); } |
所有時間均為台北時間。現在的時間是 04:24 PM。 |
Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.
『服務條款』
* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *