здравствуйте можете дать коментарий к телу программы? (задача: Дан целочисленный массив A из N элементов (0<N<100;0<A[i]<100). Определить те его элементы, индексы которых являются степенями двойки (1,2,4,8,16, ...).)
| Код | uses crt; const nmax=99; var a:array[1..nmax] of integer; n,i:integer; begin clrscr; randomize; repeat write('Размер массива до ',nmax,' n='); readln(n); until n in [1..nmax]; writeln('Исходный массив:'); for i:=1 to n do begin a[i]:=random(99)+1; write(a[i]:4); end; writeln; writeln('Элементы, чьи индексы есть степени числа 2:'); i:=1; while i<=n do begin write(a[i]:4); i:=i*2; end; readln end.
|
| Код | #include <iostream> #include <cstdlib> #include <ctime> bool isPowerOfTwo (int ); int main () { srand(time(0)); const int arraySize = 25; int array[arraySize]; std::cout << "Array: " << std::endl; for (int i = 0; i < arraySize; i++) std::cout << (array[i] = rand () % 101) << " "; std::cout << std::endl; std::cout << "Element of array: " << std::endl; for (int i = 0; i < arraySize; i++) if (isPowerOfTwo(i + 1)) std::cout << array[i] << " "; std::cout << std::endl; return 0; } bool isPowerOfTwo (int value) { int tmpValue = 2; while (tmpValue < value) tmpValue *= 2; return (tmpValue == value); }
|
|