Programming in C question. Printing Arrays

Click For Summary
The discussion revolves around improving a C programming solution for printing an array of 20 user-input values. The original code prints the array in a non-ideal manner, using multiple loops and hardcoded values, which is considered poor practice. Suggestions for improvement include using nested loops for structured output and implementing braces for clarity in loop constructs. Additionally, utilizing the modulo operator can simplify the printing process by automatically formatting the output into rows. Overall, the focus is on enhancing code elegance and maintainability while achieving the desired output.
Vagabond7
Messages
50
Reaction score
11

Homework Statement


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.


Homework Equations





The Attempt at a Solution



Code:
#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?
 
Physics news on Phys.org
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:
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:
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:
for ( i = 3; i > 0; --i )
{
    printf ("%d", A[i]);
}  
printf ("%d", A[0]);
getch ();
return 0;
 
Last edited:
In addition to using nested loops, you could use an if with modulo.

Code:
    for(i = 0; i < 20; ++i){
        printf("%d ", A[i]);
        if((i%5) == 4){
            printf("\n");
        }
    }
 
rcgldr said:
In addition to using nested loops, you could use an if with modulo.

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

Nice!
 

Similar threads

Replies
9
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
11K