โดยกำหนดอัตราการเติมถัง ความสูงของถัง และรัศมีของถัง และงานคือ ตรวจสอบถังน้ำล้น อันเดอร์โฟลว์ และเติมในเวลาที่กำหนด
ตัวอย่าง
Input-: radius = 2, height = 5, rate = 10 Output-: tank overflow Input-: radius = 5, height = 10, rate = 10 Output-: tank undeflow
แนวทางที่ใช้ด้านล่างมีดังนี้ −
- ป้อนอัตราการเติม ความสูง และรัศมีของถัง
- คำนวณปริมาตรของถังเพื่อหาอัตราการไหลของน้ำเดิม
- ตรวจสอบเงื่อนไขเพื่อหาผลลัพธ์
- ถ้าคาด <เดิมกว่าถังจะล้น
- ถ้าคาด> เดิมกว่าถังจะล้น
- ถ้าคาดหวัง =เดิมเกินถัง เติมตรงเวลา
- พิมพ์ผลลัพธ์ที่ได้
อัลกอริทึม
Start Step 1->declare function to calculate volume of tank float volume(int rad, int height) return ((22 / 7) * rad * 2 * height) step 2-> declare function to check for overflow, underflow and filled void check(float expected, float orignal) IF (expected < orignal) Print "tank overflow" End Else IF (expected > orignal) Print "tank underflow" End Else print "tank filled" End Step 3->Int main() Set int rad = 2, height = 5, rate = 10 Set float orignal = 70.0 Set float expected = volume(rad, height) / rate Call check(expected, orignal) Stop
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
//calculate volume of tank
float volume(int rad, int height) {
return ((22 / 7) * rad * 2 * height);
}
//function to check for overflow, underflow and filled
void check(float expected, float orignal) {
if (expected < orignal)
cout << "tank overflow";
else if (expected > orignal)
cout << "tank underflow";
else
cout << "tank filled";
}
int main() {
int rad = 2, height = 5, rate = 10;
float orignal = 70.0;
float expected = volume(rad, height) / rate;
check(expected, orignal);
return 0;
} ผลลัพธ์
tank overflow