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

Array

= Easy =

Proposition => An array call topTenList, of 10 value of type string.(สร้างอาเรย์ชื่อ topTenList เก็บค่า 10 ค่าที่เป็นชนิด string)

Solution =>
int i = 0;
String [] topTenList = {
  "Unthika", "Sarisa", "Rojanapon", "Sirilada", "Kananad", "Panaudda", "Chalita", "Theranun", "Siriluk", "Orapun"
};
while (i<topTenList.length) {
  println(topTenList[i]);
  i++;

}

Explain =>
int i = 0; //สร้างตัวแปร i ขึนมาเก็บค่าของ index มน Array
String [] topTenList = {
  "Unthika", "Sarisa", "Rojanapon", "Sirilada", "Kananad", "Panaudda", "Chalita", "Theranun", "Siriluk", "Orapun"
}; // นี่คือการประการและกำหนด Array ชนิด string โดยประกอบด้วยข้อมูล 10 อัน ตามโจทย์
while (i<topTenList.length) {
  println(topTenList[i]);
  i++;
}//เป็นการใช้ loop ช่วยในการสั่งพิมพ์ค่าแต่ละค่าออกมาจอก array โดยวนลูปจนกว่าจะครบจำนวนข้อมูลแล้วหยุด [เพื่อเติม ควมยาวของ array จะมากกว่า index อยู่หนึ่งเสมอ เพระาเริ่มนับต่างกัน index เริ่มนับตั้งแต่ศูนย์ แต่ ความยาวนับตั้งแต่หนึ่ง]

From : Programming in C++ => ISBN : 0-7637-3234-6




= Medium =

Proposition => Write a declaration array of a char type called Grade that have five grade by A, B, C, D, F and value of grade are 4, 3, 2, 1 Respectively then grade output if the student get 3 point what is grade that get?(กำหนดอาเรย์ ประเภท char ชื่อ Grade ซึ่งมีห้าเกรด ดังนี้ A, B, C, D มีค่าเท่ากับ 4,3,2,1, ตามลำดับ จากนั้นพิมพ์ค่าของเกรดที่นักเรียนที่ได้คะแนน 4 จะได้ออกมา)

Solution =>
char [] Grade = {
  'A', 'B', 'C', 'D'
};
int i = 0;
int student=4;
if (i <= Grade.length) {
  if (student == 4) {
    i=0;
  }
  if (student == 3) {
    i=1;
  }
  if (student == 2) {
    i=2;
  }
  if (student == 1) {
    i =3;
  }
  i++;
}

println(Grade[i-1]);



Explain =>
char [] Grade = {
  'A', 'B', 'C', 'D'
}; //เป็นการประกาศ และกำหนดอาเรย์ประเภท char ชื่อ Grade
int i = 0; //เป็นการสร้างตัวแปรเพื่อนับ index
int student=4; //ตัวแปรที่เก็บค่าคะแนนนักเรียน
if (i <= Grade.length) { //ใช้เงื่อนไขเช็คค่าว่า คะแนนของนักเรียนเท่ากับเกรดอะไร
  if (student == 4) {
    i=0;
  } //ถ้าได้ 4 คะแนน
  if (student == 3) {
    i=1;
  } //ถ้าได้ 3 คะแนน
  if (student == 2) {
    i=2;
  } //ถ้าได้ 2 คะแนน
  if (student == 1) {
    i =3;
  } //ถ้าได้ 1 คะแนน
}


println(Grade[i]); //เกรดที่อยู่ในช่อง index ออกมาตามตัวแปร i

From : Programming in c++ => ISBN : 0-7637-3234-6




= Hard =

Proposition => Draw a picture by using the Array two-dimentional for keep value of each variables.
(วาดภาพโดยใช้ อาเรย์สองมิติเก็บค่าของแต่ละตัวแปร)

Solution =>
int [][] picture = {
  {
    30, 70
  }
  , {
    50, 80
  }
};
int i=0;
void setup () {
  size (100, 100);
    background (0);
}
void draw () {
  while (i< 2) {
    ellipse (picture[0][i], picture[1][i], 30, 30);
    i++;
  }
}


Explain =>
int [][] picture = {
  {
    30, 70
  }
  , {
    50, 80
  } //เป็นการประกาศตัวแปรสองมิติชื่อว่า picture โดยมีหลักการเรียกใช้คือ ชนิดตัวแปร [][] ชื่อตัวแปร = {{ข้อมูลชุดที่1}, {ข้อมูลชุดที่2}};
}
int i=0; //ตัวแปรที่ใช้เก็บค่า index มิติที่สอง
void setup () {
  size (100, 100);
    background (0);
}
void draw () {
  while (i< 2) {
    ellipse (picture[0][i], picture[1][i], 30, 30);
    i++;
  }
} //คำสั่งวาดภาพวงกลมทั่วไป

From : Programming in c++ => ISBN : 0-7637-3234-6 

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

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