Programming in C question. Printing Arrays

Click For Summary

Discussion Overview

The discussion revolves around a programming assignment in C that involves creating an array, accepting user input, and printing the array in specified formats. Participants explore various methods to improve the initial implementation of the solution, focusing on code elegance and efficiency.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant describes their initial solution as inelegant and seeks a more proper approach to printing an array of user-inputted values.
  • Another participant suggests using nested for loops to print the array in a structured format, explaining how the outer and inner loops work together.
  • A third participant proposes using a single loop with an if statement and modulo operation to achieve the same printing format, emphasizing simplicity and clarity.
  • Some participants note the importance of using braces in loop constructs to avoid confusion regarding the scope of loop bodies.

Areas of Agreement / Disagreement

Participants generally agree on the need for improved coding practices and suggest alternative methods for printing the array. However, there is no consensus on a single "best" method, as multiple approaches are discussed.

Contextual Notes

Some participants highlight potential issues with the original code's readability and maintenance, particularly regarding indentation and the use of braces. There are also discussions about the clarity of the proposed solutions.

Who May Find This Useful

Students learning C programming, particularly those interested in array manipulation and output formatting, may find this discussion beneficial.

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
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · 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