int array[argc]
This not how you should allocate memory to dynamic variable.
Read about "dynamic memory allocation" in C.
If you can run the program o, your computer, use the debugger to see what happen.
Advice: learn to indent properly your code, it helps to read it.
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
int array[argc], c, d, t,i,j;
int n=argc-1;
for (i = 1; i < argc; i++)
{
array[i - 1] = atoi(argv[i]);
}
for (c = 0; c < n; c++) {
printf("%d ", array[c]);
}
for (c = 1 ; c <= n - 1; c++) {
d = c;
printf("\n");
while ( d > 0 && array[d] < array[d-1]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
for (j = 0; j < n; j++) {
printf("%d ", array[j]);
}
}
return 0;
}
When you don't understand what your code is doing or why it does what it does, the answer is
debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.
Debugger - Wikipedia, the free encyclopedia[
^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[
^]
Basic Debugging with Visual Studio 2010 - YouTube[
^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.