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

Slides:



Advertisements
Similar presentations
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Advertisements

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
int getThird(int *arr){ return arr[3]; } In all these examples “n” is the size of the input e.g. length of arr O(1) And what two things are wrong with.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
Multi-Dimensional Arrays Rectangular & Jagged Plus: More 1D traversal.
CS1101X: Programming Methodology Recitation 7 Arrays II.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
TIC TAC TOE. import java.util.Scanner; import java.util.Random; public class PlayTTT{ public static void main(String[]args){ Scanner reader = new Scanner(System.in);
Insertion Sort while some elements unsorted: Using linear search, find the location in the sorted portion where the 1 st element of the unsorted portion.
A: A: double “4” A: “34” 4.
1 מבוא למדעי המחשב הרצאה 5: פונקציות. 2 מבוא לפונקציות חלוקה של אלגוריתם לתת משימות: משימה - פונקציה: דוגמאות מציאת המקסימלי מבין שני איברים האינדקס של.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
CPSC 233 Tutorial Xin Liu Feb 14, Tips on keyboard input How to detect that the user just hit enter when prompted for a string import java.util.Scanner;
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
CS1101X: Programming Methodology Recitation 6 Arrays I.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 10.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
(Dreaded) Quiz 2 Next Monday.
תרגול חזרה לבוחן נמרוד מילוא.
Lecture 18: Nested Loops and Two-Dimensional Arrays
Chapter 2 Clarifications
CSC111 Quick Revision.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
יסודות מדעי המחשב – תרגול 3
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Alg2_1c Extra Material for Alg2_1
Computer Programming Methodology Input and While Loop
Introduction to Methods in java
Counted Loops.
TK1114 Computer Programming
מצביעים והקצאה דינאמית
Something about Java Introduction to Problem Solving and Programming 1.
CS 200 Arrays, Loops and Methods
Computer Programming תרגול 6 פונקציות
Arrays Declare the Array of 100 elements Notes
הרצאה 3 אלמנטים בסיסיים בשפה
1-Dimensional Arrays 2-Dimensional Arrays => Read section 1.4
Review Operation Bingo
יסודות מדעי המחשב – תרגול 4
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
הקצאות דינאמיות קרן כליף.
The for-loop and Nested loops
מיונים וחיפושים קרן כליף.
Arrays.
COMPUTER 2430 Object Oriented Programming and Data Structures I
An Introduction to Java – Part I, language basics
ניתוח מערכות מידע תכנות ב C#
תרגול 4.
Computer Programming Fall 2012 תרגול 6 פונקציות
Multidimensional Arrays
Method Revision.
Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort. Complexity analysis.
Algorithms Lakshmish Ramaswamy.
Recursion Problems.
Object Oriented Programming
CS150 Introduction to Computer Science 1
Repetition Statements
Array Review Selection Sort
Data Structures and Algorithms 2/2561
Introduction to java Part I By Shenglan Zhang.
MIS 222 – Lecture 12 10/9/2003.
Presentation transcript:

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

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

תרגיל 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

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

תרגיל 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

תרגיל 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"); {

תרגיל 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(); {

תרגיל 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

תרגיל 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

תרגיל 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;

תרגיל 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;

תרגיל 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;