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

Loop

= Easy=

Proposition => How many times does the following loop execute, and what is this output?

count =1;
while (count <= 12) {
      count << count << " " << endl;
      count++;
}

(มีการดำเนินการวนลูปทั้งหมดกี่ครั้ง และผลลัพธ์ที่ออกมาคืออะไร?)

Answer and Explain => 
วนทั้งหมด 11 ครั้ง ผลลัพธ์ที่ได้คือ 1 2 3 4 5 6 7 8 9 10 11 12
//การวนลูปเหมือนกับ processing แต่คำสั่งของ c++ ต่างกัน
//คือรับค่า count =1 เข้าไปแล้ว วน และเพิ่มค่าให้เรื่อยๆ ทีละหนึ่งจนมีค่าเท่ากับ 12 และหยุด จากนั้นพิม count ทั้งหมดออกมา
//ถ้าเขียนโดยใช้ภาษา processing ได้ดังนี้
int count =1;
while (count <= 12) {
  count =count;
  println (count);
  count++;
}


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




= Medium =

Proposition => Write the initialization loop. The variable found become true when the int variable indata contain zero. That count start at one and is incremented in each iteration.(เขียนลูปเริ่มต้น ค่า found ของตัวแปรจะเป็นจริงเมื่อ indata เท่ากับศูนย์ และ count เริ่มนับที่ 1 จากนั้นเพิ่มขึ้นเรื่อยๆ)

Solution =>
int count = 1;
boolean found = false;
int indata = 5;
while (count <= 5) {
  indata--;
  if (indata == 0) {
    found = true;
  }
  count++;
}
println ("found = "+ found);


Explain =>

int count = 1; //เป็นการกำหนดตัวนับด้วยตัวแปรชื่อ count เริ่มตั้งแต่หนึ้ง ตามโจทย์
boolean found = false; //ถ้า found ไม่เข้าเงื่อนไขให้มีค่าเป็น false
int indata = 5; //กำหนด ให้ indata มีค่าเท่ากับเท่าไหร่ก็ได้ที่ไม่น้อยกว่า 1 ไม่งั้นจะไม่เกิดการวนลูป
while (count <= 5) { //ใช้เงื่อนไขให้ count นับถึงแค่ห้า เท่ากับค่าของ indata เพื่อให้ found มีค่าเป็น true
  indata--; //ลดค่าลงเรื่อยๆ จนเท่ากับศูนย์ตามโจทย์
  if (indata == 0) {
    found = true;
  } //ถ้า indata เท่ากับ 0 ตามโจทย์แล้วให้ found มีค่าเป็นจริง
  count++; //เพิ่ม count เข้ามาทีละหนึ่ง เพื่อให้ลูปเกิดการสิ้นสุด
}
println ("found = "+ found); //พิมพ์ค่าที่เป็นผลลัพธ์ออกมา


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


= Hard =

Proposition => The following code segment is supposed to write out the ood number from 1 to 19. What dose it actually output? Change the code to work correctly.

number = 1;
while (number < 10) {
number++;
count << number*2-1 << " ";
}

(ตามโค้ดที่ให้มา คือการเขียนเลขคี่ตั้งแต่ 1-19 คำตอบที่รันออกมาแล้วถูกหรือไม่ เปลี่ยนโค้ดให้ทำงานอออกมาได้ถูกต้อง)

Solution =>
ผลลัพธ์ที่ออกมาคือ
3
5
7
9
11
13
15
17
19
ซึ่งผิด 1-19 ต้องมี เลข 1 ด้วย แก้ไขได้ดังนี้

int number = 1;
int count;
while (number <= 10) {
  count = number*2-1;
  println (count);
  number++;

}
แค่เพิ่มตรงเงื่อนไข while เข้าไปว่า <=  และ ย้ายลำดับของคำสั่ง number ++  ไปไว้ท้ายสุด เพื่อให้รับค่า number = 1 ก่อน พอดรียร้อยแล้ว ก็จะพิมค่าครบ ตั้งแต่ 1-19 เลย


Explain =>
int number = 1; //ให้ มีค่าตั้งแต่หนึ่งเพราะต้องการหาจำนวนคี่ ที่มีตั้งแต่ 1-19
int count; //เป็ตัวแปรที่สร้างขึ้นมาเพื่อเก็บค่าของจำนวนคี่ี
while (number <= 10) {
  count = number*2-1; //กำหนดค่าให้กับ count โดยทางซ้ายของ = คือสมการหาจำนวนคี่
  println (count); //พิมค่าที่ได้ออกมา
  number++;

} //ใส่เงื่อนไข while เข้าไปเพื่อให้วนลูป หาจำนวนคี่จนครบ 1-19 แล้วหลุดออกจากลูป


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

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

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