- #1
Vagabond7
- 50
- 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?