uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

commit 7847c7056abf9d1e2e88ef7c3e10f2889d869868
parent c7564e3bbd433a2f887a671fdf6d0ca7d2f77a0b
Author: Christos Margiolis <christos@margiolis.net>
Date:   Wed, 26 Feb 2020 00:13:38 +0200

new assignment

Diffstat:
Aassignment-2.1-fromctocpp/Ex1.cpp | 112+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-2.1-fromctocpp/assignment-2.1-fromctocpp.cpp | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 190 insertions(+), 0 deletions(-)

diff --git a/assignment-2.1-fromctocpp/Ex1.cpp b/assignment-2.1-fromctocpp/Ex1.cpp @@ -0,0 +1,112 @@ +#include <iostream> +#include <string> + + +struct STUD +{ + int AM; + std::string Name; + int Semester; +}; + +using namespace std; + +STUD * InitStuds (); +void swap (int &, int &); +void swap (STUD &, STUD &); +void ShowStuds (const STUD *, const int); +void SortStuds (STUD *, int); +STUD& GetStudBySemester (STUD[], const int, const int); +int Cube (int len, int=2, int=1); + + + + +int main (int argc, char **argv) +{ + int a = 20; + int b = 30; + swap (a, b); + cout << "a = " << a << ", b = " << b << endl <<endl; + + STUD *st = InitStuds (); + ShowStuds (st, 3); + SortStuds (st, 3); + ShowStuds (st, 3); + STUD &R = GetStudBySemester (st, 3, 5); + R.Name = "Changed"; + ShowStuds (st, 3); + delete[] st; + + cout << Cube (2, 3, 4) << endl; + cout << Cube (2, 3) << endl; + cout << Cube (2) << endl; +} + +//Η γνωστή συνάρτηση swap σε cpp έκδοση με references. +void swap (int &i1, int &i2) +{ + int tmp; + tmp = i1; + i1 = i2; + i2 =tmp; +} + +//Η συνάρτηση swap υπερφορτωμένη για struct STUD +void swap (STUD &i1, STUD &i2) +{ + STUD tmp; + tmp = i1; + i1 = i2; + i2 =tmp; +} + +//Δημιουργεί έναν δυναμικό πίνακα 3 θέσεων τύπου STUD και τον αρχικοποιεί "καρφωτά" +STUD * InitStuds () +{ + STUD *tmp = new STUD [3]; + tmp[0].AM = 456; + tmp[0].Name = "Αντωνίου"; + tmp[0].Semester = 2; + tmp[1].AM = 123; + tmp[1].Name = "Βασιλείου"; + tmp[1].Semester = 1; + tmp[2].AM = 789; + tmp[2].Name = "Γεωργίου"; + tmp[2].Semester = 5; + return tmp; +} + +//Τυπώνει τα στοιχεία του πίνακα τύπου STUD +void ShowStuds (const STUD *S, const int N) +{ + //N++; + for (int i = 0; i < N; i++) + cout << S[i].AM << " " << S[i].Name << " " << S[i].Semester << endl; + cout << endl; +} + +//Ταξινομεί τον πίνακα τύπου STUD +void SortStuds (STUD *S, int N) +{ + for (int i = 0; i < N - 1; i++) + for (int j = i + 1; j < N; j++) + if (S[i].AM > S[j].AM) + swap (S[i], S[j]); +} + +//Παράδειγμα συνάρτησης που επιστρέφει αναφορά +STUD& GetStudBySemester (STUD D[], const int S, const int Semester) +{ + int i; + for (i = 0; i < S; i++) + if (D[i].Semester == Semester) + return D[i]; + return D[0]; +} + +int Cube (int len, int wid, int hei) +{ + return len * wid * hei; +} + diff --git a/assignment-2.1-fromctocpp/assignment-2.1-fromctocpp.cpp b/assignment-2.1-fromctocpp/assignment-2.1-fromctocpp.cpp @@ -0,0 +1,78 @@ +#include <iostream> +#include <string> + +class Human +{ + private: + std::string name; + int age; + + public: + Human(std::string name) {this->name = name;} + std::string getname() const {return name;} +}; + +int modconst(int *, int); +void chnames(std::string *, int); +void getnames(std::string *, int); +int chval(int &, int); +int& ref(int *, int); +std::string defaultargs(std::string = "No"); + +int main(int argc, char **argv) +{ + // Ερώτημα 1 + const int x = 10; + std::cout << "Old const value: " << x << std::endl << "New const value: " << modconst((int *)&x, 15) << std::endl << std::endl; + + // Ερώτημα 2 + Human *human = new Human("Christos"); + std::string *names = new std::string[3]; + std::cout << human->getname() << std::endl << std::endl; + + chnames(names, 3); + std::cout << std::endl; + getnames(names, 3); + std::cout << std::endl; + + delete human; + delete[] names; + + // Ερώτημα 3 + int y = 5; + std::cout << "Old y value: " << y << std::endl << "New y value: " << chval(y, 10) << std::endl << std::endl; + + int *arr = new int[3]; + for (int i = 0; i < 2; i++) arr[i] = i; + std::cout << "Old value at index 1: " << arr[1] << std::endl; + ref(arr, 1) = 500; + std::cout << "New value at index 1: " << arr[1] << std::endl << std::endl; + delete[] arr; + + // Ερώτημα 4 + std::cout << "Function return values: " << std::endl; + std::cout << "With default arg: " << defaultargs() << std::endl; + std::cout << "Without default arg: " << defaultargs("Yes") << std::endl; + + return 0; +} + +void chnames(std::string *names, int numNames) +{ + for (int i = 0; i < numNames; i++) + { + std::cout << "Name " << i << ": "; + std::cin >> names[i]; + } +} + +void getnames(std::string *names, int numNames) +{ + for (int i = 0; i < numNames; i++) + std::cout << "Name " << i << ": " << names[i] << std::endl; +} + +int modconst(int *x, int newVal) {return *x = newVal;} +int chval(int &y, int newVal) {return y = newVal;} +int& ref(int *arr, int index) {return arr[index];} +std::string defaultargs(std::string arg) {return arg;}