Προγραμματισμού C++ // Write a program which defines 2 integer constants and prints their sum, difference and //product. //Να γραφεί πρόγραμμα το οποίο ορίζει δύο ακέραιες σταθερές και τυπών το άθροισμα, //τη διαφορά και το γινόμενο τους. #include <iostream> using namespace std; const int n1 = 5; const int n2 = 7; int sum = n1 + n2; int difference = n2 - n1; int product = n1 * n2; cout << "Sum = " << sum << endl; cout << "Difference = " << difference << endl; cout << "Product = " << product << endl; //Write a program which asks the user to input a string on the keyboard and then prints //the length of the string. //Να γραφεί πρόγραμμα το οποίο ζητά από τον χρήση να εισάξει μια σειρά από //χαρακτήρες από το πληκτρολόγιο και τυπώνει το μέγεθος της. #include <iostream> #include <string> using namespace std; string line; // cin >> line; getline(cin,line); cout << "Length = " << line.length(); Δείγματα Ασκήσεων στη Σελίδα: 1/4 Copyright ECDL Cyprus 20011
Προγραμματισμού C++ // Write a program which asks the user to input 5 integers (one per line) and then prints //the sum of the integers which are even. // Να γραφεί πρόγραμμα το οποίο ζητά από τον χρήστη να εισάξει 5 ακέραιους αριθμούς //(ένα σε κάθε γραμμή) και μετά τυπώνει το άθροισμα των ζυγών αριθμών. #include <iostream> using namespace std; int i,n,sum = 0; for (i=0; i<5; i++) cin >> n; if (n%2 == 0) sum = sum + n; cout << "Sum of even numbers = " << sum << endl; //Write a program which asks the user to input 5 integers (one per line), stores them in //an array, and then prints them in the reverse order in which they were input. // Να γραφεί πρόγραμμα το οποίο ζητά από τον χρήστη να εισάξει 5 ακέριους αριθμούς //(ένα σε κάθε γραμμή), τους αποθηκεύει σε ένα πίνακα και μετά τους τυπώνει με την //αντίθετη σειρά από αυτή με την οποία εισήχθηκαν. #include <iostream> using namespace std; int i,n,a[5]; for (i=0; i<5; i++) cin >> n; a[i]=n; //print the numbers for (i=4;i>=0; i--) cout << a[i] << endl; Δείγματα Ασκήσεων στη Σελίδα: 2/4 Copyright ECDL Cyprus 20011
Προγραμματισμού C++ // Write a program which creates a compound data structure describing a box which has //4 fields (height,width,depth,colour) and outputs a line with the volume and colour of the //box. // Να γραφεί πρόγραμμα το οποίο δημιουργεί μία σύνθετη δομή δεδομένων η οποία //περιγράφει ένα κιβώτιο το οποίο έχει έχει 4 πεδία (ύψος, πλάτος, βάθος και χρώμα) και //τυπώνει μια γραμμή με τον όγκο και το χρώμα του κιβωτίου. #include <iostream> using namespace std; class Box public: float height,width,depth; string colour; ; Box thebox; thebox.height=5.5; thebox.width=3.3; thebox.depth=8.4; thebox.colour="blue"; cout << "This is a " << thebox.colour << " box with a volume of " << thebox.height * thebox.depth * thebox.width << endl; Δείγματα Ασκήσεων στη Σελίδα: 3/4 Copyright ECDL Cyprus 20011
Προγραμματισμού C++ // Write a program which reads a text file, converts it to lowercase, and then writes it to //another file. // Να γραφεί πρόγραμμα το οποίο διαβάζει ένα αρχείο κειμένου, το μετατρέπει σε μικρά //γράμματα και το γράφει σε ένα άλλο αρχείο κειμένου. #include <iostream> #include <fstream> #include <string> using namespace std; void tolowercase(string &str) const int length = str.length(); for(int i=0; i < length; ++i) str[i] = tolower(str[i]); ifstream infile; ofstream outfile; string line; infile.open("input.txt"); outfile.open("output.txt"); while (! infile.eof() ) getline (infile,line); tolowercase(line); outfile << line; infile.close(); outfile.close(); Σημείωση: Οι πιο πάνω λύσεις είναι ενδεικτικές και δεν έιναι μοναδικές. Δείγματα Ασκήσεων στη Σελίδα: 4/4 Copyright ECDL Cyprus 20011
Προγραμματισμού C // Write a program which defines 2 integer constants and prints their sum, difference and //product. //Να γραφεί πρόγραμμα το οποίο ορίζει δύο ακέραιες σταθερές και τυπών το άθροισμα, //τη διαφορά και το γινόμενο τους. #include <stdio.h> const int n1 = 5; const int n2 = 7; int sum = n1 + n2; int difference = n2 - n1; int product = n1 * n2; printf("sum = %d\n",sum); printf("difference = %d\n",difference); printf("product = %d\n",product); // This exercise is presented in four different solutions in order to show the variety of // options with the C Programming Language. // Αυτή η άσκηση παρουσιάζεται με τέσσερις διαφορετικές λύσεις για να δείξει την //ποικιλία των επιλογών στη. //Write a program which asks the user to input a string on the keyboard and then prints //the length of the string - Version 1. //Να γραφεί πρόγραμμα το οποίο ζητά από τον χρήση να εισάξει μια σειρά από //χαρακτήρες από το πληκτρολόγιο και τυπώνει το μέγεθος της - Έκδοση 1. #include <stdio.h> #include <string.h> #define MAX 10000 char line[max]; gets(line); printf("length = %d\n",strlen(line)); Δείγματα Ασκήσεων στη Σελίδα: 1/6 Copyright ECDL Cyprus 2011
Προγραμματισμού C //Write a program which asks the user to input a string on the keyboard and then prints //the length of the string - Version 2. //Να γραφεί πρόγραμμα το οποίο ζητά από τον χρήση να εισάξει μια σειρά από //χαρακτήρες από το πληκτρολόγιο και τυπώνει το μέγεθος της. - Έκδοση 2 #include <stdio.h> #include <string.h> #define MAX 10000 char line[max]; // scanf only inputs from first character to space or enter scanf("%s",line); printf("length = %d\n",strlen(line)); //Write a program which asks the user to input a string on the keyboard and then prints //the length of the string - Version 3. #include <stdio.h> #include <string.h> #define MAX 10000 char line[max]; char letter; int index=0; while ((letter=getchar())!='\n') line[index++]=letter; printf("length = %d\n",strlen(line)); printf("length = %d\n",index); Δείγματα Ασκήσεων στη Σελίδα: 2/6 Copyright ECDL Cyprus 2011
Προγραμματισμού C //Write a program which asks the user to input a string on the keyboard and then prints //the length of the string - Version 4. #include <stdio.h> #define MAX 10000 char line[max]; char letter; int index=0; while ((letter=getchar())!='\n') line[index++]=letter; printf("length = %d\n",index); // Write a program which asks the user to input 5 integers (one per line) and then prints //the sum of the integers which are even. // Να γραφεί πρόγραμμα το οποίο ζητά από τον χρήστη να εισάξει 5 ακέραιους αριθμούς //(ένα σε κάθε γραμμή) και μετά τυπώνει το άθροισμα των ζυγών αριθμών. #include <stdio.h> int i,n,sum = 0; for (i=0; i<5; i++) scanf("%d",&n); if (n%2 == 0) sum = sum + n; printf("sum of even numbers = %d\n",sum); Δείγματα Ασκήσεων στη Σελίδα: 3/6 Copyright ECDL Cyprus 2011
Προγραμματισμού C //Write a program which asks the user to input 5 integers (one per line), stores them in //an array, and then prints them in the reverse order in which they were input. // Να γραφεί πρόγραμμα το οποίο ζητά από τον χρήστη να εισάξει 5 ακέριους αριθμούς //(ένα σε κάθε γραμμή), τους αποθηκεύει σε ένα πίνακα και μετά τους τυπώνει με την //αντίθετη σειρά από αυτή με την οποία εισήχθηκαν. #include <stdio.h> int i,n,a[5]; for (i=0; i<5; i++) scanf("%d",&n); a[i]=n; //print the numbers for (i=4;i>=0; i--) printf("%d\n",a[i]); Δείγματα Ασκήσεων στη Σελίδα: 4/6 Copyright ECDL Cyprus 2011
Προγραμματισμού C // Write a program which creates a compound data structure describing a box which has //4 fields (height,width,depth,colour) and outputs a line with the volume and colour of the //box. // Να γραφεί πρόγραμμα το οποίο δημιουργεί μία σύνθετη δομή δεδομένων η οποία //περιγράφει ένα κιβώτιο το οποίο έχει έχει 4 πεδία (ύψος, πλάτος, βάθος και χρώμα) και //τυπώνει μια γραμμή με τον όγκο και το χρώμα του κιβωτίου. #include <stdio.h> typedef struct float height,width,depth; char colour[100]; box; box thebox; thebox.height=5.5; thebox.width=3.3; thebox.depth=8.4; printf("please enter a colour:"); scanf("%s", &thebox.colour); printf("this is a %s box with a volume of %f\n",thebox.colour, thebox.height * thebox.depth * thebox.width); Δείγματα Ασκήσεων στη Σελίδα: 5/6 Copyright ECDL Cyprus 2011
Προγραμματισμού C // Write a program which reads a text file, converts it to lowercase, and then writes it to //another file. // Να γραφεί πρόγραμμα το οποίο διαβάζει ένα αρχείο κειμένου, το μετατρέπει σε μικρά //γράμματα και το γράφει σε ένα άλλο αρχείο κειμένου. #include <stdio.h> #include <ctype.h> FILE *ifp, *ofp; int c; ifp = fopen("input.txt","r"); ofp = fopen("output.txt","w"); c = getc(ifp) ; while (c!= EOF) putchar(c); c = getc(ifp); putc(tolower(c),ofp); fclose(ofp); fclose(ifp); Σημείωση: Οι πιο πάνω λύσεις είναι ενδεικτικές και δεν έιναι μοναδικές. Δείγματα Ασκήσεων στη Σελίδα: 6/6 Copyright ECDL Cyprus 2011
Προγραμματισμού JAVA //Exercise 1 //Write a program which defines 2 integer constants and prints their sum, difference and //product. //Να γραφεί πρόγραμμα το οποίο ορίζει δύο ακέραιες σταθερές και τυπών το άθροισμα, //τη διαφορά και το γινόμενο τους. public class Exercise1 public static void main (String[] args) final int n1 = 5; final int n2 = 7; int sum = n1 + n2; int difference = n2 - n1; int product = n1*n2; System.out.println("Sum = " + sum); System.out.println("Difference = " + difference); System.out.println("Product = " + product); //Exercise 2 //Write a program which asks the user to input a string on the keyboard and then prints //the length of the string. //Να γραφεί πρόγραμμα το οποίο ζητά από τον χρήση να εισάξει μια σειρά από //χαρακτήρες από το πληκτρολόγιο και τυπώνει το μέγεθος της. import java.io.bufferedreader; import java.io.inputstreamreader; public class Exercise2 public static void main(string[] args) throws Exception String inputline; BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); inputline = input.readline(); System.out.println(inputLine.length()); Δείγματα Ασκήσεων στη Σελίδα: 1/5 Copyright ECDL Cyprus 2011 Γλώσσα Προγραμματισμού JAVA
Προγραμματισμού JAVA //Exercise 3 //Write a program which asks the user to input 5 integers (one per line) and then prints //the sum of the integers which are even // Να γραφεί πρόγραμμα το οποίο ζητά από τον χρήστη να εισάξει 5 ακέραιους αριθμούς //(ένα σε κάθε γραμμή) και μετά τυπώνει το άθροισμα των ζυγών αριθμών. import java.io.bufferedreader; import java.io.inputstreamreader; public class Exercise3 public static void main(string[] args) throws Exception int sum=0; for (int i=0; i<5; i++) String inputline; BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); inputline = input.readline(); int n = Integer.parseInt(inputLine); if (n%2 == 0) sum=sum+n; System.out.println("Sum of even numbers = " + sum); Δείγματα Ασκήσεων στη Σελίδα: 2/5 Copyright ECDL Cyprus 2011 Γλώσσα Προγραμματισμού JAVA
Προγραμματισμού JAVA //Exercise 4 //Write a program which asks the user to input 5 integers (one per line), stores them in //an array, and then prints them in the reverse order in which they were input // Να γραφεί πρόγραμμα το οποίο ζητά από τον χρήστη να εισάξει 5 ακέριους αριθμούς //(ένα σε κάθε γραμμή), τους αποθηκεύει σε ένα πίνακα και μετά τους τυπώνει με την //αντίθετη σειρά από αυτή με την οποία εισήχθηκαν. import java.io.bufferedreader; import java.io.inputstreamreader; public class Exercise4 public static void main(string[] args) throws Exception int[] a = new int[5]; //read the numbers for (int i=0; i<5; i++) String inputline; BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); inputline = input.readline(); int n = Integer.parseInt(inputLine); a[i]=n; System.out.println(); //blank line //print the numbers for (int i=4; i>=0; i--) System.out.println(a[i]); Δείγματα Ασκήσεων στη Σελίδα: 3/5 Copyright ECDL Cyprus 2011 Γλώσσα Προγραμματισμού JAVA
Προγραμματισμού JAVA //Exercise 5 //write a program which creates a compound data structure describing a box which has 4 //fields (height,width,depth,colour) and outputs a line with the volume and colour of the //box // Να γραφεί πρόγραμμα το οποίο δημιουργεί μία σύνθετη δομή δεδομένων η οποία //περιγράφει ένα κιβώτιο το οποίο έχει έχει 4 πεδία (ύψος, πλάτος, βάθος και χρώμα) και //τυπώνει μια γραμμή με τον όγκο και το χρώμα του κιβωτίου. public class Exercise5 static class Box public double height,width,depth; public String colour; public static void main(string[] args) Box thebox = new Box(); thebox.height=5.5; thebox.width=3.3; thebox.depth=8.4; thebox.colour="blue"; System.out.println("This is a " + thebox.colour + " box with a volume of " + thebox.height*thebox.depth*thebox.width); Δείγματα Ασκήσεων στη Σελίδα: 4/5 Copyright ECDL Cyprus 2011 Γλώσσα Προγραμματισμού JAVA
Προγραμματισμού JAVA //Exercise 6 //write a program which reads a text file, converts it to lowercase, and then writes it to //another file // Να γραφεί πρόγραμμα το οποίο διαβάζει ένα αρχείο κειμένου, το μετατρέπει σε μικρά //γράμματα και το γράφει σε ένα άλλο αρχείο κειμένου. import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.filewriter; import java.io.filereader; import java.io.writer; public class Exercise6 public static void main(string[] args) throws Exception String inputfile="input.txt"; String outputfile="output.txt"; BufferedReader input = new BufferedReader(new FileReader(inputFile)); BufferedWriter output = new BufferedWriter(new FileWriter(outputFile)); String line; while (( line = input.readline())!= null) output.write(line.tolowercase()); output.newline(); output.close(); Σημείωση: Οι πιο πάνω λύσεις είναι ενδεικτικές και δεν έιναι μοναδικές. Δείγματα Ασκήσεων στη Σελίδα: 5/5 Copyright ECDL Cyprus 2011 Γλώσσα Προγραμματισμού JAVA
Προγραμματισμού PASCAL (* Write a program which defines 2 integer constants and prints their sum,difference, product *) (*Να γραφεί πρόγραμμα το οποίο ορίζει δύο ακέραιες σταθερές και τυπών το άθροισμα, τη διαφορά και το γινόμενο τους. *) PROGRAM Exercise1; CONST n1 : integer = 5; n2 : integer = 7; VAR sum, difference, product : integer; BEGIN sum := n1 + n2; difference := n2 - n1; product := n1 * n2; END. writeln ('Sum = ', sum); writeln ('Difference = ', difference); writeln ('Product = ', product); (* Write a program which asks the user to input a string on the keyboard and then prints the length of the string *) (*Να γραφεί πρόγραμμα το οποίο ζητά από τον χρήση να εισάξει μια σειρά από χαρακτήρες από το πληκτρολόγιο και τυπώνει το μέγεθος της*). PROGRAM Exercise2; VAR inputline : String; BEGIN readln (inputline); writeln (Length(inputLine)); END. Δείγματα Ασκήσεων στη Σελίδα: 1/4 Copyright ECDL Cyprus 2011 Γλώσσα Προγραμματισμού PASCAL
Προγραμματισμού PASCAL (* Write a program which asks the user to input 5 integers (one per line) and then prints the sum of the integers which are even *) (*Να γραφεί πρόγραμμα το οποίο ζητά από τον χρήστη να εισάξει 5 ακέραιους αριθμούς (ένα σε κάθε γραμμή) και μετά τυπώνει το άθροισμα των ζυγών αριθμών. *) PROGRAM Exercise3; VAR sum : integer = 0; i : integer; inputnumber : integer; BEGIN for i := 1 to 5 do Begin readln(inputnumber); if ((inputnumber mod 2) = 0) then sum := sum + inputnumber; End; writeln ('Sum of even numbers = ', sum); END. (* Write a program which asks the user to input 5 integers (one per line), stores them in an array, and then prints them in the reverse order in which they were input *) (*Να γραφεί πρόγραμμα το οποίο ζητά από τον χρήστη να εισάξει 5 ακέριους αριθμούς (ένα σε κάθε γραμμή), τους αποθηκεύει σε ένα πίνακα και μετά τους τυπώνει με την αντίθετη σειρά από αυτή με την οποία εισήχθηκαν. *) PROGRAM Exercise4; VAR i : integer; inputnumber : integer; a : array[1..5] of integer; BEGIN for i := 1 to 5 do Begin readln(inputnumber); a[i] := inputnumber; End; Δείγματα Ασκήσεων στη Σελίδα: 2/4 Copyright ECDL Cyprus 2011 Γλώσσα Προγραμματισμού PASCAL
Προγραμματισμού PASCAL END. writeln(); for i := 5 downto 1 do Begin writeln(a[i]); End; (* write a program which creates a compound data structure describing a box which has 4 fields (height,width,depth,colour) and outputs a line with the volume and colour of the box *) (* Να γραφεί πρόγραμμα το οποίο δημιουργεί μία σύνθετη δομή δεδομένων η οποία περιγράφει ένα κιβώτιο το οποίο έχει έχει 4 πεδία (ύψος, πλάτος, βάθος και χρώμα) και τυπώνει μια γραμμή με τον όγκο και το χρώμα του κιβωτίου. *) PROGRAM Exercise5; TYPE VAR Box = Record height, width, depth : real; colour : String; end; thebox : Box; BEGIN With thebox do Begin height := 5.5; width := 3.3; depth := 8.4; colour := 'blue'; End; writeln('this is a ', thebox.colour, ' box with a volume of ', thebox.height*thebox.depth*thebox.width:2:2); (* The ":2:2" is used to format the output data in a manner that is more understandable *) END. Δείγματα Ασκήσεων στη Σελίδα: 3/4 Copyright ECDL Cyprus 2011 Γλώσσα Προγραμματισμού PASCAL
Προγραμματισμού PASCAL (* write a program which reads a text file, converts it to lowercase, and then writes it to another file *) (*Να γραφεί πρόγραμμα το οποίο διαβάζει ένα αρχείο κειμένου, το μετατρέπει σε μικρά γράμματα και το γράφει σε ένα άλλο αρχείο κειμένου. *) PROGRAM Exercise6; VAR line : String; inputfile, outputfile : text; BEGIN assign (inputfile, 'input.txt'); reset (inputfile); assign (outputfile, 'output.txt'); rewrite (outputfile); While not EOF(inputFile) do Begin readln (inputfile, line); writeln (outputfile, lowercase(line)); End; close(inputfile); close(outputfile) END. Σημείωση: Οι πιο πάνω λύσεις είναι ενδεικτικές και δεν έιναι μοναδικές. Δείγματα Ασκήσεων στη Σελίδα: 4/4 Copyright ECDL Cyprus 2011 Γλώσσα Προγραμματισμού PASCAL