The program sets a one-dimensional array, the numbers of which are generated arbitrarily, you need to organize its output using cout. But with this conclusion:

cout < < m[i]; //без пробелов 

The output of the number occurs without spaces, i.e. solid numbers

 54216452122454235412 

How to make such a conclusion

 54 21 64 52 12 24 54 23 54 12 

printf - not suitable.

    6 answers 6

    So:

     cout << m[i] << ' '; 

    printf is not suitable

    Why? You can not use it or you yourself made such a conclusion? And then you can:

     printf("%i ", m[i]); 
    • through printf, the teacher did not narv) as if we were taught the kind of thing, do as taught) - toris
    • thanks, everything works - toris
    • In real programming, formatted output through printf () is easier to do. When you have passed all the teacher, tell him about it, let him teach correctly. - avp
    • 2
      Sishny printf is not type safe, so it teaches correctly. By the way, I invented my type-safe String :: Format. Used like this: int iv = 123; double pi = 3.1415926; String str = String :: Format ("iv = <^>; pi = <^>") (iv) (pi); Such is the formatting of strings with emoticons. - gammaker
    • 3
      A C ++ programmer should know both printf and scanf, since they are actually used and a lot of code is written with them. And about the type safety, so in this case, the proger and head are given to prevent errors. In addition, the compilers immediately issue warnings if the types do not match. - skegg

    eg

     cout << m[i] << " " ; 
    • 2
      I did not have time to write it. - DelphiM0ZG
    • And why 2 spaces? - gammaker
    • I don’t know that it didn’t seem like a little :) - beardog
    • - Why did you put jmp smth twice? -And suddenly the first one will not work ... - sudo97
     #include <iterator> #include <algorithm> #include <iostream> using namespace std; ... copy(m, m + sizeof(m)/sizeof(m[0]), ostream_iterator<int>(cout, " ")); cout << endl; 
    • Not for beginners of course, but I will put a '+'. m + sizeof(m)/sizeof(m[0]) If I'm not mistaken, this can be replaced by end (m). - gammaker
    • endl not recommended, better - and faster - "\ n". endl , how to say it, flashing output buffers - strangeqargo

    We connect library

     #include<iomanip> 

    further in the array output:

     cout<<m[i]<<setw(5); 

    The setw function makes spaces between elements for the number of ones indicated in brackets.

    • one
      setw sets the width of the output field (data + placeholder), rather than 'makes spaces'. The placeholder is set via setfill. And it’s still worth setting the width BEFORE outputting the data, not after. - αλεχολυτ
      cout << m[0]; for(int i=1; i < sizeof m/sizeof(int);) cout << ' ' << m[i++]; cout << endl; 
       #include<iostream> usin namespace std; int main(){ int a[1024]; for(int i=0; i<=1024; i++) a[i]=i; for(int i=0; i<=1024; i++) cout << a[i] << ' ';//ну или printf("%d ", a[i]); } 
      • 3
        Why answer if you already have such an answer? Especially since you have an error - going beyond the array. Since the countdown is from scratch, the element is 1023, and you include 1024 in the cycle. - gammaker
      • I did not find the full answer. Moreover , the mistake is not fatal and can be corrected even if you are a beginner - sudo97
      • 2
        about a non-fatal error, it's you in vain. going out of the array like this can lead to data corruption, security breaches and just an emergency unloading of the program. - skegg
      • > and just for emergency unloading of the program. Hardly. On the stack, in my opinion, buffer overflow errors are not checked, so crashes only when the program uses corrupted data. > I did not find the complete answer. He could fill the array himself, once asked a question about his conclusion. He also announced the cycle judging by the fact that he showed the result of the output of his program without gaps. > It can be corrected even if you are a beginner. When I was a beginner, I ran across such errors. The compiler does not report them. Only in Visual Studio you can enable code analysis, in which he writes about this error - gammaker
      • one
        Take the elf. Suppose the main function. The first variable that is declared in it is an array. During program execution, space is allocated in the stack by changing the contents of the esp register. Suppose we overflowed the array while filling. What is behind the array in front of the available function stack? char ** argv. If we break it, then when we try to read the parameters of the process call, the segmentation fault will occur. This is just one of the scenarios. - skegg