0

I need to print an int array that has 50 values the array must contain multiple rows and each row can't have any more than 15 variables on it. Can any one point me in the right direction or explain where I'm going wrong?

#include <iostream>
using namespace std; 
int main() 
{ 
   int alhpa[51]; 

      for(int i = 0; i < 51; i++)
      {
         alpha[i] = -1; // initializes all elements to -1
      }


      for(int i = 0; i < 51; i++)
         { 
            for(int j = 0; j < 15; j++)
               { 
                  cout << alpha[j] << "\t";
               }  
            cout << endl;
         }


   return 0;
}

So the above code works it just doesn't do what I intended it to, if you run this you'll see that the array is printed in rows and each row does have 15 variables on it. How ever the issue is that it only prints the first 15 elements in the array and each time the main loop executes the process is reset and as I said above the only output is the first 15 variables over and over again.

I need to figure out how to print this array out in the same way it's printing now but I need all the elements in the array to be processed and printed.

I know that the last line wouldn't have 15 on it because there should only be room for 5 because 15 * 3 = 45

Any help would be awesome!

3
  • ...the above code works it just doesn't do what I intended it to. Isn't that the definition of "doesn't work"? ;) The logic for your double loop is incorrect. Think about what you are wanting to do here. You want to output a new line every 15 elements. So just make a one for loop (with i from 0 to 50) that counts how many times you output an element and each time it reaches 15, output a newline and reset the counter. Commented Feb 18, 2016 at 21:55
  • @RHertel that will cause an access violation for most of the values of i. Commented Feb 18, 2016 at 21:55
  • @lurker That is true. I noticed that; but I also noticed that in the meantime a correct solution has been posted, so there's no point in correcting my comment. I thus deleted it. Commented Feb 18, 2016 at 21:56

4 Answers 4

1

How about modifying this code

for(int i = 0; i < 51; i++)
         { 
            for(int j = 0; j < 15; j++)
               { 
                  cout << alpha[j] << "\t";
               }  
            cout << endl;
         }

to this

for(int i = 0; i < 51; i += 15)
         { 
            for(int j = i; j < 15+i && j < 51; j++)
               { 
                  cout << alpha[j] << "\t";
               }  
            cout << endl;
         }

This prints them in sets of 15 each line

The output of your program in this case will be

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1- 1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1- 1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1- 1
-1 -1 -1 -1 -1 -1
Sign up to request clarification or add additional context in comments.

1 Comment

Ah I see I had some thing like this earlier today but when I did it I didn't write the nested loop properly, thanks for the solution!
1

Change

cout << alpha[j] << "\t";

to

if(15*i+j < 51)
    cout << alpha[15*i+j] << "\t";

If the element you access is only j, which loops from 0 to 14, it is normal that you only print the first 15 values.

With this change you can also change the outer loop to for(int i = 0; i < 4; i++)

Comments

1

As an alternative, you can use a counter for elements per line:

int count = 0;

for (int i = 0; i < 51; i++)
{
    cout << alpha[i] << "\t";

    if (++count == 15)
    {
        cout << endl;
        count = 0;
    }
}

if (count > 0)    // EOL after any residual values
    cout << endl;

1 Comment

Performance Niggle: I'd use '\n' in the loop to reduce flushing.
0

Your

cout << alpha[j] << "\t";

Only references the j var, meaning it will just repeat the same first 15 items in the array over and over. You will need to refactor this nested loop to achieve the results you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.