Ask Experts Questions for FREE Help !
Ask
    babykat's Avatar
    babykat Posts: 9, Reputation: 1
    New Member
     
    #1

    Jul 17, 2007, 07:36 AM
    Meaning of these C programs
    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's Avatar
    benn11 Posts: 1,036, Reputation: 43
    Ultra Member
     
    #2

    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's Avatar
    asterisk_man Posts: 476, Reputation: 32
    Full Member
     
    #3

    Jul 17, 2007, 08:16 AM
    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!
    babykat's Avatar
    babykat Posts: 9, Reputation: 1
    New Member
     
    #4

    Sep 3, 2007, 06:56 AM
    Can you please add more details to the second program?
    Suddashil's Avatar
    Suddashil Posts: 3, Reputation: 1
    New Member
     
    #5

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

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Must Have/Useful Programs [ 66 Answers ]

I thought it would be a good idea to get a list of useful programs that we could keep here. If a mod comes across this - could you sticky it please. It would be quite helpful. I'm bound to have missed some programs, so if there is some that you think should be on this list - post here or PM me...

Anyone know what is the meaning of... [ 2 Answers ]

Does anyone know the meaning of "Jannum" Is is good... :D... or bad :o??

Programs didn't come [ 4 Answers ]

When my computer was sent out for repair, it went through the "System Recovery" process. When it came back, it didn't have many programs (it had Quicken, random games, etc. but not much else). I put in the Applications Recovery CD & it added a few more programs (i.e. Adobe Acrobat, Windows Media...

Other meaning of a name [ 2 Answers ]

Is there any other meaning for the name 'Harrison' other than son of harry?

Programs [ 1 Answers ]

Is there a free download that will help me find all the programs on my computer and remove the old, duplicate, or unused ones safely


View more questions Search