Advertisement
Guest User

Witch

a guest
Mar 22nd, 2010
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1.  
  2. /* Sum numbers 0 ... 9 two at a time            */
  3. /* index increments                             */
  4. /* sum() function uses "pass by reference"      */
  5. /* even if it's only about one "return value"   */  
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10.  
  11. void sum(int, int, int * );
  12.  
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16.     int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  17.     int array_copy[10];
  18.     int i, j;
  19.  
  20.  
  21.     for(i=0; i<10; i++)
  22.         array_copy[i]=array[i];
  23.  
  24.     for(j=0; j<10; j++)
  25.         printf("%d\n", array_copy[j]);
  26.  
  27.     printf("\n");
  28.     printf("__________________________________________");
  29.     printf("\n\n");
  30.  
  31.  
  32.     for( i=0; i<9; i++)
  33.     {
  34.         sum(array[i], array[i+1], &array[i+1]);
  35.         printf("Partial sum %d equals: %d\n", i+1, array[i+1]);    
  36.     }
  37.    
  38.     printf("\nGrandtotal equals: %d\n", array[9]);
  39.  
  40.     printf("\n");
  41.     printf("__________________________________________");
  42.     printf("\n\n");
  43.  
  44.     for(j=0; j<10; j++)
  45.         printf("%d\n", array_copy[j]);
  46.  
  47.     printf("\n\n");
  48.  
  49.     system("PAUSE");   
  50.     return 0;
  51. }
  52.  
  53.  
  54. /*************
  55. * Functions
  56. *************/
  57. /* ___________________________________________________________________________*/
  58.  
  59. void sum(int a, int b, int *summa )
  60. {
  61.     *summa = a + b;  /* return with "pass by reference" */
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement