Phân tích chương trình sau đây một cách cẩn thận. Xác định body của vòng lặp và tìm ra cách để thoát ra khỏi body đó. Kiểm tra cách mà source code bên dưới thực hiện thuật toán mà chúng ta đã đề cập trước đó
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> using namespace std; int main(void) { /* temporary storage for the incoming numbers */ int number; /* get the first value */ cin >> number; /* we will store the currently greatest number here */ int max = number; /* if the number is not equal to -1 we will continue */ while(number != -1) { /* is the number greater than max? */ if(number > max) { /* yes – update max */ max = number; } /* get next number */ cin >> number; } /* print the largest number */ cout << "The largest number is " << max << endl; /* finish the program successfully */ return 0; } |