คำตอบที่ได้รับเลือกจากเจ้าของกระทู้
ความคิดเห็นที่ 1
เขียนแค่ do...while เดียวน่าจะสะดวกกว่านะครับ โค้ดคร่าวๆ นะครับ อาจจะต้องไปปรับแต่งเรื่องตัวแปรที่รับ เพื่อให้จบโปรแกรม
int main()
smallVar = largeVar = enterVar = 0; // ค่าเริ่มต้น 0
get value 1st time to "enterVar"; // รับค่าใส่ตัวแปร enterVar
smallVar = largeVar = enterVar; // ค่าต่ำสุด = ค่าสูงสุด = ตัวแรกที่รับ
do{
if (enterVar < smallVar){ // ถ้าน้อยกว่าค่าต่ำสุด เปลี่ยนค่าต่ำสุด
smallVar = enterVar;
}
if (enterVar > largeVar){ // ถ้ามากกว่าค่าสูงสุด เปลี่ยนค่าสูงสุด
largeVar = enterVar;
}
display "Small Value = " smallVar; // แสดง
display "Large Value = " largeVar; // แสดง
get value another time to "enterVar" ("X" to end); // รับค่าต่อไปใส่ตัวแปร enterVar
}while(enterVar != "X"); // ทำไปเรื่อยๆ จนกว่าจะใส่ค่า X
display "END";
}
int main()
smallVar = largeVar = enterVar = 0; // ค่าเริ่มต้น 0
get value 1st time to "enterVar"; // รับค่าใส่ตัวแปร enterVar
smallVar = largeVar = enterVar; // ค่าต่ำสุด = ค่าสูงสุด = ตัวแรกที่รับ
do{
if (enterVar < smallVar){ // ถ้าน้อยกว่าค่าต่ำสุด เปลี่ยนค่าต่ำสุด
smallVar = enterVar;
}
if (enterVar > largeVar){ // ถ้ามากกว่าค่าสูงสุด เปลี่ยนค่าสูงสุด
largeVar = enterVar;
}
display "Small Value = " smallVar; // แสดง
display "Large Value = " largeVar; // แสดง
get value another time to "enterVar" ("X" to end); // รับค่าต่อไปใส่ตัวแปร enterVar
}while(enterVar != "X"); // ทำไปเรื่อยๆ จนกว่าจะใส่ค่า X
display "END";
}
แสดงความคิดเห็น
[c++] ถามเกี่ยวกับการสร้างลูปเทียบค่าจำนวนไปเรื่อยๆครับ
ป้อนค่า 1
แสดง 1
ป้อนค่า 2
แสดง 2
1 is the smallest so far
2 is the largest so far
ป้อนค่า 3
แสดง 3
1 is the smallest so far
3 is the largest so far
ป้อนค่า 0
แสดง 0
0 is the smallest so far
3 is the largest so far.....(ไปเรื่อยๆไม่สิ้นสุด)
แต่ผมไม่รู้จะใช้ลูปซ้อนลูปได้หรือไม่ ลองซ้อนดูโดยใช้ if แต่ก็ไม่ผ่าน มันไม่เก็บค่าใหม่ใน double ครับ ทำอย่างไรให้การคำนวณเปรียบเทียบค่าใหม่ที่ป้อนล่าสุดและแสดงผลเหมือนข้างบนครับ โค๊ดผมตามล่างต้องแก้จุดไหนบ้าง ขอบคุณครับ
int main()
{
double num1, num2, num3;
double smallest = 0;
double largest = 0;
cout << "Enter a value (a double value is fine)\n"; //เริ่มต้นเป็นปรกติ
cin >> num1;
cout << num1 << "\n";
cout << "Enter another value\n";
cin >> num2;
cout << num2 << "\n";
while (num1 < num2) //รันผ่าน
{
smallest = num1;
largest = num2;
cout << smallest << " is the smallest so far\n"
<< largest << " is the largest so far\n";
cout << "Enter another value\n";
cin >> num3;
}
while (num1 > num2) //รันผ่าน
{
smallest = num2;
largest = num1;
cout << smallest << " is the smallest so far\n"
<< largest << " is the largest so far\n"
<< "Enter another value\n";
cin >> num3;
}
while (num3 < smallest) // รันตัวที่สามและตัวต่อมาที่เก็บไว้ใน num3 ไม่ผ่าน
{
smallest = num3;
cout << smallest << " is the smallest so far\n"
<< largest << " is the largest so far\n"
<< "Enter another value\n";
}
while (num3 > largest) // รันตัวที่สามและตัวต่อมาที่เก็บไว้ใน num3 ไม่ผ่าน
{
largest = num3;
cout << smallest << " is the smallest so far\n"
<< largest << " is the largest so far\n"
<< "Enter another value\n";
}
return 0;
}
โปรแกรมรันไม่ผ่านตามนี้ครับ จำแต่ค่าเริ่มต้นแต่ไม่ทราบว่าจะคำนวณค่าต่อๆมาอย่างไร