วันศุกร์ที่ 20 กันยายน พ.ศ. 2556

=Array=(Eazy)

Programming Exercise
Proposition => Write the code to calculate the average of the element included in a one-dimensional double array named rates. The array has five element.

Translated into Thailand => เขียนโค้ดเพื่อคำนวนหาค่าเฉลี่ยของค่าองค์ประกอบที่อยู่ในอะเรย์1มิติ ข้อมูลภายในอะเรย์มี5ข้อมูล


Solution=> 
int[] rate={16,9,8,11,7}; //ประกาศตัวแปลเป็นอะเรย์ชนิดจำนวนเต็ม 
int i;
float sum=0;
for(i=0;i<rate.length;i++){ //ลูปfor()ใช้ในการวนลูปเพื่อให้โปรแกรมทำงานซ้ำๆเมื่อเงื่อนไขเป็นจริง
  sum=sum+rate[i];              //ใช้ในการรวมผลบวกของทุกจำนวนในarray
}
println(sum/rate.length);   //แสดงค่าออกมาที่หน้าจอ

Out put => 10.2

You can run code in http://processingjs.org/tools/processing-helper.html



text book name :Programming and problem solving with Java – 2nd edition
Author : Nell Dale and Chip Weems
              ISBN 978-0-7637-3402-2

ไม่มีความคิดเห็น:

แสดงความคิดเห็น