Can anyone explain what this section code is doing ?

This is a discussion on Can anyone explain what this section code is doing ? within the C Programming forums, part of the General Programming Boards category; Hi I came across this C code and I am a bit confused as to what this is actually doing. ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    15

    Can anyone explain what this section code is doing ?

    Hi I came across this C code and I am a bit confused as to what this is actually doing. Could someone spend some time to help a C novice out and explain exactly what this section of code is saying. Thanks

    Code:
    if(ge_event_ready)
    		  {
    
    		    if(ndo[1]==0)
    		      for(i=1;i<=ge_event_fold;i++)
    			{
    			  if(i==1) printf("BEFORE REORDERING=%d\n",ge_event_fold);
    			  printf("\tid:%d \tenergy:%d\n",ge_event_id[i],ge_event_energy[i]);
    			}
    
    		    for(j=1;j<=111;j++)
    		      reorder_ge_event[j]=0;
    
    		      for(i=1;i<=ge_event_fold;i++)
    			{
    			  reorder_ge_event[ge_event_id[i]]=ge_event_energy[i];
    			}
    
    		      k=0;
    		      for(i=1;i<=111;i++)
    			{
    			  if(reorder_ge_event[i]!=0)
    			    {
    			      k++;
    			      ge_event_id[k]=i;
    			      ge_event_energy[k]=reorder_ge_event[i];
    			    }			  
    			}
    
    		    for(j=1;j<=111;j++)
    		      reorder_ge_event[j]=0;
    
    		    if(ndo[1]==0)
    		      for(i=1;i<=ge_event_fold;i++)
    			{
    			  if(i==1) printf("AFTER REORDERING=%d\n",ge_event_fold);
    			  printf("\tid:%d \tenergy:%d\n",ge_event_id[i],ge_event_energy[i]);
    			}
    		    
    		  }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It would be hard to describe much here on a macro level because we don't know what all these variables are for. But I can provide a quick point about some basic syntax:
    Code:
    if(ge_event_ready)
    Is a truth test. "Truth" in C (and most programming languages I'm aware of) indicates a value that is non-zero. "Falsity" indicates a zero value. A NULL pointer in C is considered a zero value.

    Beyond that you may want to try some more specific questions.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    15
    Quote Originally Posted by MK27 View Post
    It would be hard to describe much here on a macro level because we don't know what all these variables are for. But I can provide a quick point about some basic syntax:
    Code:
    if(ge_event_ready)
    Is a truth test. "Truth" in C (and most programming languages I'm aware of) indicates a value that is non-zero. "Falsity" indicates a zero value. A NULL pointer in C is considered a zero value.

    Beyond that you may want to try some more specific questions.
    Thanks I know enough about c to know what a truth test is. I get a bit confused when it all becomes nested then j and k starts being introduced.
    If you want to know what the variables are then

    ndo = debugging flag
    ge_event_fold = number of gamma rays detected in an event
    ge_event_id[i]= the crystal number which detected the gamma ray event
    ge_event_energy [i]=energy of the gamma ray detected
    there are 111 crystal detectors
    reorder_ge_event means that that when an event occurs with fold larger than 1 (i.e more than one gamma ray detected) then order the events in ascending crystal number.

    I just dont understand how this is achieved when I look at this code.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Step 1 is don't omit the optional braces.
    Code:
    	for(j=1;j<=111;j++) {
    		reorder_ge_event[j]=0;
    	}
    It takes a fraction of a second to add them when you're editing the code (some editors will automatically add } for each { you insert).

    Finding them when they're missing (and shouldn't be) can take days in a lot of code.


    Next, modularise!
    Code:
    void showDebug( const char *msg, int len, int *event, int *energy ) {
    	int	i;
    	printf("%s=%d\n", msg, len );
    	for(i=1;i<=len;i++)
    	{
    		printf("\tid:%d \tenergy:%d\n",event[i],energy[i]);
    	}
    }
    
    void clearArray ( int len, int *array ) {
    	int	i;
    	for(i=1;i<=len;i++)
    	{
    		array[i] = 0;
    	}
    }
    
    
    if(ge_event_ready)
    {
    	const int maxlen = 111;
    	if(ndo[1]==0) {
    		showDebug( "BEFORE REORDERING", ge_event_fold, ge_event_id, ge_event_energy );
    	}
    
    	clearArray( maxlen, reorder_ge_event );
    
    	for(i=1;i<=ge_event_fold;i++)
    	{
    		reorder_ge_event[ge_event_id[i]]=ge_event_energy[i];
    	}
    
    	k=0;
    	for(i=1;i<=maxlen;i++)
    	{
    		if(reorder_ge_event[i]!=0)
    		{
    			k++;
    			ge_event_id[k]=i;
    			ge_event_energy[k]=reorder_ge_event[i];
    		}			  
    	}
    
    	clearArray( maxlen, reorder_ge_event );
    
    	if(ndo[1]==0) {
    		showDebug( "AFTER REORDERING", ge_event_fold, ge_event_id, ge_event_energy );
    	}
    }
    When you find yourself repeating basically the same code - say by copy/paste and change a few things, then create a function to do the work.

    Then you're not staring at so much detail all at once.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    15
    Thanks for the advice, but I did not actually write the code. As I have stated I was looking for someone with a fair bit of knowledge in C to be able to talk me through the code explaining what each line did.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Without knowing the context of the code snippet ("we don't know what all these variables are for"), describing to you what the code does is pretty boring: any novice in C should be able to figure out what the code does, line by line, because it consists of routine constructs used in routine ways.

    Where did you find this code snippet? Why do you want to know what it does?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    15
    Sorry to have bored you laserlight, By the way I think I define what the variables do above.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Oh, my mistake. One way to see how this might work is to consider an example. Consider:
    Let ge_event_fold = 2.
    Let ge_event_id[1] = 3.
    Let ge_event_id[2] = 1.
    Let ge_event_energy[1] = 123.
    Let ge_event_energy[2] = 456.

    At first all elements of reorder_ge_event are reset to 0. Then:
    reorder_ge_event[ge_event_id[1]] = ge_event_energy[1];
    hence:
    reorder_ge_event[3] = 123;
    likewise:
    reorder_ge_event[1] = 456;

    Next, the elements of reorder_ge_event are traversed from 1 to 111. Only those with non-zero values are processsed. Therefore,
    ge_event_id[1] = 1;
    ge_event_energy[1] = reorder_ge_event[1] = 456;
    and:
    ge_event_id[2] = 3;
    ge_event_energy[2] = reorder_ge_event[3] = 123;

    Observe that ge_event_id is now sorted in ascending order, and ge_event_energy is sorted correspondingly. How this works is that reorder_ge_event is a map of the crystal number and corresponding energy. By iterating over it in order of crystal number, one can then reorder the corresponding energy according to crystal number, instead of the original event number.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    15
    Thanks Laserlight it makes a lot more sense now.

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please explain this code
    By ChoCo in forum C Programming
    Replies: 8
    Last Post: 03-09-2010, 03:32 PM
  2. Help with mprotect and writing on code section
    By raghu2383 in forum Linux Programming
    Replies: 3
    Last Post: 06-20-2008, 02:40 AM
  3. Could someone explain this code for me please...
    By JoshR in forum C++ Programming
    Replies: 89
    Last Post: 06-26-2005, 01:20 AM
  4. Could Someone Please Explain This Code
    By HelpMePlease in forum C Programming
    Replies: 13
    Last Post: 05-27-2004, 12:53 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21