/******************************
* ΛΥΣΕΙΣ ΘΕΜΑΤΩΝ ΙΟΥΝΗ 2016 *
*****************************/
/*************************************************************************
* ΘΕΜΑ 1ο
*************************************************************************/
//thema1_Jun16.hpp
#ifndef thema1_Jun16_hpp // περιορισμός ορισμού του "thema1_Jun16.hpp" μόνο μια φορά
#define thema1_Jun16_hpp
#define check(x,y,z) (((x)%(y) == z)?1:0)
#include <cmath>
void count(int n, int a[], int b[], int c[], int &count1, int &count2, int &count3){
for (int i = 0; i < n; i++){
if (b[i] == 0) { // δεν μπορώ να διαιρέσω με το μηδέν
count1++;
}
else if (check(a[i],b[i],c[i])){
count2++;
}
else if (!check(a[i],b[i],c[i])){
count3++;
}
}
}
void comp(int n, int a[], int b[], int c[]){
for (int i = 0; i < n; i++) c[i] = fmin(a[i], b[i]);
}
#endif
//thema1_Jun16.cpp
#include <iostream>
#include <cstdlib> // για την rand()
#include "thema1_Jun16.hpp"
#define NMAX 100
using namespace std;
int main(){
int x[NMAX],y[NMAX],z[NMAX], unablediv = 0, divtrue = 0, divfalse = 0;
// δίνω τυχαίες τιμές στα διανύσματα x,y,z
// δείτε: http://www.cplusplus.com/reference/cstdlib/rand/
for (int i = 0; i < NMAX; i++){
// cin >> x[i]; may replace rand.
x[i] = rand()%50 - 20;
y[i] = rand()%50 - 20;
z[i] = rand()%50 - 20;
cout << "x,y,z = " << x[i] << ", " << y[i] << ", " << z[i] << "\n";
}
count(NMAX, x, y, z, unablediv, divtrue, divfalse);
cout << unablediv << " " << divtrue << " " << divfalse << "\n";
comp(NMAX, x, y, z);
for (int i = 0; i < NMAX; i++) cout << z[i] << ",\t";
cout << "\n";
return 0;
}
/*************************************************************************
* ΘΕΜΑ 2ο
*************************************************************************/
// thema2_Jun16.cpp
#include <iostream>
using namespace std;
// Συνάρτηση που επιστρέφει τον n-sto όρο της ακολουθίας
double fract(int n){
if (n == 1) return 1.0;
else {
n--;
return 1.0/(2.0 + fract(n));
}
}
// Συνάρτηση που αποθηκεύει στην διεύθυνση της res το άθροισμα της σειράς
// των πρώτων n όρων της ακολουθίας
void fractsum(int n, double &res){
if (n == 1) res = 1.0;
else {
fractsum(n-1, res); // καλώ την αναδρομή μέχρι να γίνει n = 1
res = res + fract(n); // προσθέτω τον νέο όρο
}
}
double fractsum1(int n){
double res;
if (n == 1) return 1.0;
else {
res = fractsum1(n-1) + fract(n); // προσθέτω τον νέο όρο
// καλώ την αναδρομή μέχρι να γίνει n = 1
return res; // επιστρέφω το αποτέλεσμα της σειράς
}
}
// main function
int main(){
int n;
double result = 0.0;
cout << "Δώστε το n\n";
cin >> n;
cout.precision(16);
for (int i = 1; i <= n; i++){
cout << fract(i) << ", " << "\n";
}
// χρησιμοποιώ την fractsum()
fractsum(n, result);
cout << "result = " << result << "\n";
// χρησιμοποιώ την fractsum1()
result = fractsum1(n);
cout << "result = " << result << "\n";
return 0;
}
/*************************************************************************
* ΘΕΜΑ 3ο
*************************************************************************/
// thema3_Jun16.cpp
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int n;
cout << "Dwse to n\n";
cin >> n;
float *a = new float[n], *pa = &a[0];
float lambda1 = 0, lambda8 = 0;
// υπολογίζω τις νόρμες
for (int i = 0; pa < a + n; pa++, i++){ // πρώτα η L_1
cout << "Dwse to a(" << i << ")\n";
cin >> *pa;
lambda1 += abs(*pa); // lambda1 = lambda1 + abs(*pa)
}
// ξεκινάω τον δείκτη από το a[n-1]
pa--;
// for (; pa >= a; pa--){
for (; pa > a + (-1); pa--){ // μετά η L_inf
lambda8 = max(lambda8, abs(*pa));
}
// εκτύπωση αποτελεσμάτων
cout << "Norm L_1 = " << lambda1 << "\n";
cout << "Norm L_inf = " << lambda8 << "\n";
delete [] a;
return 0;
}
/*************************************************************************
* ΘΕΜΑ 4ο
*************************************************************************/
// thema4_Jun16.cpp
#include <iostream>
#include <string>
#define NMAX 35
using namespace std;
#define FOR(i,start,end) for (int i=start; i < end; i++)
class Student{
private:
string onoma, eponymo;
unsigned int AM;
// NMAX is a macro defined above
float grades[NMAX];
public:
Student(string onom, string epon, int am){
onoma = onom;
eponymo = epon;
AM = am;
//the following loop
//for (int i = 0; i < NMAX; i++) grades[i] = 0.0;
//can been substituted by the macro defined above
FOR(i, 0, NMAX) grades[i] = 0.0;
}
// copy constructor
Student(const Student &st){
onoma = st.onoma;
eponymo = st.eponymo;
AM = st.AM;
FOR(i, 0, NMAX) grades[i] = st.grades[i];
}
~Student() {
cout << "Διαγραφή !\n";
cout << "Όνομα:" << onoma << "\n";
cout << "Επώνυμο:" << eponymo << "\n";
cout << "AM:" << AM << "\n\n";
//cout << "Μάθημα" << "--" << "Βαθμός\n";
//FOR(i, 0, NMAX) cout << i << " " << grades[i] << "\n"; "
}
void set(int mathima, float grade){
grades[mathima] = grade;
}
bool test(float grades[]){
int cpass = 0;
FOR(i,0,NMAX) {
if (grades[i] >= 5) cpass++;
}
return (cpass == NMAX); // αν cpass == 35 τότε True, αλλιώς False
}
float mo_grades(){
float sum = 0.0;
FOR(i,0,NMAX) {
if (this->grades[i] >= 5) sum += this->grades[i];
}
return sum/float(NMAX);
}
bool operator== (const Student &stu){
if (onoma == stu.onoma && eponymo == stu.eponymo && AM == stu.AM){
FOR(i, 0, NMAX){
if (grades[i] != stu.grades[i]) return false;
}
return true;
}
else return false;
}
friend void print(Student); // τώρα η print() έχει πρόσβαση στις μεταβλητές κ' συναρτήσεις της Student
};
void print(Student s){
cout << "Όνομα: " << s.onoma << "\n"
<< "Επώνυμο: " << s.eponymo << "\n"
<< "AM: " << s.AM << "\n"
<< "Μάθημα" << "--" << "Βαθμός\n";
FOR(i, 0, NMAX) cout << i << " " << s.grades[i] << "\n";
cout << "Μέσος όρος: " << s.mo_grades() << "\n";
}
int main(){
Student s1("Kostas","Mixos",1050333), s2("Mike","Lamar",1050111), *s3 = new Student("Maria","Pentagioth",1050222);
s1.set(1,7.5); s1.set(2,7); s1.set(3,8);
s2.set(1,6.0); s2.set(2,3.0); s2.set(3,7.0);
s3->set(1,5.0); s3->set(2,9.0); s3->set(3,8.0);
// Παράδειγμα χρήσης του τελεστή ==
if (s1==s2) cout "true\n";
cout << s1.mo_grades() << endl;
cout << s2.mo_grades() << endl;
cout << s3->mo_grades() << endl;
// Προτιμώ να ελευθερώσω την μνήμη καλώντας τον καταστροφέα
//delete s3;
s3->~Student();
return 0;
}
Τα προγράμματα: thema1_Jun16.hpp, thema1_Jun16.cpp, thema2_Jun16.cpp, thema3_Jun16.cpp, thema4_Jun16.cpp.
Οι εκφωνήσεις: Θέματα Ιούνη 2016 (pdf)