I will add many comments (way more than you would ever write in your own code)
Program #1:
Code:
//define the function main which is the entry point into the program
main ()
{
//define a variable i which is an integer
int i;
//define a variable a which is an array of 5 integers {39...4}
int a[5] = {39,23,34,12,4};
//define a variable b which is an array of 5 integers which have yet to be defined
int b[5];
//set i equal to 0
i = 0;
//continue to execute the statements inside the { } as long as the value of i is less than 5
while (i < 5) {
// set the ith element of the array b to the (4-i)th element of the array a
b[i] = a[4-i];
//set i equal to i + 1
i=i + 1;
}
//set i equal to zero
i = 0;
//continue to execute the statements inside the { } as long as the value of i is less than 5
while (i<5) {
//set the ith element of the array a to the ith element of the array b
a[i] = b[i];
//set i equal to i + 1
i = i + 1;
}
}
in english:
you start with a = {39,23,34,12,4}, this is a[0]==39, a[1]==23...a[4]==4
you then set the ith element of b to the (4-i)th element of a
so b[0]==a[4], b[1]=a[3],...b[4]=a[0]
basically b is set to be the reverse order of a
finally you put the values of b back into a so now a contains the same values it originally did but in the opposite order.
This time I'll just comment what is less obvious.
you have written "for (i = 0; i < n/2; i+=) {"
but I assume you mean "for (i = 0; i < n/2; i+=2) {"
Program #2:
Code:
main()
{
int a[] = {10,20,30,5,15,25};
int n = 6;
int i, t;
//execute the contents of { } starting with i==0 and continuing while i < 3. each time through the loop increment by 2
//i+=2 means i = i + 2
for (i = 0; i < n/2; i+=2) {
//t holds the ith element of a
t = a[i];
//a gets set to the (n-i-1)th element of a
a[i] = a[n-i-1];
//the (n-i-1)th element of a gets set to t
a[n-i-1] = t;
}
}
this code is doing the same as the first program. it is reversing the order of the elements of a. however, in this case the program uses a single integer t as a buffer instead of a buffer array.
If you have any additional questions please ask!