/* * functions.cpp * * Created on: 26.11.2013 * Author: trifon */ #include #include #include #include "triarea.h" using namespace std; double f(double x) { double y; cin >> y; return x + y; cout << "Hello, world!" << endl; // !!! return "abc"; // !!! return true; } double square(double x) { return x * x; } void test_functions() { cout << f(2) << ' ' << f(2) << endl; //cout << f(2)+3 cout << square(square(5)-10) << endl; cout << sqrt(5) << endl; } int add5(int& x) { static int sum = 0; x += 5; sum += x; return sum; } void swap(int& x, int& y) { int oldx = x; x = y; y = oldx; //x = x + 1; } void swap(double& x, double& y) { double oldx = x; x = y; y = oldx; //x = x + 1; } // отпечатва "анонимен" масив void printArray(int a[], int n) { for(int i = 0; i < n; i++) cout << a[i] << ' '; cout << endl; } // отпечатва масив с име void printArray(int a[], int n, char name[]) { for(int i = 0; i < n; i++) cout << name << "[" << i << "] = " << a[i] << endl; // !!! a[1] = 100; } int readArray(int a[], char name[]) { int n; cout << "n = "; cin >> n; for(int i = 0; i < n; i++) { cout << name << "[" << i << "] = "; cin >> a[i]; } return n; } bool equal(char s1[], char s2[]) { return strcmp(s1, s2) == 0; } void findMinMax(int a[], int n, int& min, int& max) { min = max = a[0]; for(int i = 1; i < n; i++) { if (a[i] < min) min = a[i]; if (a[i] > max) max = a[i]; } } int main() { cout << triarea(1, 1, 4, 1, 4, 5) << endl; int y = 10; cout << add5(y); cout << ' ' << y << endl; cout << add5(y); cout << ' ' << y << endl; // !!! cout << add5(3) << endl; int z = 5; cout << y << ' ' << z << endl; swap(y,z); cout << y << ' ' << z << endl; double a = 1.2; double b = 2.3; cout << a << ' ' << b << endl; swap(a, b); cout << a << ' ' << b << endl; char c1 = 'a', c2 = 'b'; // !!! swap(c1, c2); int d[3] = { 10, 20, 30 }; int c[5] = { 1, 2, 3, 4, 5 }; printArray(c, 5, "c"); printArray(d, 3); printArray(c, 5, "c"); int x = readArray(c, "c"); printArray(c, x, "c"); printArray(d, 3, "d"); cout << equal("abc", "abc") << endl; int low, high; findMinMax(c, x, low, high); cout << low << ' ' << high << endl; return 0; }