If you know the maximum number of integers that you will input, then there is no need to dynamically allocate storage for an array unless the max number is so large that you risk StackOverflow (which is compiler dependent, but generally you can fit at least 250000 integers, see C/C++ maximum stack size of program)
Unless you are dealing with that many (you're not, you're talking 20), instead of dynamically allocating storage, simply declare a constant and declare an integer array of constant size, e.g.
#include <stdio.h>
#define MAXI 20 /* if you need a constant, define one */
int main (void) {
int n = 0,
array[MAXI] = {0};
...
You now have array with storage for MAXI (20) integers. You have also declared a convenient constant at the top of your code, so if you need to adjust the number of integers you accept, you simply change one-number rather than picking through all declarations and loop limits to make the change.
The MOST important thing you will do is validate all user input. That means verifying that the user entered valid data, your program successfully converted the input to a value you can use in your code, and.. the value is within the proper range. You do this for EVERY input you take. For example, getting the number of integers from the user:
printf ("how many numbers (up to %d)?: ", MAXI);
/* validate ALL user input, including range of input */
if (scanf ("%d", &n) != 1 || n < 1 || n > MAXI) {
fprintf (stderr, "error: invalid input 'n'.\n");
return 1;
}
By checking the return of scanf you validate one integer was entered by the user and successfully converted to int and placed in the variable n. You next must verify that it is in the range of values your program will take (e.g. 1->MAXI. The entry of a negative value more than 20 would result in Undefined Behavior, so make sure the value is in the proper range. (0 just would be self-defeating for your program)
Next, get the values and store them in your array. (you are just doing the same thing n times, so use a loop! You can also dispense with the range check as long as you are entering values within the normal integer range)
for (int i = 0; i < n; i++) { /* read/validate 'n' integers */
printf ("array[%2d]: ", i);
if (scanf ("%d", &array[i]) != 1) {
fprintf (stderr, "error: invalid input 'array[%d]'.\n", i);
return 1;
}
}
(it's also nice to prompt the user reminding them which value you are expecting, e.g. "array[1]: ", etc..)
Other than outputting your array, your sort is the last thing you need to do. C provides qsort as the standard sorting routine. It is far more efficient and far better tested than anything you (or I) will write off the top of our heads -- use it in all real situations.
That said, there is nothing wrong with writing your own sort, and it is good practice. (and for 20 values or less, it is just as fast). But, use a function! If there are parts of your code that can be used more than once, write a function to help make your code more maintainable (and much easier to read). You can write a bubble-sort simply:
void bubblesort (int *arr, int size)
{
int i, j, tmp;
for (i = 0; i < size; i++) {
for(j = 0; j < (size-1) - i; j++) {
if( arr[j] < arr[j+1] ) { /* change to > for ascending */
tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
Putting it altogether, you can do something similar to the following:
#include <stdio.h>
#define MAXI 20 /* if you need a constant, define one */
void bubblesort (int *arr, int size)
{
int i, j, tmp;
for (i = 0; i < size; i++) {
for(j = 0; j < (size-1) - i; j++) {
if( arr[j] < arr[j+1] ) { /* change to > for ascending */
tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
int main (void) {
int n = 0,
array[MAXI] = {0};
printf ("how many numbers (up to %d)?: ", MAXI);
/* validate ALL user input, including range of input */
if (scanf ("%d", &n) != 1 || n < 1 || n > MAXI) {
fprintf (stderr, "error: invalid input 'n'.\n");
return 1;
}
for (int i = 0; i < n; i++) { /* read/validate 'n' integers */
printf ("array[%2d]: ", i);
if (scanf ("%d", &array[i]) != 1) {
fprintf (stderr, "error: invalid input 'array[%d]'.\n", i);
return 1;
}
}
printf ("before:"); /* output before: ... */
for (int i = 0; i < n; i++)
printf (" %d", array[i]);
putchar ('\n');
bubblesort (array, n); /* sort your array */
printf ("after :"); /* output after: ... */
for (int i = 0; i < n; i++)
printf (" %d", array[i]);
putchar ('\n');
return 0;
}
Example Use/Output
$ /bin/arrayinput
how many numbers (up to 20)?: 5
array[ 0]: 1
array[ 1]: 2
array[ 2]: 3
array[ 3]: 4
array[ 4]: 5
before: 1 2 3 4 5
after : 5 4 3 2 1
Look things over and let me know if you have further questions.