printing 2 arrays with 10 numbers per line
hey guys i got 2 arrays, how would i print 10 numbers per line, so that would be 5 numbers from each array.
array1[40];
array2[40];
array1 array2// array1 array2// array1 array2 // array1 array2 // array1 array2
array1 array2// array1 array2// array1 array2 // array1 array2 // array1 array2
array1 array2// array1 array2// array1 array2 // array1 array2 // array1 array2
...... and so on
how can i put this in a loop?
and so on but there are 80 elements so i cant go one by one.
Re: printing 2 arrays with 10 numbers per line
Quote:
Originally Posted by
howardstark
hey guys i got 2 arrays, how would i print 10 numbers per line, so that would be 5 numbers from each array.
This is basically the same question as your other thread you started.
How do you print two items on a line? Forget about arrays for the moment -- I have two int variables, how do you print both of them on the same line?
Code:
#include <iostream>
int main()
{
int i = 1;
int j = 2;
//...
}
Using cout, how do you print both the value of i and j on the same line? OK, if you can do that, then that is the first step.
So now in a loop, how do you print array1[x] and array2[x] on the same line? OK, if you can do that, what does the "x" denote? Doesn't that look like an index that will go from 0 to 40 (or to be exact, 39)? What construct uses an index? Isn't it the for-loop construct?
Now how do you print a line, regardless of what's on that line, and then place the output cursor on the next line? Isn't that just "\n" or endl? Now combine all of that information.
This is how you break down a problem.
Regards,
Paul McKenzie
Re: printing 2 arrays with 10 numbers per line
Quote:
Originally Posted by
Paul McKenzie
This is basically the same question as your other thread you started.
How do you print two items on a line? Forget about arrays for the moment -- I have two int variables, how do you print both of them on the same line?
Code:
#include <iostream>
int main()
{
int i = 1;
int j = 2;
//...
}
Using cout, how do you print both the value of i and j on the same line? OK, if you can do that, then that is the first step.
So now in a loop, how do you print array1[x] and array2[x] on the same line? OK, if you can do that, what does the "x" denote? Doesn't that look like an index that will go from 0 to 40 (or to be exact, 39)? What construct uses an index? Isn't it the for-loop construct?
Now how do you print a line, regardless of what's on that line, and then place the output cursor on the next line? Isn't that just "\n" or endl? Now combine all of that information.
This is how you break down a problem.
Regards,
Paul McKenzie
they are different because in this one i know the how many elements each array has, in the other thread. i dont know hom many elements each array has but just maximum elements it can hold.
Re: printing 2 arrays with 10 numbers per line
Quote:
Originally Posted by
howardstark
they are different because in this one i know the how many elements each array has, in the other thread. i dont know hom many elements each array has
Arrays are fixed in size, so you do know how many elements there are. If your array is declared to have 40 elements, then it has 40 elements -- you can't change its size.
Quote:
but just maximum elements it can hold.
Again, if an array is declared to have 40 elements, then 40 elements is the maximum.
Regards,
Paul McKenzie
Re: printing 2 arrays with 10 numbers per line
Quote:
Originally Posted by
howardstark
they are different because in this one i know the how many elements each array has, in the other thread. i dont know hom many elements each array has but just maximum elements it can hold.
Just divide the sizeof the total array by one item in the array.
Code:
int iNumItems = 0;
int array1[40];
iNumItems = (sizeof(array1)/sizeof(array1[0]));
Re: printing 2 arrays with 10 numbers per line
Code:
array1[0] array2[0] array1[1] array2[1] array1[2] array2[2] array1[3] array2[3] array1[4] array2[4]
array1[5] array2[5] array1[6] array2[6] array1[7] array2[7] array1[8] array2[8] array1[9] array2[9]
array1[10] array2[10] array1[11] array2[11] array1[12] array2[12] array1[13] array2[13] array1[14] array2[14]
//...
What pattern do you see with each line? Look at the first index on each line (0, 5, 10, ...). Again, the loop index for each line is a multiple of 5.
Now look within in each line -- you see an "inner" number going from the line's starting index (index+0, index+1, index+2, index+3, index+4). Another "inner loop" that goes from 0 to 4
Regards,
Paul McKenzie