Ask Experts Questions for FREE Help!
  Advanced
Register  |  Log in  
   Ask    
 Answer  
  Help  

Ask QuestionsprogressAnswer QuestionsprogressBuild ReputationprogressBecome an Expert
 
Free Answers in 3 Easy Steps

Register Now
3 Steps

At Ask Me Help Desk you can ask questions in any topic and have them answered for free by our experts. To ask questions or participate in answering them you must register for a free account. By registering you will be able to:
  • Get free answers from experts in any of our 300+ topics.
  • Accept money for answers that you provide.
  • Communicate privately with other members (PM).
  • See fewer ads.

Home > Computers & Technology > Programming > Compiled Languages > C   »   Meaning of these C programs

 
Thread Tools Display Modes
Question
 
 
#1  
Old Jul 17, 2007, 06:36 AM
babykat
New Member
babykat is offline
 
Join Date: Jul 2007
Posts: 9
babykat See this member's comment history on his/her Profile page.
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.

Reply With Quote
 
     

Answers
 
 
Old Jul 17, 2007, 07:11 AM   #2  
Ultra Member
benn11 is offline
 
benn11's Avatar
 
Join Date: Apr 2007
Posts: 1,042
benn11 See this member's comment history on his/her Profile page.
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
  Reply With Quote
 
     
 
 
Old Jul 17, 2007, 07:16 AM   #3  
Full Member
asterisk_man is offline
 
asterisk_man's Avatar
 
Join Date: Nov 2006
Location: East coast of U.S.A.
Posts: 472
asterisk_man See this member's comment history on his/her Profile page.
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!
  Reply With Quote
 
     
 
 
Old Sep 3, 2007, 05:56 AM   #4  
New Member
babykat is offline
 
Join Date: Jul 2007
Posts: 9
babykat See this member's comment history on his/her Profile page.
can you please add more details to the second program?
  Reply With Quote
 
     
 
 
Old Nov 15, 2007, 10:47 PM   #5  
New Member
Suddashil is offline
 
Join Date: Sep 2007
Posts: 6
Suddashil See this member's comment history on his/her Profile page.
your questions are too basic. Pl go through any C book available and you will get the answer
  Reply With Quote
 
     


Thread Tools
Display Modes

 
Similar Sponsors

Similar Threads
Question Asker Forum Answers Last Post
Must Have/Useful Programs LTheobald Computers for Beginners 60 Aug 31, 2008 10:52 PM
Anyone know what is the meaning of ..... moonbeam5689 Languages 2 Jul 24, 2007 05:47 PM
Programs didn't come zone Computers for Beginners 4 May 22, 2007 07:17 AM
Other meaning of a name Phoenix25 Pregnancy & New Motherhood 2 Feb 21, 2007 09:01 PM
programs little evil Computers for Beginners 1 Oct 25, 2006 08:52 PM




Copyright ©2003 - 2007, Ask Me Help Desk.
All times are GMT -8. The time now is 04:32 PM.