PDA

View Full Version : Meaning of these C programs


babykat
Jul 17, 2007, 07:36 AM
could someone please explain this program to me? I'm a beginner in C.

Program #1:
main ()
{
int I;
int a[5] = {39,23,34,12,4};
int b[5];

I = 0;
while (I < 5) {
b[i] = a[4-i];
I=i + 1;
}

I = 0;
while (I<5) {
a[i] = b[i];
I = I + 1;
}
}


Program #2:
main()
{
int a[] = {10,20,30,5,15,25};
int n = 6;
int I, t;

for (I = 0; I < n/2; I+=) {
t = a[i];
a[i] = a[n-i-1];
a[n-i-1] = t;
}
}


Could you please help me understand them, especially what each expression or command line means? Thanks.

benn11
Jul 17, 2007, 08:11 AM
main () // start of a new program
{
int i; // declaration of an integer "i"
int a[5] = {39,23,34,12,4}; // declaration and initialization of an integer "a", which is an array
int b[5]; // declaration of an integer "b", which is an array

asterisk_man
Jul 17, 2007, 08:16 AM
I will add many comments (way more than you would ever write in your own code)

Program #1:


//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:


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!

babykat
Sep 3, 2007, 06:56 AM
Can you please add more details to the second program?

Suddashil
Nov 16, 2007, 12:47 AM
Your questions are too basic. Pl go through any C book available and you will get the answer