วันจันทร์ที่ 16 กันยายน พ.ศ. 2556

Array

Easy

An Introduction to Programming whith C++ / Diane Zak
    ISBN : 0-619-06473-0
    page : 524 

Question : Write the statement to declare int array named number .The array should have 5 element.

แปล : เขียนคำสั่งที่จะประกาศอาร์เรย์ชนิด int ชื่ออาเรย์ number โดยอาร์เรย์จะต้องมี  5 element

Answer :  int[ ] balances = new int[5];//ประกาศ array ชนิด int ชื่อ balances โดยมี 5 element
                balances[0]=1;//index ที่ 0  มีค่าเท่ากับ 1
                balances[1]=2;//index ที่ 1  มีค่าเท่ากับ 2
                balances[2]=3;//index ที่ 2  มีค่าเท่ากับ 3
                balances[3]=4;//index ที่ 3  มีค่าเท่ากับ 4
                balances[4]=5;//index ที่ 4  มีค่าเท่ากับ 5
โดย index ของ array จะเริ่มต้นที่ 0 ก่อนเสมอ

Medium

Programming in C++ / Nell Dale,Chip Weems.
    ISBN : 0-7637-3234-6(pbk.)
    page : 506


Question : Write the following declaration an Array called topTenList, of 10 Value of type string.and type of colors the Array should be called colorMix,and contain value of type float.

แปล : เขียนประกาศ Array ต่อไปนี้ ที่เรียกว่า topTenList ของค่าชนิด string 10 ค่า.และชนิดของสีให้ Array ชื่อ ว่า colorMixc และเก็บค่าชนิด float .

code


void setup(){
 size(600,150);
 background(2);
 String[]topTenList={"mint","Gade","Som","Daw","June","Nut","Waii","sun","Mon","Jay"}; //ประกาศ+กำหนด array ชนิด String ชื่อ topTenList
 float[]colorMix={12.5,15,30,40,60,20,50,77.4,19.9,33.5};//ประกาศ+กำหนด array ชนิด floatชื่อ colorMix
 int a=0;
 int i=0;
 int j=0;
 int x=30;
 int y=100;

 while(a<topTenList.length){
   if(j<topTenList.length){
     textSize(15);
     text(topTenList[i],x,50);
     text(colorMix[i],x-10,y);
        x=x+55;
        i=i+1;
        j=j+1;
   }
   a=a+1;
 }
 }

result





Hard


An Introduction to Programming whith C++ / Diane Zak
    ISBN : 0-619-06473-0
    page : 524 

Question : Write the code to declare  array named balances.The array should have four rows and six columns.
แปล :เขียนโค้ดเพื่อประกาศอาร์เรย์ชื่อ balances , อาร์เรย์ จะต้องมีสี่แถวและหกคอลัมน์


code
void setup()
{
  size(300,300);
}
void draw(){
  background(2);
  int x=10;
  int y=55;
  int r=30;
  int n=4;
  int v=10;
int[ ] balances = new int[4];//ประกาศ array ชนิด int ชื่อ balances โดยมีขนาดเท่ากับ 4
balances[0]=6;//index ที่ 0  มีค่าเท่ากับ 6
balances[1]=6;//index ที่ 1  มีค่าเท่ากับ 6
balances[2]=6;//index ที่ 2  มีค่าเท่ากับ 6
balances[3]=6;//index ที่ 3  มีค่าเท่ากับ 6

 Rec(x,y,balances[0],r); //ใช้ค่าที่เก็บไว้ใน array มากำหนดขนาด r ของสี่เหลี่ยม
 y=y+r+v;
 Rec(x,y,balances[1],r);
 y=y+r+v;
 Rec(x,y,balances[2],r);
 y=y+r+v;
 Rec(x,y,balances[3],r);
 y=y+r+v;
}


void Rec(int x,int y,int n,int r){
  
  while(n>0){ //ใช้ while loop ในการวนลูป เซ็คเงื่อนไขในการวาดสี่เหลี่ยม
    rect(x,y,r,r);
    n=n-1;
    x=x+50;
  }
}

result


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

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