Vertically allgining an array

This is a discussion on Vertically allgining an array within the C++ Programming forums, part of the General Programming Boards category; This is a part of my code for an array. Code: for(i=2;i<n;i++) { if(a[i] != 0) { cout<<a[i]<<" "; } ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    Vertically allgining an array

    This is a part of my code for an array.

    Code:
       for(i=2;i<n;i++)
       {
             if(a[i] != 0)
             {
                cout<<a[i]<<" ";
             }
       }   cout<<endl;
       int count = 0;
       for(i=2;i<n;i++)
       {
          if(a[i] != 0)
          count = count + 1;
       }
    
           cout<<"There are "<<count<<" primes <= to "<<n<<endl;
           cout<<"Enter pos int (0 to quit)? ";
           cin>>n;
    This is in a while loop, and it is part of a code I had for a prime algorithm.

    Anyways I want my out put vertically aligned like this:

    Code:
    179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281
    283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409
    419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541
    547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659
    661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809
    811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941
    947 953 967 971 977 983 991 997
    There are 168 primes <= 1000
    My array has no alignment and I don't know how you go about this. Thanks!

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Use std::setw() from <iomanip>.

    For example, cout<<setw(6)<<12;
    prints
    Code:
        12
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21