/* * arrays.cpp * * Created on: 05.11.2013 * Author: trifon */ #include using namespace std; void test_arrays() { int a[] = { 2, 4 }; // int a[2] = { 2, 4 }; int b[30] = { 1,5,7,8,2,6 },c[30] = { 1,5,7,8,2,6 }; int i; cout << "i = "; cin >> i; cout << b[i] << endl; b[i] = 10; // !!! b = c; // !!! Не сравнява поелементно! if (b == c) cout << "Да!"; else cout << "Не!"; // !!! cin >> b; cout << b; } int main() { const int MAX = 100; int a[MAX], n; cout << "n = "; cin >> n; for(int i = 0; i < n; i++) { cout << "a[" << i << "] = "; cin >> a[i]; } int sum = 0; for(int i = 0; i < n; i++) sum += a[i]; cout << "sum = " << sum << endl; int x; cout << "x = ";cin >> x; int i = 0; while (i < n && a[i] != x) i++; // a[i] == x - ДА // i == n - НЕ cout << "Числото " << x; // ? ---> a[i] != x // ? ---> i == n if (i == n) cout << " НЕ"; cout << " се среща в масива!" << endl; i = 0; while(i < n-1 && a[i] <= a[i+1]) i++; // i == n-1 - ДА // a[i] > a[i+1] - НЕ cout << "Масивът "; if (i < n-1) cout << "НЕ "; cout << "образува монотонно растяща редица!" << endl; i = 0; bool foundSame = false; while (!foundSame && i < n - 1) { // искаме да проверим дали // a[i] се среща в a[i+1],...,a[n-1] int j = i+1; // има ли някое a[j] == a[i]? while (j < n && a[i] != a[j]) j++; // j == n - a[i] не се среща в a[i+1],...,a[n-1] // a[j] == a[i] - a[i] се среща if (j < n) foundSame = true; i++; } cout << "Масивът "; if (foundSame) cout << "НЕ "; cout << "представя множество!" << endl; for(int i = 0; i < n; i++) cout << "a[" << i << "] = " << a[i] << endl; return 0; }