15 กันยายน 2556

..# Array #..

..# Array #..


Easy   P.524 /3
Write the C++ statement to declare a one-dimensional double array named rates that has five elements. Then write the code to assign the following numbers to the array: 6.5, 8.3, 4,2,and 10.5.
Answer
          double[] rate = new int[5];
                   double[] rate[0] = 6.5;
                   double[] rate[1] = 8.3;
                   double[] rate[2] = 4;
                   double[] rate[3] = 2;
                   double[] rate[4] = 10.5;
// or double[] rate = {6.5, 8.3, 4, 2, 10.5};

 
Middle   P.524 /15
Write the C++ code to add together the numbers stored in the first and second elements inclouded in a one-dimensional int array named num. Display the sum on the screen.
Answer
          int[] num = {20, 30};
                   println(num[0]+num[1]);
                   // add together the numbers stored in the first and second elements
                   // output  50

 

Hard   P.524 /20
Write the C++ cord to add together the number stored in the first row, first column of the num array, and the number stored in the second row, first column of the num array. Display the sum on the screen.
Answer
          void setup(){
                        int[][] num = {{1,2},{4,3}}; // แระกาศและกำหนดค่าอะเรย์สองมิติ num
                        int[] N = new int[num[1].length]; 
/* ประกาศและกำหนดให้ค่าอะเรย์หนึ่งมิติ  N ให้รับค่าผลบวกของ column มีความยาวเท่ากับ จำนวน column ในแถวที่ 1 */
                        int i = 0;  // ประกาศและกำหนดค่าเริ่มต้น i = 0
                         while (i < num[1].length){
                             N[i] = num[0][i]+num[1][i];

  /* add together the number stored in the first row, first column of the num array, and the number stored in the second row, first column */
                             i = i + 1; // เพิ่มค่า i จะขึ้นตำแหน่งใหม่
                        }
                        println(N);
                   }



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

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