Presentation is loading. Please wait.

Presentation is loading. Please wait.

תרגול 6 1 1 Introduction to C - Fall 2010 - Amir Menczel.

Similar presentations


Presentation on theme: "תרגול 6 1 1 Introduction to C - Fall 2010 - Amir Menczel."— Presentation transcript:

1 תרגול 6 1 1 Introduction to C - Fall Amir Menczel

2 תרגיל 1 מערך דו-מימדי (NXN) יקרא "מאוזן-אלכסון" אם סכום האיברים באלכסון הראשי שלו (המודגש) שווה למחצית מסכום איברי כל המערך. כתבו את הפונקציה isBalanced המקבלת מערך בגודל NXN ומחזירה האם המערך הינו "מאוזן-אלכסון". לדוגמא: המערך הבא הינו "מאוזן-אלכסון" public static boolean isBalanced (int [][] arr) } { 2

3 תרגיל 1 - פתרון public static boolean isBalanced (int [][] arr)}
int all = 0, diagonal = 0; for (int i = 0; i < arr.length; i++){ for (int j = 0; j < arr[i].length; j++){ all += arr[i][j]; if (i == j) diagonal += arr[i][j]; { return 2*diagonal == all; 3

4 תרגיל 2 מערך דו-מימדי יקרא "ממויין שורות" אם כל השורות בו ממויינות בסדר עולה. מערך דו-מימדי ייקרא "ממויין עמודות" אם כל העמודות בו ממויינות בסדר עולה. מערך ייקרא "ממויין" אם המערך "ממויין שורות" וגם "ממויין עמודות". כתבו פונקציה המקבלת מערך דו-מימדי ומחזירה האם המערך "ממויין". לדוגמא: המערך הבא "ממויין": 7 6 4 1- 14 10 25 21 3 60 52 public static boolean isSorted (int [][] arr) } {

5 תרגיל 2 - פתרון public static boolean isSorted (int [][] arr){
return isColumnSorted(arr) && isRowSorted(arr); { public static boolean isRowSorted(int[][] arr) { for (int i = 0; i < arr.length; i++) for (int j = 1 ; j < arr[0].length; j++) if (arr[i][j - 1] > arr[i][j]) return false; return true; public static boolean isColumnSorted(int[][] arr) { for (int i = 1; i < arr.length; i++) for (int j = 0 ; j < arr[0].length; j++) if (arr[i-1][j] > arr[i][j]) 5

6 תרגיל 3 3 2 1 4 8 11 -2 9 7 נגדיר "פרח" במערך דו מימדי כך:
3 2 1 4 8 11 -2 9 7 נגדיר "פרח" במערך דו מימדי כך: 3X3- האיברים מתוך תת מערך בגודל - האיבר המרכזי במערך הוא "לב הפרח". - ארבעת האיברים הצמודים לו בפינותיו הם "עלי הפרח". - הערך של "לב הפרח" שווה לסכום ערכי "עלי הפרח". א. כתוב תוכנית הקולטת למערך בגודל 18X15מספרים שלמים ב. מערך נקרא "פרחוני", אם יש בו לפחות 5 "פרחים". כתבו תוכנית שתחזיר אמת במידה ומערך הוא "פרחוני" ושקר אחרת public static void main( String [] args) } int arr[][] = new int[15][18];//input array inputArr(arr); if (isFlowery(arr)) System.out.println("The array is flowery"); else System.out.println("The array isn't flowery"); {

7 תרגיל 3 – פתרון חלק א' public static void inputArr(int[][] arr) {
Scanner sc = new Scanner(System.in); int i,j;//loop counters System.out.println("Please enter 15 rows and 18 columns"); for(i=0;i<arr.length;i++){ for(j=0;j<arr[i].length;j++){ arr[i][j] = sc.nextInt(); {

8 תרגיל 3 – פתרון חלק ב' private static boolean isFlowery(int[][] arr) {
int count=0;//number of flowers for(int i=0;i<arr.length;i++) for(int j=0;j<arr[i].length;j++) if (isFlower(arr,i,j)) count++; return count >= 5; { public static boolean isFlower(int[][] arr, int i, int j) { int sum;//sum of flower's elements if(i==0 || i == arr.length-1 || j==0 || j == arr[0].length-1) return false; sum=arr[i-1][j-1]+arr[i-1][j+1]+arr[i+1][j-1]+arr[i+1][j+1]; if(arr[i][j]==sum) return true; else

9 תרגיל 4 תרגיל: כתבו פונקציה המקבלת מערך של שמות סטודנטים בקורס ומערך של ציונים ומחזירה מערך המכיל את כל שמות הסטודנטים בעלי ציון גדול מהממוצע ממויינים לפי הציונים. לדוגמא: public static void main (String[] args) } String [] students = { "A", "B", "C", "D", "E", "F", "G" }; int [] grades = { 100, 80, 90, 100, 40, 90, 70 }; String[] excellent = getExcellentStudents(students, grades); for (int i = 0; i < excellent.length; i++) System.out.print(excellent[i] + " "); { יודפס (הממוצע הוא 81.42): C F D A

10 תרגיל 4 - פתרון public static String[] getExcellentStudents(String[]students, int[]grades){ double avg = calcAverage(grades); int excellentCount = aboveAverage(grades, avg); String [] excellentStudent = new String[excellentCount]; int [] excellentGrades = new int [excellentCount]; for (int i = 0, j = 0; i < grades.length; i++){ if (grades[i] >= avg){ excellentGrades[j] = grades[i]; excellentStudent[j] = students[i]; j++; { selectionSort(excellentGrades , excellentStudent); return excellentStudent;

11 תרגיל 4 - פתרון public static int aboveAverage(int[] grades, double avg) { int count = 0; for (int i = 0; i < grades.length; i++) } if (grades[i] > avg) count++; { return count; public static double calcAverage(int[] grades) { double avg = 0; avg += grades[i]; return avg / grades.length;

12 תרגיל 4 - פתרון public static void selectionSort (int [] arr, String[] strs){ for(int i=0; i<arr.length; i++){ int min_pos=i; for(int j=i+1; j<arr.length; j++){ if ( arr[j] < arr[min_pos] ) min_pos=j; { String tmp = strs[i]; // help variable strs[i] = strs[min_pos]; strs[min_pos] = tmp; int temp = arr[i]; // help variable arr[i]=arr[min_pos]; arr[min_pos]=temp;


Download ppt "תרגול 6 1 1 Introduction to C - Fall 2010 - Amir Menczel."

Similar presentations


Ads by Google