วันอังคารที่ 24 กันยายน พ.ศ. 2556

=Array=(Medium)

Programming Exercise
Proposition => Write a Function that returns the lowest subscript in an array. Also find a function that returns the highest subscript in an array.


Translated into Thailand => เขียนฟังก์ชั่นที่ รีเทิร์นค่า ที่น้อยที่สุดและค่าที่มากที่สุด ใน Array


Solution=> 
void setup(){
 println("Max :"+Max(x));   //แสดงข้อความบนหน้าจอ
 println("Min :"+Min(x));
}
int[] x={4,2,3,4,5,7,6};   //ประกาศตัวแปลเป็นอะเรย์ชนิดจำนวนเต็ม 

int Max(int[] y){  //พังก์ชันที่มีการreturnค่าเป็นจำนวนเต็ม และมีparameter 
  int max=0;       //ประกาศและกำหนดค่า Maxในตอนแรก
  int i;
  for(i=0;i<y.length;i++){   //ใช้รันหาว่ามีตัวไหนในArrayที่มีค่ามากกว่าค่าMax 
   if(y[i]>max){
    max=y[i];         //เก็บค่าที่มีค่ามากกว่าค่าMaxคอนเริ่มต้นให้เป็นค่าMaxแทน
   }
  }
  return max;   //return max ไปที่ฟังก์ชัน
}

int Min(int[] m){      //พังก์ชันที่มีการreturnค่าเป็นจำนวนเต็ม และมีparameter 
 int min=m[0];      //ประกาศและกำหนดค่า Min ในตอนแรก
 int i;
 for(i=0;i<m.length;i++){  //ใช้รันหาว่ามีตัวไหนในArrayที่มีค่ามากกว่าค่าMin
   if(m[i]<min){
    min=m[i];         //เก็บค่าที่มีค่ามากกว่าค่าMinคอนเริ่มต้นให้เป็นค่าMinแทน
   }
  }
  return min;  //return min ไปที่ฟังก์ชัน

}



Out put => Max : 7
                  Min : 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

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

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