3. ! 83
สามารถกาหนดคาของอารเรย เปน constant โดยระบจานวนสมาชิกหรือไมระบุก็ไดดังนี้
ํ ุํ
const int score[10] = { 20,25,30,26,24,18,17,25,24,22};
const int score[] = { 20,25,30,26,24,18,17,25,24,22};
3.2 การกาหนดคาใหอารเ รยจากตวแปร เชน
ํ ั
x = 20; y = 26;
score[0] = x;
score[9] = y;
4. การรบขอมลทางคียบอรดเกบในอาเรยโดยใชคาสั่งลูป เพื่อความสะดวกและความยืดหยุนใน
ั ู ็ ํ
โปรแกรม เราสามารถรับคาคงที่เขาไปเก็บไวในอารเรยแตละสมาชิกโดยใชลูป for, while , do เชน
for( no=0 ; no<=9; ++no)
cin>>score[no]; //enter from keyboard to array and no is index
5. การแสดงผลขอมลอารเ รย สามารถแสดงผลขอมลในอารเ รยแตละสมาชกได โดยการอางชอ
ู ู ิ ่ื
และอินเด็กซ โดยใชลูป for , while ,do เพอแสดงผลขอมลในอารเ รยทงหมดได เชน
่ื ู ้ั
for(no=0;no<=9;++no)
cout<<score[no]<<endl; //display data all element of array and no is index
•! ตัวอยางโปรแกรม array1.cpp แสดงการรับคาคะแนนนักศึกษา 10 คน เกบไวในอารเ รย
็
คํานวณหาผลรวมคะแนนทงหมดและคานวณหาคาเฉลี่ยของคะแนน พรอมแสดงผลขอมูล
้ั ํ
จากอารเ รยทงหมด
้ั
/*Program : array1.cpp
Process : input and display data for array 1 dimension */
#include <iostream.h>
#include <conio.h>
void main() //begin program
{ int no,score[10];
float avg=0,sum=0;
clrscr();
//input integer constant to array
cout<< "Please enter score of student 10 personsnn";
for(no=0;no<=9;++no)
{ cout<< "Std.#"<<no+1<<" : ";
cin>>score[no];
}
ศิริชัย นามบุรี อารเรย(Array)
4. ! 84
cout<< "O.K. you enter complete...press any key to displayn";
getch();clrscr();
//display value of each element of array
cout<< "Display your enter score :n";
for(no=0;no<=9;++no)
{ cout<< "Std.#"<<no+1<<" : "<<score[no];
cout<<endl;
sum+=score[no]; //calculate summation
}
avg=sum/10.0; //calculate average of score
cout<<"Summation of score = "<<sum<<endl;
cout<<"Average of score = "<<avg<<endl<<'a';
getch();
} //end program
•! ตัวอยางโปรแกรม arr_func.cpp แสดงการรับคาคะแนนนักศึกษา 10 คน เกบไวในอารเ รย
็
คํานวณหาผลรวมคะแนนทงหมดและคานวณหาคาเฉลี่ยของคะแนน พรอมแสดงผลขอมูล
้ั ํ
จากอารเ รยทงหมด แตเขียนแยกเปนฟงกชนทําหนาที่แยกกัน
้ั ั
/*Program : arr_func.cpp
Process : define input and display data for array 1 dimension */
#include <iostream.h>
#include <conio.h>
//declaration prototype function
void input();
void display();
//declaration external variable
int score[10];
float avg=0,sum=0;
void main() //begin main program
{
clrscr();
input(); //call function
display(); //call function
avg=sum/10.0; //calculate average of score
cout<<"Summation of score = "<<sum<<endl;
cout<<"Average of score = "<<avg<<endl<<'a';
getch();
} //end main program
void input() //input() function for enter integer constant to array
ศิริชัย นามบุรี อารเรย(Array)
5. ! 85
{ int no;
cout<< "Please enter score of student 10 personsnn";
for(no=0;no<=9;++no)
{ cout<< "Std.#"<<no+1<<" : ";
cin>>score[no];
}
cout<< "O.K. you enter complete...press any key to displayn";
getch();clrscr();
}
void display() //display() function for show value of each element of array
{ int no;
cout<< "Display your enter score :n";
for(no=0;no<=9;++no)
{ cout<< "Std.#"<<no+1<<" : "<<score[no];
cout<<endl;
sum+=score[no]; //calculate summation
}
}
♦! ารเรย 2 มิติ
อ
อารเรย 2 มิติ (two-dimension array) หมายถึง อารเรยทมตวอินเด็กซสาหรบการอางถงขอมล
่ี ี ั ํ ั ึ ู
จํานวน 2 ชุด คือ อนเดกซชแนวแถว(row) และแนวคอลัมน (column)
ิ ็ ้ื
ลักษณะของขอมูลชนิดที่เปนอารเรย 2 มิติ เชน คะแนนสอบของนักศึกษาจํานวน 10 คน ทั้ง
หมด 3 วิชา ดงตาราง
ั
นักศึกษาคนที่ ! แนวคอลัมน
คะแนนวิชา
คณิตศาสตร ภาษาอังกฤษ วิทยาศาสตร
! แนวแถว 1 25 30 35
2 28 25 32
3 30 29 33
4 29 30 35
5 35 20 37
6 26 31 30
7 20 26 30
8 26 25 24
9 29 30 35
10 26 32 25
ศิริชัย นามบุรี อารเรย(Array)
9. ! 89
void input() //input() function for enter integer constant to array
{ int std_no,subj_no,col,row;
cout<< "Please enter score of student 10 personsnn";
gotoxy(5,3);cout<<"Std # Math English Science";
col=15;row =5; //varaible for function gotoxy()
for(std_no=0;std_no<=9;++std_no)
{ gotoxy(7,row); //skip cursor to enter data position
cout<<std_no+1;
for(subj_no=0;subj_no<=2;++subj_no)
{ gotoxy(col,row);
cin>>score[std_no][subj_no];
col+=15; //increase column position
}
row++; //increase row position
col=15; //set column at old position in new row (next student)
}
cout<< "O.K. you enter complete...press any key to displayna";
getch();clrscr();
}
void display() //display() function for display score
{ int std_no,subj_no,col,row;
cout<< "*** Display score of student 10 persons 3 subjects ***nn";
gotoxy(5,3);cout<<"Std # Math English Science";
col=15;row =5;
for(std_no=0;std_no<=9;++std_no)
{ gotoxy(7,row);
cout<<std_no+1;
for(subj_no=0;subj_no<=2;++subj_no)
{ gotoxy(col,row);
cout<<score[std_no][subj_no];
col+=15;
}
row++; col=15;
}
}
ศิริชัย นามบุรี อารเรย(Array)