/**********************************
* ΛΥΣΕΙΣ ΘΕΜΑΤΩΝ ΣΕΠΤΕΜΒΡΗ 2016 *
*********************************/
/*************************************************************************
* ΘΕΜΑ 1ο
*************************************************************************/
//thema1_Sep16.hpp
#ifndef thema1_Sept16_hpp // περιορισμός ορισμού του "thema1_Jun16.hpp" μόνο μια φορά
#define thema1_Sept16_hpp
#define sign(x) ((x > 0)?(1):(-1))
void signs(int n, int a[], int b[]){
for (int i = 0; i < n; i++){
if(a[i] == 0) b[i] = 0;
else b[i] = sign(a[i]);
}
}
void count(int n, int a[], int &count1, int &count0, int &countm1){
for (int i = 0; i < n; i++){
if (a[i] == 1) count1++;
if (a[i] == 0) count0++;
if (a[i] == -1) countm1++;
}
}
#endif
//thema1_Sep16.cpp
#include "thema1_Sep16.hpp"
#include <iostream>
#include <cstdlib> // για την rand()
#define NMAX 10
using namespace std;
int main(){
int ones = 0 , zeros = 0, minusones = 0;
int x[NMAX], y[NMAX];
for (int i = 0; i < NMAX; i++){
x[i] = rand()%50 - 20;
y[i] = rand()%50 - 20;
}
signs(NMAX, x, y);
count(NMAX, y, ones, zeros, minusones);
for (int i = 0; i < NMAX; i++) cout << y[i] << "\t";
cout << "\n";
cout << "(ones, zeros, minus ones) = (" << ones << "," << zeros << "," << minusones <<")\n";
return 0;
}
/*************************************************************************
* ΘΕΜΑ 2ο
*************************************************************************/
// thema2_Sep16.cpp
#include <iostream>
#define FOR(i,start,stop) for(int i = start; i < stop; i++)
using namespace std;
void sum(int n, int a[], int b[], int c[]){
if (n > 0){
c[n-1] = a[n-1] + b[n-1];
n--;
sum(n, a, b, c);
}
}
void inner(int n, int a[], int b[], float &res){
if (n > 0) {
res += a[n-1]*b[n-1];
n--;
inner(n, a, b, res);
}
}
int main(){
int n;
cout << "Δώσε το n\n";
cin >> n;
int *x = new int[n], *y = new int[n], *z = new int[n];
float res = 0.0;
FOR(i,0,n) cin >> x[i];
FOR(i,0,n) cin >> y[i];
sum(n,x,y,z);
FOR(i,0,n) cout << z[i] << "\t";
cout << "\n";
inner(n,x,y,res);
cout << "inner(x,y) = " << res << endl;
delete [] x, y, z;
return 0;
}
/*************************************************************************
* ΘΕΜΑ 3ο
*************************************************************************/
// thema3_Sep16.cpp
#include <iostream>
using namespace std;
int main(){
int n;
cout << "Δώσε το n\n";
cin >> n;
int *x = new int[n], *y = new int[n], *z = new int[n];
int *px, *py, *pz;
float inner = 0.0;
// είσοδος στοιχείων στα διανύσματα x, y, βαδίζοντας προς τα μπρος
for(px = x; px < x + n; px++) cin >> *px;
for(py = y; py < y + n; py++) cin >> *py;
// υπολογισμός του διανύσματος z = x + y, βαδίζοντας προς τα μπρος
for(pz = z, px = x, py = y; pz < z + n; pz++,px++,py++) *pz = (*px) + (*py);
// εκτύπωση των στοιχείων του z για έλεγχο
for (pz = z; pz < z + n; pz++) cout << *pz << "\t";
cout << endl;
// υπολογισμός εσωτερικού γινομένου , βαδίζοντας αντίστροφα στις λίστες x,y
for (; px >= x; px--, py--) inner += (*px) * (*py);
// εκτύπωση εσωτερικού γινομένου
cout << inner << endl;
// αλλιώς (βαδίζοντας προς τα μπρος)
//inner = 0.0;
//for (px = x, py = y; px < x + n; px++, py++) inner += (*px) * (*py);
//cout << inner << endl;
return 0;
}
/*************************************************************************
* ΘΕΜΑ 4ο
*************************************************************************/
// thema4_Sep16.cpp
#include <iostream>
#include <cmath>
using namespace std;
class Point{
private:
float x,y,z;
public:
Point(float x1, float y1, float z1){
x = x1;
y = y1;
z = z1;
}
Point(Point const &p){
x = p.x;
y = p.y;
z = p.z;
}
~Point(){
cout << "Deleting Point(" << x << "," << y << "," << z << ")\n";
}
Point symmetric(){
Point p(0,0,0);
p.x = -x;
p.y = -y;
p.z = -z;
return p;
}
float distance(Point p){
return sqrt(pow(x- p.x,2) + pow(y- p.y,2) + pow(z- p.z,2));
}
Point &operator= (const Point &p){
if (this != &p){
x = p.x;
y = p.y;
z = p.z;
}
return (*this);
}
bool operator== (const Point &p){
return (x == p.x && y == p.y && z == p.z);
}
friend void print(Point);
};
void print(Point p){
cout << "Point(" << p.x << "," << p.y << "," << p.z << ")\n";
}
int main(){
Point A(1,1,1), B(2,2,2), *c = new Point(3,3,3);
print(A);
print(B);
cout << A.distance(B) << endl << B.distance(*c) << endl << c->distance(A) << endl;
cout << sqrt(3) << endl << sqrt(3) << endl << sqrt(12) << endl;
cout << "Testing if A == B\n";
if (A == B){
cout << "same points \n";
}
else {
cout << "different points\n";
}
B = A;
cout << "Testing again, if A == B\n";
if (A == B){
cout << "same points \n";
}
else {
cout << "different points\n";
}
print(B);
print(A.symmetric());
print(A);
print(B);
print(*c);
c->~Point();
// delete c;
return 0;
}
Τα προγράμματα: thema1_Sep16.hpp, thema1_Sep16.cpp, thema2_Sep16.cpp, thema3_Sep16.cpp, thema4_Sep16.cpp.
Οι εκφωνήσεις: Θέματα Σεπτέμβρη 2016 (pdf)