# Homework Help: Programming in C question. Printing Arrays

1. Nov 13, 2012

### Vagabond7

1. The problem statement, all variables and given/known data
Alright, I actually solved this problem myself, it is just ugly and hilariously inelegant. I won't lose points since it is an introductory course, and it technically works, but I wondered what the "right way" to do this is.

Here is what I had to do. Create an array where the user inputs 20 values to go into the array. Then print the array with 5 numbers per line in order the user input it. Then print it 4x4 in reverse order.

I did this, but it is hilariously bad. Like I said, my teacher only cares if it works, but for the sake of learning things correctly I would like to know to proper way to do this.

2. Relevant equations

3. The attempt at a solution

Code (Text):

#include <stdio.h>
int main ()
{int i = 0;
int A[20];//Array creation
for ( i = 0; i < 20; i++ ) {
printf("Enter a value: ");
scanf("%d", &A[i]); // entering numbers into the array
} /* end for */

for ( i = 0; i < 5; ++i ) // The part the prints the array in order with 5 numbers per row
printf ("%d", A[i]);
printf ("\n");
for ( i = 5; i < 10; ++i )
printf ("%d", A[i]);
printf ("\n");
for ( i = 10; i < 15; ++i )
printf ("%d", A[i]);
printf ("\n");
for ( i = 15; i < 20; ++i )
printf ("%d", A[i]);
printf ("\n\n\n\n");// making space for aesthetic reasons

for ( i = 19; i > 15; --i )// The hilarious solution to printing the array backwards, the punchline is really good.
printf ("%d", A[i]);
printf ("\n");
for ( i = 15; i > 11; --i )
printf ("%d", A[i]);
printf ("\n");
for ( i = 11; i > 7; --i )
printf ("%d", A[i]);
printf ("\n");
for ( i = 7; i > 3; --i )
printf ("%d", A[i]);
printf ("\n");
for ( i = 3; i > 0; --i )
printf ("%d", A[i]);
printf ("%d", A[0]);

getch ();
return 0;
} //program ending

I especially find the part where I just slap on print A[0] because the counter wasn't counting down all the way to A[0]. I know it's rough, but it works. What is a better way to do this?

2. Nov 13, 2012

### Staff: Mentor

You need to use nested for loops to do what you want. For the first part, where you print 4 lines, with 5 numbers per line, you could do this:
Code (Text):
for (i = 0; i < 4; i++)
{
for (j = 0; j < 5; j++)
{
printf("%d ", A[4*i + j]);
}
printf("\n");
}

Think about how this works. The outer loop runs slower than the inner loop in that each iteration of the outer loop has 5 iterations of the inner loop.

When i is 0, j take on values 0, 1, 2, 3, and 4, and you print A[0], A[1], A[2], A[3], and A[4], then you print a new-line character.

When i is 1, j takes on the same values as above, and you print A[4*1 + 0], A[4*1 +1], A[4*1 +2], A[4*1 +3], and A[4*1 +4], then you print a new-line character.

And so on.

You can do the same sort of thing to print out the array values in reverse, with 5 lines of 4 values per line (you said 4 X 4) but I think this is what you meant).

It's bad programming practice to neglect putting in braces for your for loops (and other loops and if blocks). When you write things like this:

Code (Text):

for ( i = 3; i > 0; --i )
printf ("%d", A[i]);
printf ("%d", A[0]);
getch ();
return 0;

your indentation suggests that there are four statements in the loop body, but there is only one. You might understand that there is only one statement in the loop body, but someone who is maintaining your code will mistakenly think that all four statements are in the loop body.

A better way to write this is like so:
Code (Text):

for ( i = 3; i > 0; --i )
{
printf ("%d", A[i]);
}
printf ("%d", A[0]);
getch ();
return 0;

Last edited: Nov 13, 2012
3. Nov 13, 2012

### rcgldr

In addition to using nested loops, you could use an if with modulo.

Code (Text):

for(i = 0; i < 20; ++i){
printf("%d ", A[i]);
if((i%5) == 4){
printf("\n");
}
}

4. Nov 14, 2012

Nice!