user2699298
user2699298

Reputation: 1476

How to print specific amount of elements of array per line?

I want to print 10 elements per line of the array, so output would look something like this:

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

And here's my code so far:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main()
{
    srand(time(0));
    int array[1000];
    for (int i = 0; i < 1000; i++) array[i] = rand() % 1000;
    sort(array, array + 1000);
    for (int i = 0; i < 1000;){
        i += 10;
        for(int j = 0; j < i; j++){
            cout << array[j] << " ";
        }
        cout << endl;
    }
    return 0;
}

But I can't get it to work. For some reason it repeats all the numbers over and over.

Upvotes: 2

Views: 21596

Answers (8)

RichardPlunkett
RichardPlunkett

Reputation: 2988

Here for a nice compact answer:

const int N = 10;
for ( int i = 0; i < 1000; i++ )
{
    cout << array[i] << ((i+1) % N ? " " : "\n");
}

Upvotes: 4

Vlad from Moscow
Vlad from Moscow

Reputation: 311058

Try the following

const int N = 10;
for ( int i = 0; i < 1000; i++ )
{
    std::cout << array[i] << " ";
    if ( ( i + 1 ) % N == 0 )  std::cout << std::endl;
}

Upvotes: 3

AchillesHT
AchillesHT

Reputation: 1

add a newline will be a simple way to solve this problem. Simple means explicit behavior for descripting the processing.

Upvotes: 0

drescherjm
drescherjm

Reputation: 10857

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main()
{
  srand(time(0));
  int array[1000];
  for (int i = 0; i < 1000; i++) array[i] = rand() % 1000;
  sort(array, array + 1000);
  for (int i = 0; i < 1000;i += 10){
   for(int j = 0; j < 10; j++){
     cout << array[i+j] << " ";
   }
   cout << endl;
  }
  return 0;
}

Here is a live version: http://ideone.com/76ZVp8

The changes I made from the original code was to fix the inner loop to loop from

for(int j = 0; j < i; j++){

to

for(int j = 0; j < 10; j++){

The reason for this change is you do not want to loop from 0 to i at each iteration of the outer loop. That would create an increasing # of outputs for each iteration of the outer loop starting over at the beginning.

Since I made the first change to restrict j from 0 to 9 the next change followed to fix the index that is printed the output array index from

cout << array[j] << " ";

to

cout << array[i+j] << " ";

After that I created the ideone code and noticed the still wrong output. The first 10 members of the array were being skipped by the initial increment of 10 in the following code:

i += 10;

By moving this increment to the outer for loop the increment of 10 will happen after the inner loop and the newline which is what we want. So the new code for the outer for loop becomes:

for (int i = 0; i < 1000;i += 10){

Upvotes: 3

RichardPlunkett
RichardPlunkett

Reputation: 2988

For a smallest change to your code, change this line:

for(int j = 0; j < i; j++){

to:

for(int j = i-10; j < i; j++){

and it should mostly work. To avoid other issues you would also want to change:

for (int i = 0; i < 1000;){
        i += 10;

to

for (int i = 10; i < 1000; i+=10) {

Still, those the various other answers that use i % 10 will tend to be better general approaches.

Upvotes: 3

Jason Sgalla
Jason Sgalla

Reputation: 176

You can add:

...

If ((j % 10) = 0)

// Add a newline

...

Upvotes: 1

JaredPar
JaredPar

Reputation: 755141

The inner loop only uses j in the argument list. It needs to also consider i so that it can skip past the numbers it's already printed out

There is an easier way though. Instead of doing nested loops, just print out a new line every 10 elements.

for (int i = 0; i < 1000;) {
  cout << array[i] << " ";
  if ((i + 1) % 10 == 0) {
    cout << endl;
  }
}

Upvotes: 9

MicroVirus
MicroVirus

Reputation: 5477

Your problem is in the inner loop:

    for(int j = 0; j < i; j++){
        cout << array[j] << " ";
    }

Try to read it again and see what it's doing, maybe by stepping through it with the debugger, and you should be able to figure out why it's printing the same numbers again and again, and also why your lines are becoming longer and longer.

Upvotes: 2

Related Questions