Given an integer N and a set of N integers. Find the number of the first maximal odd number from the given set. If there are no odd numbers in the set, then output 0.
Program:
#include <vcl.h> #include <iostream.h> #include <conio.h> #include <stdlib.h> char *Rus(const char *text); void main() { int a[10]; int i, n, max; cout << Rus(" Введите n: "); cin >> n; randomize(); for (i = 1; i < n; i++) { a[i] = rand() % 50; cout << a[i] << " "; } max = a[0]; for (i = 1; i <= n - 1; i++) { if ((a[i] > max) && (a[i] % 2 != 0)) max = a[i]; } { if ((max == a[0]) && (a[0] % 2 != 0) || (max != a[0])) cout << " " << Rus("искомый элемент= ") << max; else cout << " " << Rus(" таких элементов нет "); } getch(); } char bufRus[256]; char *Rus(const char *text) { CharToOem(text, bufRus); return bufRus; }
The program incorrectly counts. How to write the conditions correctly?