Tell me how to act if an array is given (for example, n) with a length of 32 elements, arbitrarily filled with zeros and ones, and you want to display the contents in the form:

n (1) = "The first four elements of the array"
n (2) = "The second four elements of the array"
...
n (8) = "The last four elements of the array"

Thank you in advance.

  • Understood nothing. Where to withdraw? What is the problem? - skegg

4 answers 4

I do not know why two cycles ...

#include <stdio.h> int main(){ int size = 32; int arr[size]; int i = 0; for(i = 0;i<size;i++){ arr[i] = 1; } for(i = 0;i<size;i++){ if(!(i%4)){ printf("\n"); } printf("%d ", arr[i]); } return 0; } 

    There is such a thing as nesting cycles. So here's the idea: the first cycle iterates over the array segments, and the second all elements of this segment.

     int segs = n/4; for(int i = 0; i < segs; i++ ) { std::cout << "n(" << i+1 << ") = "; for(int j = 0; j < 4 && segs * i + j < n; j++) { std::cout << mas[segs * i + j] << ' '; } } 

      In my opinion, you can do with one cycle:

       #include <stdio.h> int main() { int n[32] = {0,1,1,1,0,0,0,0, 1,1,1,0,0,1,1,0, 0,1,1,0,0,0,1,1, 1,1,1,1,0,0,0,1}; int i, k = 1; for (i = 0; i< sizeof(n)/sizeof(int); i = i+4,k++) { printf("n(%1d) = \"%1d%1d%1d%1d\"\n",k,n[i],n[i+1],n[i+2],n[i+3]); } } 

      At the output we get:

       n(1) = "0111" n(2) = "0000" n(3) = "1110" n(4) = "0110" n(5) = "0110" n(6) = "0011" n(7) = "1111" n(8) = "0001" 

        a little incomprehensible: an array of (for example n) 32 elements is given, how is it? Well, okay) the numbers 32 and 8 are invented, so you can use the sizeof the main thing so that it is divided by 4 :)

         #include <iostream> int main() { int array[32]; int result[8]; int j = 0; int m = 0; for (int i=0; i<32; i++) { array[i] = i; // Π·Π°ΠΏΠΎΠ»Π½ΠΈΠΌ массив } for(int i = 0; i < 8; i++) { m = m + 4; for(j; j < m; j++) { result[i] = array [j]; // Π½Π΅ ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½ΠΎ, Π² ΠΏΠ»ΡŽΡΠ°Ρ… Π½Π΅ силСн, Π΄Π°Π½Π½Ρ‹Π΅ ΡΡƒΠΌΠΌΠΈΡ€ΡƒΡŽΡ‚ΡΡ } } for (int i=0; i<8; i++) { std::cout << result[i] << std::endl; //ΠΏΠΎΠΊΠ°ΠΆΠ΅ΠΌ массив } }