ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΛΓΟΡΙΘΜΟΙ ΕΡΓΑΣΤΗΡΙΟ Κωδικός Θ: ΤΠ3001, Κωδικός Ε: ΤΠ3101 (ΜΕΥ/Υ) Ώρες (Θ - Ε): 4-2 Προαπαιτούμενα: Δρ. ΒΙΔΑΚΗΣ ΝΙΚΟΣ
ΕΡΓΑΣΤΗΡΙΟ 2 Arrays
Θεωρία Θεωρία Έννοιες Κανόνες Παραδείγματα Ασκήσεις Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 3
Έννοιες Θεωρία Έννοιες Κανόνες Παραδείγματα Ασκήσεις Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 4
Κανόνες Θεωρία Έννοιες Κανόνες Παραδείγματα Ασκήσεις Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 5
Παραδείγματα σε Java public class MathLibraryExample public static void main(string[] args) int i = 7; int j = -9; double x = 72.3; double y = 0.34; public class MathLibraryExample public static void main(string[] args) int i = 7; int j = -9; double x = 72.3; double y = 0.34; System.out.println("i is " + i); System.out.println("j is " + j); System.out.println("x is " + x); System.out.println("y is " + y); // The absolute value of a number is equal to // the number if the number is positive or // zero and equal to the negative of the number // if the number is negative. System.out.println(" " + i + " is " + Math.abs(i)); System.out.println(" " + j + " is " + Math.abs(j)); System.out.println(" " + x + " is " + Math.abs(x)); System.out.println(" " + y + " is " + Math.abs(y)); // Truncating and Rounding functions // You can round off a floating point number // to the nearest integer with round() System.out.println(x + " is approximately " + Math.round(x)); System.out.println(y + " is approximately " + Math.round(y)); // The "ceiling" of a number is the // smallest integer greater than or equal to // the number. Every integer is its own // ceiling. System.out.println("The ceiling of " + i + " is " + Math.ceil(i)); System.out.println("The ceiling of " + j + " is " + Math.ceil(j)); System.out.println("The ceiling of " + x + " is " + Math.ceil(x)); System.out.println("The ceiling of " + y + " is " + Math.ceil(y)); Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 6
Παραδείγματα σε Java public class MathLibraryExample public static void main(string[] args) int i = 7; int j = -9; double x = 72.3; double y = 0.34; // The "floor" of a number is the largest // integer less than or equal to the number. // Every integer is its own floor. System.out.println("The floor of " + i + " is " + Math.floor(i)); System.out.println("The floor of " + j + " is " + Math.floor(j)); System.out.println("The floor of " + x + " is " + Math.floor(x)); System.out.println("The floor of " + y + " is " + Math.floor(y)); // Comparison operators // min() returns the smaller of the two arguments you pass it System.out.println("min(" + i + "," + j + ") is " + Math.min(i,j)); System.out.println("min(" + x + "," + y + ") is " + Math.min(x,y)); System.out.println("min(" + i + "," + x + ") is " + Math.min(i,x)); System.out.println("min(" + y + "," + j + ") is " + Math.min(y,j)); public class MathLibraryExample public static void main(string[] args) int i = 7; int j = -9; double x = 72.3; double y = 0.34; // There's a corresponding max() method // that returns the larger of two numbers System.out.println("max(" + i + "," + j + ") is " + Math.max(i,j)); System.out.println("max(" + x + "," + y + ") is " + Math.max(x,y)); System.out.println("max(" + i + "," + x + ") is " + Math.max(i,x)); System.out.println("max(" + y + "," + j + ") is " + Math.max(y,j)); // The Math library defines a couple // of useful constants: System.out.println("Pi is " + Math.PI); System.out.println("e is " + Math.E); // Trigonometric methods // All arguments are given in radians // Convert a 45 degree angle to radians double angle = 45.0 * 2.0 * Math.PI/360.0; System.out.println("cos(" + angle + ") is " + Math.cos(angle)); System.out.println("sin(" + angle + ") is " + Math.sin(angle)); // Inverse Trigonometric methods // All values are returned as radians double value = 0.707; System.out.println("acos(" + value + ") is " + Math.acos(value)); System.out.println("asin(" + value + ") is " + Math.asin(value)); System.out.println("atan(" + value + ") is " + Math.atan(value)); Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 7
Παραδείγματα σε Java public class MathLibraryExample public static void main(string[] args) int i = 7; int j = -9; double x = 72.3; double y = 0.34; // Exponential and Logarithmic Methods // exp(a) returns e (2.71828...) raised // to the power of a. System.out.println("exp(1.0) is " + Math.exp(1.0)); System.out.println("exp(10.0) is " + Math.exp(10.0)); System.out.println("exp(0.0) is " + Math.exp(0.0)); // log(a) returns the natural // logarithm (base e) of a. System.out.println("log(1.0) is " + Math.log(1.0)); System.out.println("log(10.0) is " + Math.log(10.0)); System.out.println("log(Math.E) is " + Math.log(Math.E)); // pow(x, y) returns the x raised // to the yth power. System.out.println("pow(2.0, 2.0) is " + Math.pow(2.0,2.0)); System.out.println("pow(10.0, 3.5) is " + Math.pow(10.0,3.5)); System.out.println("pow(8, -1) is " + Math.pow(8,-1)); // sqrt(x) returns the square root of x. for (i=0; i < 10; i++) System.out.println( "The square root of " + i + " is " + Math.sqrt(i)); // Finally there's one Random method // that returns a pseudo-random number // between 0.0 and 1.0; System.out.println("Here's one random number: " + Math.random()); System.out.println("Here's another random number: " + Math.random()); Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 8
Παραδείγματα σε C Παράδειγμα με μονοδιάστατο πίνακα #include <stdio.h> main() int n[ 10 ]; /* n is an array of 10 integers */ int i,j; /* initialize elements of array n to 0 */ for ( i = 0; i < 10; i++ ) n[ i ] = i + 100; /* set element at location i to i + 100 */ /* output each array element's value */ for (j = 0; j < 10; j++ ) printf("element[%d] = %d\n", j, n[j] ); Copyright: http://www.tutorialspoint.com/cprogramming/c_arrays.htm Παράδειγμα με πίνακα δύο διαστάσεων #include <stdio.h> main() int x; int y; int array[8][8]; /* Declares an array like a chessboard */ for ( x = 0; x < 8; x++ ) for ( y = 0; y < 8; y++ ) array[x][y] = x * y; /* Set each element to a value */ printf( "Array Indices:\n" ); for ( x = 0; x < 8;x++ ) for ( y = 0; y < 8; y++ ) printf( "[%d][%d]=%d ", x, y, array[x][y] ); printf( "\n" ); Copyright: http://www.cprogramming.com/tutorial/c/lesson8.html Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 9
Παραδείγματα σε C Παράδειγμα που τοποθετεί Ν ακέραιους σε ένα array και βρίσκει το άθροισμα των αρνητικών αριθμών, το άθροισμα των θετικών αριθμών και το μέσο όρο τους. #include <stdio.h> #define MAXSIZE 10 main() int array[maxsize]; int i, num, negative_sum = 0, positive_sum = 0; float total = 0.0, average; printf ("Enter the value of N \n"); scanf("%d", &num); printf("enter %d numbers (-ve, +ve and zero) \n", num); for (i = 0; i < num; i++) scanf("%d", &array[i]); printf("input array elements \n"); for (i = 0; i < num; i++) printf("%+3d\n", array[i]); /* Summation starts */ for (i = 0; i < num; i++) if (array[i] < 0) negative_sum = negative_sum + array[i]; else if (array[i] > 0) positive_sum = positive_sum + array[i]; else if (array[i] == 0) ; total = total + array[i] ; average = total / num; printf("\n Sum of all negative numbers = %d\n", negative_sum); printf("sum of all positive numbers = %d\n", positive_sum); printf("\n Average of all input numbers = %.2f\n", average); Copyright: http://www.sanfoundry.com/c-program-sum-average-array/ Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 10
Παραδείγματα σε C Παράδειγμα #1 με χρήση του math.h Παράδειγμα #2 με χρήση του math.h #include <stdio.h> #include <stdlib.h> /* abs */ #include <math.h> /* fabs */ main() int i = 4; int j = -49; int r = 1; float x = 74.4; float y = 0.44; float z = -93.1; printf("i is %d\n",i); printf("j is %d\n",j); printf("j is %d\n",r); printf("x is %f\n",x); printf("y is %f\n",y); printf("y is %f\n",z); printf(" %d is %d\n",i,abs(i)); printf(" %d is %d\n",j,abs(j)); printf(" %d is %d\n",r,abs(r)); #include <stdio.h> #include <math.h> /* round, ceil */ main() int i = 4; int j = -49; int r = 1; float x = 74.4; float y = 0.4; float z = -93.1; printf("i is %d\n",i); printf("j is %d\n",j); printf("j is %d\n",r); printf("x is %f\n",x); printf("y is %f\n",y); printf("y is %f\n",z); //round printf("%f is approximately %f\n",x,round(x)); printf("%f is approximately %f\n",y,round(y)); printf("%f is approximately %f\n",z,round(z)); //ceil printf("the ceiling of %d is %d\n",i,(int)ceil(i)); printf("the ceiling of %d is %d\n",j,(int)ceil(j)); printf(" %f is %f\n",x,fabs(x)); printf("the ceiling of %d is %d\n",r,(int)ceil(r)); printf(" %f is %f\n",y,fabs(y)); printf("the ceiling of %f is %d\n",x,(int)ceil(x)); printf(" %f is %f\n",z,fabs(z)); printf("the ceiling of %f is %d\n",y,(int)ceil(y)); printf("the ceiling of %f is %d\n",z,(int)ceil(z)); getchar(); getchar(); Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 11
Ασκήσεις για Java Δημιουργείστε 2 πίνακες, 1 τύπου Integer μεγέθους 5 και 1 τύπου double μεγέθους 5 Βάλτε τις παρακάτω τιμές 7, -9, 5, -3, 27 και -72.23, 0.34, 13.76, -45.23, 56.98 αντίστοιχα Αλλάξτε τον κώδικα έτσι ώστε να λειτουργεί με τους πίνακες και όχι με τις μεταβλητές. Δημιουργήστε έναν τρίτο πίνακα 2 διαστάσεων που να κρατάει τις τιμές του ceiling και του floor. Γεμίστε τον με ένα for loop που να κάνει χρήση του μήκους (length) του πίνακα και εμφανίστε τα περιεχόμενα του στην οθόνη. Μετά κάντε sort τα στοιχεία του πίνακα 3 και εμφανίστε τα στην οθόνη. Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 12
Ασκήσεις για C Δημιουργείστε 2 πίνακες τύπου integer διαστάσεων 3x3. Στον πίνακα input να εισάγονται τιμές από το πληκτρολόγιο ενώ o πίνακας result θα χρησιμοποιηθεί για την αποθήκευση των αποτελεσμάτων. Στην πρώτη γραμμή του πίνακα result, εισάγετε τις απόλυτες τιμές των τιμών που βρίσκονται στην πρώτη γραμμή του πίνακα input χρησιμοποιώντας την συνάρτηση abs(). Αντίστοιχα, στην δεύτερη γραμμή να χρησιμοποιηθεί η συνάρτηση round() και στην τρίτη η συνάρτηση ceil() Εμφανίστε τα περιεχόμενα των δύο πινάκων στην οθόνη. Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 13