CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Jan 2014
    Posts
    12

    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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: printing 2 arrays with 10 numbers per line

    Quote Originally Posted by howardstark View Post
    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

  3. #3
    Join Date
    Jan 2014
    Posts
    12

    Re: printing 2 arrays with 10 numbers per line

    Quote Originally Posted by Paul McKenzie View Post
    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: printing 2 arrays with 10 numbers per line

    Quote Originally Posted by howardstark View Post
    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.
    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

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    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

  6. #6
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: printing 2 arrays with 10 numbers per line

    Quote Originally Posted by howardstark View Post
    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]));
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured