..# Variable #..
Easy P.111 /6
Write a C++ statement to declare and initialize and int named constant called
MAXPAY whose value is 20.
Answer int MAXPAY; //declare
MAXPAY = 20;
Middle P.110 /1
Write the statement to declare and initialize a variable that can store an item’s
price, which can contain decimal places. Name the variable price and
assign the double data type to it. Then write an assignment statement
that assign the value 16.23 to the variable.
Answer double price = 16.23; // ประกาศและกำหนดค่าตัวแปร
println(price); // ปริ๊นค่า price
Hard P.111 /11
Assume a program needs
to calculate an employee’s gross pay. The input items will be the employee’s
name,hours worked, and pay rate. Write the appropriate C++ statements to
declare and initialize the necessary memory location. The hours worked and pay
rate will always be integer. Then write an assignment statement that calculates
the gross pay. You do not need to worry about overtime pay, because no employees
work more than 40 hours.
Answer String employee
= "YUY"; // ประกาศและกำหนดค่าตัวแปร
ใช้ String เพราะค่าเป็นตัวอักษร
int
hours = 12; // ประกาศและกำหนดค่าตัวแปร
เป็นจำนวนเต็ม ค่า hours = 12
int
pay = 300; // ประกาศและกำหนดค่าตัวแปร
เป็นจำนวนเต็ม ค่า pay = 300
int
gross = hours*pay;
// ประกาศและกำหนดค่าตัวแปร
ให้ค่า gross เท่ากับ ค่า hours คูณ ค่า
pay
println("employee
name is "+employee); // คำสั่งปริ๊น
println("gross
pay = "+gross); // คำสั่งปริ๊น