Upload
watcharin-puangplia
View
212
Download
0
Embed Size (px)
DESCRIPTION
อาร์เรย์ 1 มิติ 2 มิติ
Citation preview
�������� �������� (Array)(Array)
����������������������
� ����������������� 1 ����
� �����������������������
� ��������� (String)
� ����%&'%�()%���������*������ �����������������������
� �������+�&)%�*(��������
� �������,���-&)%���������
� ���*��)%����.���(*�������������������
� ����%&'%�()%���������*�����/0&��1�(
� ����������������� 2 ����
� ��������34.�4�����%� 2 ����
�����������
��������)5���6���������)5���6�
� �������� )5� �7��������7,34.�8��(-��9(�:5.�*�������+��;�����7,1(�-�-4����(�,��< ���:������(6-�����7,1(�-�-4����(�,��< ���:������(6-�
� ����7,=�+7�=�-��;���7%*(�,;����&�(%��)���=8��-4����( �,��4���=�-��4�&����7,��%,�����%��(5.�&��(6�
� ����7,��%,������4���%� �4,4��(�� (Element)
� �������+�&��%,��4,4��(�������+*1��������%����(6-� ��%=�� �������+�&��%,��4,4��(�������+*1��������%����(6-� ��%=�*1��,�-�1(4 (Index) ��E(������&��&
����������������� ����������������� 11 ��������
� *1��,�-�1(4*(������&��&�:4�& 1 )%�
� �7�������������������:� �7�������������������:
1(�-����7, 15.�������[�(�-��&��������];
� �(�-��&�����������&��E()%�)&34.�3%�(�9( ��E(��������5�(�:=(�6�%6-�
� �����%�&: int ages[10];
char name[50];
�(�-�(�-��&�������������� ��&�������������� ((*(�(%��6���*(�(%��6���))
� ��������+���(�-��&��������*(�(%��6���6-�J-�*1� sizeof� ��5���==�*1����)8�(�O�;6-� �1%( ����4���������������� ��5���==�*1����)8�(�O�;6-� �1%( ����4���������������-�&(49int numbers[10];
� �(�-��&������ numbers = 10 * 4 = 40 6���
� char name[50]; �(�- name = 50 * 1 = 50 6���
������������� ��������� ������ int
=&��4�(',,�:Y���&J�������%�6�(49=&��4�(',,�:Y���&J�������%�6�(49#include <iostream.h>
#include <conio.h>
int main()
{
int ages[10];
char name[50];
double scores[20];
cout << "Size of ages = " << sizeof(ages) << endl;
cout << "Size of name = " << sizeof(name) << endl;
cout << "Size of scores = " << sizeof(scores) << en dl;cout << "Size of scores = " << sizeof(scores) << en dl;
getch();
return 0;
}�������
Size of ages = 40Size of name = 50Size of scores = 160
�������+�&)%�*(���������������+�&)%�*(��������
� �7����������&��&+�&�4,4��(��*(�������� :
15.�������[�,�-�1(4] /*�,�-�1(4���������.�34. 0*/15.�������[�,�-�1(4] /*�,�-�1(4���������.�34. 0*/
� �,�-�1(4=���E()%�)&34. ������ ��5�(�:=(��;6-� �����%�&�1%(int scores[5] ;
scores[ 0] = 82;;
scores[1] = 49 ;
82scores[0]
49scores[1]
???scores[2]
???scores[3]
???scores[4]
���*1�)8���.& ���*1�)8���.& for for *(�������+�&)%�*(��������*(�������+�&)%�*(��������#include <iostream.h>#include <conio.h>int main(){
int a[i],i;
for (int i = 0; i < 5; i++)a[i] = 2 * i;
getch();return 0;
}
a[0] a[1] A[2] A[3] A[4]
0 2 4 6 8
)���'�-:,�-34.:�6-��%��)��9&)���'�-:,�-34.:�6-��%��)��9&
� *1�������(��������34.������6�� �1%(int a[ 4];int a[ 4];
a[4] = 2;
a[5] = 10;
for(int i=0;i<=4;i++)
a[i] = i;
��5.���(J���������=��=����)����%� Segmentation Fault��5.���(J���������=��=����)����%� Segmentation Fault
� �8��(-)%�*�������������������J-���&int x[10] ;
x = 11 ;
������)%�*�������������������)%�*�������������
� ������)%�=��'7�*1� �:5.���;�*(�������������������+38�6-�J-�*1� cin �1%(�-4�����������1(�-�5.( ��%���&�4���6-�J-�*1� �1%(�-4�����������1(�-�5.( ��%���&�4������\�%�=�(8�)%�34.�����(�9(��;�6��34.�4,4��(��*- �����%�&�1%(float num[2];
cin >> num[0];
cin >> num[ 1];cin >> num[ 1];
� ��5���==�*1�)8���.& for *(��O434.���&�������,��< )%�int a[10];
for(int i=0;i<10;i++)
cin >> a[i];
�����-&)%���&�������������-&)%���&��������
� �����-&)%���&���������������;�����+38�6-�J-�*1� cout
�1%(�-4�����������1(�-�5.( ��%���&���\�4,4��(��34.���&���-��� �1%(�-4�����������1(�-�5.( ��%���&���\�4,4��(��34.���&���-��� �1%(char ch[3] ;
ch[0] = 'A'; ch[1] = 'B'; ch[2] = 'C';
cout << ch[0] << ch[1] << ch[2] ;
� ��5���==�*1�)8���.& for *(��O434.���&�����-&�,���4,4��(�� for(int i=0;i<3;i++)
cout << ch[i] << endl ;
//ex1.cpp#include <iostream.h>#include <conio.h>int main(){
int num[ 4],i;
=&��4�(',,�:Y��%�(34.��,5���&J�������%�6�(49=&��4�(',,�:Y��%�(34.��,5���&J�������%�6�(49
',,�:Y�:Enter num[0]: 2
Enter num[1]: 5
Enter num[ 2]: 7
������)%�
int num[ 4],i;
for(i=0;i<4;i++){
cout << "Enter num [" << i << "]: ";cin >> num[i];
}
for(i=0;i<4;i++)num[i] = 2 * num[i];
Enter num[ 2]: 7
Enter num[3]: 3
num[0] = 4
num[1] = 10
num[2] = 14
num[3] = 6�����-&)%�num[i] = 2 * num[i];
for(i=0;i<4;i++)cout << "num[" << i << "] = " << num[i] << endl;
getch();return 0;
}2 5 7 34 10 14 6
num[0] num[1] num[2] num[3]
���*��)%����.���(�������� ���*��)%����.���(�������� (Array Initialization)(Array Initialization)
int nums[3] = {4,1,8 };
4
nums[0]
1
nums[1]
8
nums[2]
char ch[3] = {'C', 'O', 'E'};
C
ch[0]
O
ch[1]
E
ch[2]
� *(��O434.���������4�(�-*�]% ���*��)%����.���(�;��==��4�����9(���3�-*��%6-�
float gallons[9] = { 19.5, 16.2, 14.14,
18.17, 22.3, 10.1 ,
22.50, 15.2, 8.15 };
char name[ 8] = { 'C', 'o', 'm', 'p', 'u', char name[ 8] = { 'C', 'o', 'm', 'p', 'u', 't', 'e', 'r' };
� ����8��(-)%����.�*�������������6�%)�� �4,4��(��34.��,5�=��4)%���E( 0int a[5] = {2,9,5 };
2
a[0]
9
a[1]
5
a[2]
0
a[3]
0
a[4]
� �8�����������1(�- char ��==�*1���],���O� '\0' �3( 0 �;6-�0 �;6-�char ch[4] = {'C', 'A', 'T'};
C
ch[0]
A
ch[1]
T
ch[2]
\0
ch[3]
� ���*(����8��(-)%����.���(6�%6-��4����8��(-�(�-��&�������� )��6:�,���=��8��(-�(�-��&��������*���3%����=8�(�(�4,4��(��34.6-��4����8��(-)%����.���( �1%(int x[] = { 15, 8, 7, 10};int x[] = { 15, 8, 7, 10};
15
x[0]
8
x[1]
7
x[2]
10
x[3]
�(�-��&�������� x =�6-��3%���� 4
� ��%+�����6�%�4����8��(-)%����.���( =����&�4����8��(-� ��%+�����6�%�4����8��(-)%����.���( =����&�4����8��(-�(�-����
int x[];
J�������8�������)%��7&�\-*(��������J�������8�������)%��7&�\-*(��������//max.cpp
#include <iostream.h>
#include <conio.h>
int main()int main()
{
int a[4] = {-1,6,9,2};
int i,max = a[0];
for(i=1;i<4;i++)
{
if(a[i] > max)
Maximum value is 9
�������:
if(a[i] > max)
max = a[i];
}
cout << "Maximum value is " << max << endl ;
getch();
return 0;
}
-1
a[0]
6
a[1]
9
a[2]
2
a[3]-1
max
69
J�������8�������)%�',�����&3\��4,4��(��*(��������J�������8�������)%�',�����&3\��4,4��(��*(��������//sum.cpp#include <iostream.h>#include <conio.h>#define SIZE 4 // ����8��(-)%�)&34.
Sum of all elements = 17
�������:
#define SIZE 4 // ����8��(-)%�)&34. int main(){
int num[SIZE] = {1,4,5,7};int i,total = 0;
for(i=0;i<SIZE;i++)total = total + num[i];total = total + num[i];
cout << "Sum of all elements = " << total;getch();return 0;
}1
num[0]4
num[1]5
num[2]7
num[3]0
total
151017
��������� ��������� (String)(String)
� String )5� �7��������7,34.*1���;����)���^�.&������-����������,������������,�����
� String ��E(����������& char 34.�4������ '\0' (��5�34.��4���%� null character) ��E(����%&���+�&=\-��9(�\- �1%(char name[4] = {'B', 'A', 'T', '\0 '};
� �� �,�������-&)%���& String �1%(� �� �,�������-&)%���& String �1%(cout << "My name is " << name;
�����%�&�����-&)%���& �����%�&�����-&)%���& StringString
//name.cpp
#include <iostream.h>
#include < conio . h>#include < conio . h>
int main()
{
char name[5] = {'J','o','h','n','\0 '} ;
char surname[8] = {'F','\0','K','E','N'};
cout << name << endl ;
cout << surname << endl ; cout << surname << endl ;
getch();
return 0 ;
} JohnF
�������:
������)%� ������)%� StringString
� ������)%� String �;)5� ���������)���=��'7�*1� ^�.&���)���34.�����(�9(�4��������6-��,����� *1�-.�/�0� cin���)���34.�����(�9(�4��������6-��,����� *1�-.�/�0� cin
char str[20];
cin >> str;
�����%�&������)%� �����%�&������)%� StringString//name2.cpp
#include <iostream.h>
#include <conio.h>
int main()
{
char n[ 10],s[ 10];
�������:
SomchaiJaidee
Enter name:Enter surname:
char n[ 10],s[ 10];
cout << "Enter name: ";
cin >> n;
cout << "Enter surname: ";
cin >> s;
cout << "Hello " << n << " " << s << endl;
getch();
return 0;
JaideeHello Somchai JaideeEnter surname:
return 0;
}
n[1]n[0] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9]
S o m c h a i \0
J a i d e e \0
���*��)%����.���(��� String
� *��)%����.���(���5�(�������������& char - ��4 '\0 ' �`-3�������3�������char name[4] = {'S','u','n','\0 '};
� *1��)�5.�&���� "..."char name[4] = "Sun";
S
name[ 0]
u
name[ 1]
n
name[ 2]
\0
name[ 3]name[ 0] name[ 1] name[ 2] name[ 3]
char str[] = "Good";
Gstr[0]
ostr[1]
ostr[2]
dstr[3]
\0str[4]
����8��(-)%�*����� String
� �8��(-)%�*����� String J-�*1��)�5.�&���� = 38�6-��a:����(�8��(-)%����.���(�3%�(�9(��(�8��(-)%����.���(�3%�(�9(char name[8] = "Somchai";
� ��%=��8��(-)%�J-�*1��)�5.�&���� = *(b���,�&6�%6-�char name[ 8];char name[ 8];
name = "Somchai";
� /0&��1�(����c�(*(b��� C++ 34.1%��*(����8��(-)%� String )5�/0&��1�( strcpy ^�.&*(���*1�/0&��1�((49=����& include 6/,� string.h -�������include 6/,� string.h -�������
� �����%�&�1%(char n[6];
strcpy(n,"Jim");
J i m \ 0J
n[0]
i
n[1]
m
n[2]
\ 0
n[3] n[4] n[5]
�����)��������& String
� /0&��1�(����c�(34.*1�*(�����)������ String )5� /0&��1�( strlen ^�.&*(���*1�/0&��1�((49=����& include 6/,� string.h -���strlen ^�.&*(���*1�/0&��1�((49=����& include 6/,� string.h -�������
� )��������& String 34.6-�(49=�6�%��� '\0'
char name[] = "Somchai";
int len;int len;
len = strlen(name); //this line, len = 7
len = strlen("Cat"); //this line, len = 3
�����%�&���*1�/0&��1�(��4.����� String
//len.cpp#include <iostream.h>#include <string.h>#include <string.h>int main(){
char name[20];int len;strcpy(name,"David");len = strlen(name);
Length of David is 5
�������:
cout << "Length of " << name << " is " << len; cout << endl;return 0;
}
//len2.cpp#include <iostream.h>#include <string.h>int main()
=&��4�(',,�:Y���&J�������%�6�(49
3int main(){
char str[] = "CAN";int i,len = strlen(str);for(i=0;i<len;i++)
str[i] = str[i] + 1 ;
cout << str << endl ;
return 0;
DBO
�������:
3
return 0;}
C
str[0]
A
str[1]
N
str[2]
\0
str[3]
D B O
����%&'%�()%���������*�����/0&��1�(
� ���/0&��1�( my_func �4��(�����&/0&��1�(-�&(49void my_func(int x);void my_func(int x);
� �,�*( main 6-��4�����������������������15.��%� numint num[10];
� ����%&�4,4��(��34. 0 ��&�������� num 6���E(���������(����&/0&��1�( my_func �����+��4�(6-�-�&(49��&/0&��1�( my_func �����+��4�(6-�-�&(49my_func(num[0]);
�����%�&����%&)%���%,��4,4��(��*(��������*�����/0&��1�(//ex2.cpp#include <iostream.h>
void check_val(int x);
int main()
{{
int a[3] = {2,-1,5 };
check_val(a[0]) ;
return 0 ;
}
void check_val(int x)
{
2: Positive
�������:
if(x >= 0)
cout << x << ": Positive\n" ;
else
cout << x << ": Negative\n" ;
}
#include <iostream.h>
void check_val(int x);
int main()
{
int i,a[3] = {2,-1,5 };
1��2�����������3���4�5�,�6��27
for(i=0;i<3;i++)
check_val(a[i]);
return 0 ;
}
void check_val(int x)
{
if(x >= 0)
2: Positive-1: Negative5: Positive
�������:
if(x >= 0)
cout << x << ": Positive\n" ;
elsecout << x << ": Negative\n i
}
/0&��1�(34.�4������)%�������E(��������
� /0&��1�(�����+34.=����)%�������E(��������6-� ^�.&�7������&�����4�(��(�����&/0&��1�(��E(-�&(49�����4�(��(�����&/0&��1�(��E(-�&(49
1(�-����7, 15.�/0&��1�((1(�-����7, 15.�������[�(�-��������]);
� *(��O4/0&��1�(�4������)%�������E(�������� 1 ���� ��==�6�%���&�8��(-�(�-��&���������;6-�
� �����%�&�1%(� �����%�&�1%(int sum_arr(int num[10]) ;
void print_arr(int a[5]);
float average(int num[]);
����%&��������3\��4,4��(��*�����/0&��1�(
� ����%&��������*(��O4(49 *1��)%15.����������������3%�(�9( �1%( ���*( main �4�����������������-�&(49�1%( ���*( main �4�����������������-�&(49int num[10] ;
� �,�/0&��1�( print_arr �4��(���/0&��1�(-�&(49
void print_arr(int a[10]);
� ����%&�������� num 3\��4,4��(��6�*��/0&��1�( print_arr � ����%&�������� num 3\��4,4��(��6�*��/0&��1�( print_arr �����+��4�(6-�-�&(49print_arr(num);
�����%�&����%&��������3\��4,4��(��*�����/0&��1�(
#include <iostream.h>void print_arr(int a[4]) ;
int main() {int main() {int num[4] = {5,2,-1,8 };
print_arr(num);return 0 ;
}
void print_arr(int a[4]){
5num[0]
2num[1]
-1num[2]
8num[3]
a[0] a[1] a[2] a[3]
{
for(int i =0;i<4;i++) cout << a[i];}
5 2 -1 8
�������:
=&��4�(J�������8��������)%�)��((��&(�������=8�(�( 10 )( �,�*��:��:�)%��a,4.���&)��((3�9&��- J-��8��(-*���%�(���)��((=��'7�*1��,��%�(34.��-&
�����%�&34. 1
J-��8��(-*���%�(���)��((=��'7�*1��,��%�(34.��-&)%��a,4.���7%*( main �8������%�(34.)8�(�O)%��a,4.�*����7%/0&��1�(15.� average J-�*���%&��������34.��;�)��((3�9&��& 10 )(=�� main ��*��/0&��1�( average ()��((�����+��E(3�(���6-�)�����+��E(3�(���6-�)
main average
���������-8���
-,�9�20
#include <iostream .h>float average(float num[]);
int main()
{int i;
float score[10],avg_score;for(i=0;i<10;i++)
{
cout << "Enter score " << i+1 << " : " ;
cin >> score[i];
}
return 0;
}
}avg_score = average(score);
cout << "Average score is " << avg_score;
float average(float num[])
{
float total,avg;
total = 0 ;
for(int i=0;i<10;i++)
total = total + num[i];
avg = total/10 ;
return avg;
}}
������6�)%���&��������b��*(/0&��1�(
� /0&��1�(34.�4������:������������E(�������� ����4������6�)%�/0&��1�(34.�4������:������������E(�������� ����4������6�)%�b��*(��������-�&�,%�� =��%&38�*����������34.+7��%&����E(���������(����&/0&��1�(�4�����,4.�(��,&-���
=&��4�(',,�:Y���&J�������%�6�(49#include <iostream.h>
void edit_arr(int a[]);
int main() {
int i,num[ 4] = { 2, 5}; 2 5 0 04 25int i,num[ 4] = { 2, 5};
edit_arr(num);
for(i=0;i<4;i++)
cout << num[i];
return 0 ;
}
void edit_arr(int a[]) {
2num[0] num[1] num[2] num[3]
5 0 0
a[0] a[1] a[2] a[3]
4 25
4 25 0 0
�������:
void edit_arr(int a[]) {
int i;
for(i=0;i<4;i++)
a[i] = a[i]*a[i];
}
QUIZQUIZ
1.=&��4�(/0&��1�(15.� sum_one_to_n ^�.&/0&��1�((49=��4������:������������E(=8�(�(��;� 1 ��� �,�=��%&)%��,����E()%�',�����9&��% 1 =(+�&=8�(�(��;�-�&�,%��=8�(�(��;�-�&�,%��
2.=&��4�(J������*(������)%�=8�(�(��;�=��'7�*1� 1 ��� =��(�9(
J������=���-&)%���&',�����9&��% 1 =(+�&=8�(�(��;����(�9(
�8��(-*���%�(34.)8�(�O)%�',�����7%*(/0&��1�( sum_one_to_n
�8������%�(34.���)%�=��'7�*1��,��%�(34.��-&)%�',���*����7%*(/0&��1�(�8������%�(34.���)%�=��'7�*1��,��%�(34.��-&)%�',���*����7%*(/0&��1�(
Main(6�%���&���=���)%�=��'7�*1��%������%� 1 ��5�6�%)
�:�.������ =&��4�(J�������:5.���)%��7&�\-��& Array 2 ���� �(�- 7 x 8 J-���4��*1�&�( Function Max
int maxarray(int arr[ ]) int maxarray(int arr[ ]) {Int i,j,max;
max = arr[0,0];for (i=0;i<7;i++)
for(j=0;j<8;j++)if (max<arr[i,j])
max = arr[i,j];max = arr[i,j];return max;
}