![]() |
![]() |
#1 |
註冊會員
|
![]() 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題相加後取平均的部份也是不會寫? 麻煩會的大大教一下,謝謝 |
![]() |
送花文章: 4,
![]() |
![]() |
#2 (permalink) |
管理版主
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() 數字平方 直接自己乘自己 就好了
先 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 |
![]() |
送花文章: 2027,
![]() |
![]() |
#4 (permalink) | |
管理版主
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() 引用:
懶得刪除而已 ![]() 因為是用 Dev-C++ 的建立新專案方式做出模版 所以才有較詳細的 int main(int argc, char *argv) 你也可以改成 main() 原本說是否有 sqr 的數學涵式 所以引入 #include <math.h> 結果 math.h 裡面好像沒有這個的說... ![]() 直接註解化 |
|
![]() |
送花文章: 2027,
![]() |
![]() |
#5 (permalink) |
註冊會員
|
![]() 喔,原來如此,
我的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那次也算進去?? |
![]() |
送花文章: 4,
![]() |
![]() |
#8 (permalink) |
管理版主
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() #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; } |
![]() |
送花文章: 2027,
![]() |
![]() |
#9 (permalink) |
註冊會員
|
![]() 可是這題怎麼不會??
#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); } |
![]() |
送花文章: 4,
![]() |
![]() |
#10 (permalink) |
管理版主
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() 您那題可以說是標準模式 (有上過正統程式設計老師都會這樣教/寫)
在 迴圈外先執行一次 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); 這樣您就會了解問題所在 |
![]() |
送花文章: 2027,
![]() |