Professional Documents
Culture Documents
المصفوفات في برمجة الحاسب
المصفوفات في برمجة الحاسب
א
א
1
אא 154 א
א א
Wאא
Kאאאאא
Wאא
Wאא
Kאאאא J1
Kאאא J2
Kאאאאא J3
Kאאא J4
Kא J5
Kאאא J6
Kאאאא J7
Kאאאאאא J8
Wאאא
K٪100אאא
K10 Wאא
Wאא
K •
K •
K •
Wאא
Kאאא
-1-
אא 154 א
א א
W
،אאאאאאא א א
אאאאאא
א א K
KאאאאKא
Wאאא
،E א F א א א
אאאאאאא
K(Index)אאאאאאא،
אF א א א א אא א
K(Variable)אEא
WKאאאאא
1. int array1[];
2. array1[] = new int[9];
intא،intarray1אE1Fא
אאF אא 9E2FאKא
א Kאאא 9 E8
Wאא
int array1[] = new int[9];
Wאאא
int[] array1 = new int[9];
WE1-1Fאarray1אא
-2-
אא 154 א
א א
array1
0 0 0 0 0 0 0 0 0
7 8 6 Index Æ 0 1 2 3 4 5
E1-1F
WF א א א א א ،
א ،א א k ،array1[k]
אאאאn،KEאא
Kn-1
א،אאאא
א(Default Value)אאאא
WאאאKאא
int, byte, short, long Æ 0
double, float Æ 0.0
char Æ \u0000 א
String Æ null
Boolean Æ false
Wאאאא
int ages[]= {20, 18, 34, 42, 28};
،אא،agesאא
KאE2-1FאKאאאא
-3-
אא 154 א
א א
ages
20 18
34 42 28
Index Æ 0 1 2 3 4
E2-1F
،ages[3] Wא א אא ages א 42
Wאא42א
System.out.println(ages[3]);
Wאא،18531אאא
ages[1]=53;
W1-1W
// array1.java
1. public class array1{
2. public static void main(String args[]){
3. double a[]=new double[10];
4. for(int i=0; i<10; i++){
5. a[i]=i*2.5;
6. System.out.println(a[i]);
7. } // end for
8. } // end main
9. } // end class array1
-4-
אא 154 א
א א
Wא
אKא10doubleaאE3Fא
ii*2.5אאאאE5F
E6F א Kא א אא א
WאאE3-1FאKaא
E3-1F
W2-1W
// array2.java
1. import javax.swing.*;
2. public class array2{
3. public static void main(String args[]){
4. int b[]=new int[5];
5. String s;
6. for(int i=0; i<5; i++){
7. s=JOptionPane.showInputDialog("Enter a number:");
8. b[i]=Integer.parseInt(s);
9. } // end for
-5-
אא 154 א
א א
Wא
E9-6FאKאא 5 bאE4Fא
אE12-10FאKbא
،bא 5אא Kb
WאE4-1FאK60אאא
E4-1F
WאאE5-1Fא
-6-
אא 154 א
א א
E5-1F
W
K אאא(Index)א J1
Kאאאאא J2
א(new)אאאאא J3
K
(Default א א אא א א J4
K Kא(Type)אאאValue)
א א א אא J5
אאאא،array1.lengthW)Klength
K(array1
Wאאאאאא J6
array=new int[5];
Wאאאאאאאא J7
1.
int array[]={5, 3, 8, 9, 2};
2. int array[]=new int[] {5, 3, 8, 9, 2};
Wאאא
W3-1W
//array3.java
1. import javax.swing.*;
2. class array3{
3. public static void main(String args[]){
4. String s, output, title, str1, str2;
5. str1="Enter how many number you want to store in the array:";
6. str2="Enter number # ";
7. int n, odd=0;
8. s=JOptionPane.showInputDialog(str1);
9. n=Integer.parseInt(s);
10. int [] arr=new int[n];
11. output= " ";
12. for(int i=0; i<arr.length; i++){
13. s=JOptionPane.showInputDialog(str2+(i+1)+":");
14. arr[i]=Integer.parseInt(s);
15. output+=arr[i]+"\n ";
16. } //end for
17. for(int i=0; i<arr.length; i++)
18. if(arr[i]%2==1) odd++; // end for
19. title="The results of the example (1-3)";
20. output+="\nThere are "+odd+" odd numbers in the array";
21. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.INFORMATION_MESSAGE);
22. System.exit(0);
23. } //end main
24. } //end class array3
-8-
אא 154 א
א א
Wא
Knאאא،אE9-8Fא
F אאא n arrאE10Fא
nE16-12FאKEאאא
אKEarr.lengthאאF arrאא
oddאאא،arrאאאE18F
E6-1FאKאE21FאK EאאF
Wאא
E6-1F
WאאE7-1Fא
-9-
אא 154 א
א א
E7-1F
W4-1W
// array4.java
1. import javax.swing.*;
2. class array4{
3. public static void main(String args[]){
4. int a[]={2, 4, 5, -5, 3, 10, 4, 11, 1, 7, -2};
5. String title="The results of the Example (1-4)";
6. String results="";
20. }
21. }
Wא
אE17-7FאKאא aאE4Fא
אאאא،switchא
K א א “More than Five OR Less than One“ א
WאאE8-1Fא
E8-1F
W5-1W
// array5.java
1. import javax.swing.*;
2. class array5{
3. public static void main(String args[]){
4. double marks[] = new double[6];
5. String names[] = new String[6];
- 11 -
אא 154 א
א א
6. String s;
7. String t1 = ”Enter the Student's name:”;
8. String t2 = "Enter his mark:";
- 12 -
אא 154 א
א א
E9-1F
- 13 -
אא 154 א
א א
E9-1Fא
WאאE10-1Fא
E10-1F
- 14 -
אא 154 א
א א
W6-1W
// array6.java
1. import javax.swing.*;
2. class array6{
3. public static void main(String args[]){
4. int num[] = new int[]{4, 6, 2, 3, 9, 5, 7, 2, 4, 7};
5. String output="The Contents of the array are:\n";
6. String title="The results of the Example (1-6)";
7. for(int i=0; i<=num.length-1; i++)
8. output+=num[i]+" ";
9. output+="\nThe Contents of the array in reverse order are:\n";
10. for(int i=num.length-1; i>=0; i--)
11. output+=num[i]+" ";
12. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.INFORMATION_MESSAGE);
13. System.exit(0);
14. }
15. }
Wא
אאאאE11-10Fא
אFnumאאאK
E11-1Fא KEא num.length-1אא
Wאא
E11-1F
- 15 -
אא 154 א
א א
W(Sorting)א
א Kא אא א
Kאאאא
.(Bubble Sort)אאאאאא
W7-1
// array7.java
1. import javax.swing.*;
2. class array7{
3. public static void main(String args[]){
4. int num[] = new int[] {4, 6, 2, 3, 9, 5, 8, 1, 10, 7};
5. int temp;
6. String title="The results of the Example (1-7)";
7. String output="Array contents in original order:\n";
8. for(int i=0; i<num.length; i++)
9. output+=num[i]+" ";
10. for(int i=1; i<num.length; i++)
11. for(int j=0; j<num.length-1; j++)
12. if(num[j]>num[j+1]){
13. temp=num[j];
14. num[j]=num[j+1];
15. num[j+1]=temp;
16. }
17. output+="\n\nArray contents in ascending order:\n";
18. for(int i=0; i<num.length; i++)
19. output+=num[i]+" ";
20. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.INFORMATION_MESSAGE);
21. System.exit(0);
22. }
23. }
- 16 -
אא 154 א
א א
Wא
،num א א א E16-10F א
אאאE12Fאא
אאKאאאאאא
-18אFאE9-8אFאא
אE12-1FאK(Ascending)אאאא KE19
Wא
E12-1F
W8-1
// array8.java
1. import javax.swing.*;
2. class array8{
3. public static void main(String args[]){
4. JTextArea outArea= new JTextArea();
5. int mark[] = new int[] {78, 81, 52, 92, 48, 90, 66, 40, 96,84};
- 17 -
אא 154 א
א א
Wא
E23-14F א K(Descending) א א א א
א،אE19-17Fא،א
- 18 -
אא 154 א
א א
E13-1FאK אאאE22-20F
Wאא
E13-1F
- 19 -
אא 154 א
א א
W(Searching)אא
א א
א א א Kא אא
K(Binary Search)אא(linear Search)אאW
،אאאאאאא
א א אא א א א
،אאאאאKא
א א א
Kאאאאאאא אאאא
אאאאאאאאא
K
W9-1
// array9.java
1. import javax.swing.*;
2. class array9{
3. public static void main(String args[]){
4. int n[] = new int[10];
5. int num, k=-1;
6. String title="The results of the Example (1-9)";
7. String s, output="";
16. }
17. if(k!=-1)
18. output+="Found value in index "+k;
19. else
20. output+="Value not found";
21. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.PLAIN_MESSAGE);
22. System.exit(0);
23. }
24. }
Wא
א אאאאאא
numאאאאאאE16-12FאKא
אאאאא،א
אK1 Jkאאאא،k
kאאאאאאE20-17F
אאאאאאא kא،1 J
אאאE14-1FאKkא
Wא14א
E14 J1F
Wא7אאאE15-1Fא
- 21 -
אא 154 א
א א
E15 J1F
W10-1
// array10.java
1. import javax.swing.*;
2. class array10{
3. public static void main(String args[]){
4. int id[] = new int[] {2, 10, 1, 7, 4, 6, 3, 8, 5, 9};
5. String name[]= {"Ahmad", "Mohammed", "Naser", "Saleem",
"Adel", "Khaled", "Ali", "Kareem", "Jamil",
"Nader"};
Wא
אאאאאאא
אא idאאאE15-11FאKאא
א idאאאא،(no)א
א א א א א index א id א א
אאE19-16Fא،nameאאאא
“There is no student idאאאא אא
Kאאאאאwith this ID !!!”
K4אאאאאE16-1Fא
E16-1F
K13אאאאE17-1Fא
- 23 -
אא 154 א
א א
E17-1F
W11-1
// array11.java
1. import javax.swing.*;
2. class array11{
3. public static void main(String args[]){
4. int id[] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
5. String name[]= {"Ahmad", "Mohammed", "Naser", "Saleem",
"Adel", "Khaled", "Ali", "Kareem", "Jamil",
"Nader"};
15. middle=(low+high)/2;
16. if(no==id[middle]){
- 24 -
אא 154 א
א א
17. index=middle;
18. break;
19. }
20. else if(no<id[middle])
21. high=middle-1;
22. else
23. low=middle+1;
24. }
25. if(index!=-1)
26. output+=name[index];
27. else
28. output="There is no student with this ID !!!";
29. JOptionPane.showMessageDialog(null, output, title,
JOptionPane.PLAIN_MESSAGE)
30. System.exit(0);
31. }
32. }
Wא
E4אF idאאE10-1Fאאא
אKאאאאאאא
אאאE24-14F
אאאאאאאאאא
(while)אאאidאא
Kid אאא
W6אאאאאE18-1Fא
- 25 -
אא 154 א
א א
E18-1F
W19אאאאE19-1Fא
E19-1F
W12-1
// array12.java
12. }
Wא
אE23-20FאE12-9Fא،אאאא
،א keyאאאאא
אאאאאאאאא
K
KאאE20-1Fא
- 27 -
אא 154 א
א א
E20-1F
- 28 -
אא 154 א
א א
W(Two-Dimensional Arrays)אאא
אW،אא
א א א א Kא א
אאאאKE21-1Fאא،
W
1. int b[][];
2. b = new int[ 3 ][ 4 ];
אאאא، אאא
bאE20-1FאK
Kא
0 اﻟﻌﻤﻮد 1 اﻟﻌﻤﻮد 2 اﻟﻌﻤﻮد 3 اﻟﻌﻤﻮد
0 اﻟﺼﻒ b[0][3] b[0][2] b[0][1] b[0][0]
1 اﻟﺼﻒ
b[1][3] b[1][2] b[1][1] b[1][0]
2 اﻟﺼﻒ
b[2][3] b[2][2] b[2][1] b[2][0]
اﺳﻢ اﻟﻤﺼﻔﻮﻓﺔ
رﻗﻢ اﻟﺼــﻒ
رﻗﻢ اﻟﻌﻤـــﻮد
(21-1) ﺷﻜﻞ
Wאאאאא
int b[][] = { { 1, 2 }, { 3, 4 } };
- 29 -
אא 154 א
א א
4 5
E23-1F
א א א
Wאא
1. int a[][];
2. a = new int[ 2 ][ ]; // allocate rows
3. a[ 0 ] = new int[ 5 ]; // allocate row 0
4. a[ 1 ] = new int[ 3 ]; // allocate row 1
- 30 -
אא 154 א
א א
،אE2Fא،aאE1Fא
E4Fא،א E3Fא
KaאE24-1FאKא
0 0 0 0 0 A[0][4]
0 0 0
A[1][2]
E24-1F
- 31 -
אא 154 א
א א
Wאאא
W13-1
// array13.java
Wא
א ،אא array1 א E3F א
אE4FאK אא
א א א ، א ،אא array2
Kאאא،אאאא،
،אarray1אE10-6Fא
array1.length א array1 א א
- 32 -
אא 154 א
א א
W14-1
// array14.java
6. int sum;
7. System.out.println("The array is:");
- 33 -
אא 154 א
א א
8. System.out.println("\t\t[0]\t[1]\t[2]\t[3]");
15. System.out.println();
Wא
، ، א א
E14-7FאKE5-3Fאאgradesא
-16FאKאא gradesא
א E19 אF E21
sumאאא،E20אF
grades[i].lengthאאאא
sum א KEאF א i א
KאאE26-1FאKE17אF
- 34 -
אא 154 א
א א
E26-1F
W15-1
// array15.java
- 35 -
אא 154 א
א א
12. sum+=nums[1][k];
13. System.out.println("\nThe sume of elements on the 2nd row is:
"+sum);
14. sum=0;
15. for(int k=0; k<nums.length; k++)
16. sum+=nums[k][2];
17. System.out.println("\nThe sume of elements on the 3rd column is:
"+sum);
18. sum=0;
19. for(int i=0; i<nums.length; i++)
20. for(int j=0; j<nums[i].length; j++)
21. if(i==j) sum+=nums[i][j];
22. System.out.println("\nThe sume of elements on the main diagonal
is: "+sum);
23. sum=0;
24. for(int i=0; i<nums.length; i++)
25. for(int j=0; j<nums[i].length; j++)
26. if(i+j==nums.length-1) sum+=nums[i][j];
27. System.out.println("\nThe sume of elements on the secondary
diagonal is: "+sum);
28. System.out.println();
29. }
30. }
Wא
א א א ،א א א א א
FאאEאאF
Eא F א א א א
אאאאאא،
KEאאאאאאFא
E14-12FאKאE11-7Fא
-16FאKE1אאאFאאאא
KE2אאאFאאאאE18
-20F א nums א א א א
- 36 -
אא 154 א
א א
אאאE22Fאאא،E23
אאאE28-25FאKאא
אאאא א ،א
KאאE27-1FאKE27Fאא
E27-1F
- 37 -
אא 154 א
א א
W
אא 20 א א W2
אאKאאאאא
אאאאאWFKא
KEאא
א ، א אא N א א W4
א א K א
א א ،א א א א א א
אאאאאא
K“Not found in the array”
K א א W5
אFאאאאאא
אאאאKEאא
K2500
- 38 -
אא 154 א
א א
WאאW7
2 9 4 6 4
6 1 9 3 1
6 5 2 9 9
8 3 4 7 3
3 6 5 3 5
Wא
Kאאאאאאאא J
Kאאאאא J
Kאאאאא J
Kאאאאא J
- 39 -